Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
Add Fixed Point unit tests
Add Vector unit tests
Add Colour unit tests
Add Date unit tests
Add Timespan unit tests
Add Approx to assist floating point comparisons

Add double Vector (for testing)
Add Vector <=>, >, >=, <, <= operators
Add Vector cross-type operators

Add `fixed_point_t::_0_25()`
Add Fixed Point float comparison operators
Make fixed_point_t(int64) constructor explicit
Fix fixed_point_t Vector abs function

Add rgb and rgba integer parsing to Colour
Add Colour string parse methods
Add invert operator to Colour
Add full_invert to Colour (includes alpha)
Add _rgba Colour user-defined literal

Add Timespan prefix and postfix -- operators
Add Timespan to_chars method

Add Date prefix and postfix -- operators
Add Date to_chars method
Improve Date to_string method
Add Date from_chars method
Improve Date from_string method
Add Date from_string_log method
  • Loading branch information
Spartan322 committed Feb 7, 2025
1 parent 6b921dc commit d4d2120
Show file tree
Hide file tree
Showing 26 changed files with 2,267 additions and 432 deletions.
7 changes: 4 additions & 3 deletions src/openvic-simulation/console/ConsoleInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <optional>
#include <string>
#include <string_view>
#include <system_error>

#include "openvic-simulation/DefinitionManager.hpp"
#include "openvic-simulation/InstanceManager.hpp"
Expand Down Expand Up @@ -286,9 +287,9 @@ std::optional<Date> ConsoleInstance::validate_date(std::string_view value_string
return std::nullopt;
}

bool success = true;
Date date = Date::from_string(value_string, &success, true);
if (!success) {
Date::from_chars_result result;
Date date = Date::from_string(value_string, &result);
if (result.ec != std::errc {}) {
write_error("Invalid date");
return std::nullopt;
}
Expand Down
8 changes: 5 additions & 3 deletions src/openvic-simulation/dataloader/Dataloader.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "Dataloader.hpp"

#include <system_error>

#include <openvic-dataloader/csv/Parser.hpp>
#include <openvic-dataloader/detail/CallbackOStream.hpp>
#include <openvic-dataloader/v2script/Parser.hpp>
Expand Down Expand Up @@ -581,10 +583,10 @@ bool Dataloader::_load_history(DefinitionManager& definition_manager, bool unuse
definition_manager.get_history_manager().get_bookmark_manager().get_last_bookmark_date();

for (std::string const& dir : pop_history_dirs) {
bool successful = false;
const Date date = Date::from_string(dir, &successful);
Date::from_chars_result result;
const Date date = Date::from_string_log(dir, &result);

if (successful && date <= last_bookmark_date) {
if (result.ec == std::errc{} && date <= last_bookmark_date) {
bool non_integer_size = false;

ret &= apply_to_files(
Expand Down
9 changes: 5 additions & 4 deletions src/openvic-simulation/dataloader/NodeTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <concepts>
#include <string_view>
#include <system_error>

#include <openvic-dataloader/detail/SymbolIntern.hpp>
#include <openvic-dataloader/detail/Utility.hpp>
Expand Down Expand Up @@ -207,15 +208,15 @@ node_callback_t NodeTools::expect_colour(callback_t<colour_t> callback) {

node_callback_t NodeTools::expect_colour_hex(callback_t<colour_argb_t> callback) {
return expect_uint<colour_argb_t::integer_type>([callback](colour_argb_t::integer_type val) -> bool {
return callback(colour_argb_t::from_integer(val));
return callback(colour_argb_t::from_argb(val));
}, 16);
}

callback_t<std::string_view> NodeTools::expect_date_str(callback_t<Date> callback) {
return [callback](std::string_view identifier) -> bool {
bool successful = false;
const Date date = Date::from_string(identifier, &successful);
if (successful) {
Date::from_chars_result result;
const Date date = Date::from_string_log(identifier, &result);
if (result.ec == std::errc{}) {
return callback(date);
}
Logger::error("Invalid date identifier text: ", identifier);
Expand Down
7 changes: 4 additions & 3 deletions src/openvic-simulation/history/HistoryMap.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <memory>
#include <system_error>

#include "openvic-simulation/dataloader/NodeTools.hpp"
#include "openvic-simulation/types/Date.hpp"
Expand Down Expand Up @@ -62,9 +63,9 @@ namespace OpenVic {
ast::NodeCPtr value, NodeTools::key_value_callback_t default_callback = NodeTools::key_value_invalid_callback
) {
/* Date blocks (loaded into the corresponding HistoryEntry) */
bool is_date = false;
const Date sub_date { Date::from_string(key, &is_date, true) };
if (is_date) {
Date::from_chars_result result;
const Date sub_date { Date::from_string(key, &result) };
if (result.ec == std::errc{}) {
if (sub_date < date) {
Logger::error("History entry ", sub_date, " defined before parent entry date ", date);
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/openvic-simulation/map/MapDefinition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ ProvinceDefinition::index_t MapDefinition::get_index_from_colour(colour_t colour
}

ProvinceDefinition::index_t MapDefinition::get_province_index_at(ivec2_t pos) const {
if (pos.nonnegative() && pos.less_than(dims)) {
if (pos.nonnegative() && pos.is_within_bound(dims)) {
return province_shape_image[get_pixel_index_from_pos(pos)].index;
}
return ProvinceDefinition::NULL_INDEX;
Expand Down
6 changes: 6 additions & 0 deletions src/openvic-simulation/types/Colour.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "openvic-simulation/types/Colour.hpp" // IWYU pragma: keep

namespace OpenVic {
template struct basic_colour_t<std::uint8_t, std::uint32_t>;
template struct basic_colour_t<std::uint8_t, std::uint32_t, rgb_colour_traits<std::uint8_t, std::uint32_t>>;
}
Loading

0 comments on commit d4d2120

Please sign in to comment.