From c59aeef2e0c86ec3f0f34ce77c205f3ef0b034cc Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Sun, 8 Nov 2026 22:04:06 +0100 Subject: Abstract move. Forstio independence --- modules/core/tests/abstract_data.cpp | 390 +++++++++++++++++++++++++++++ modules/core/tests/abstract_foundation.cpp | 189 ++++++++++++++ modules/core/tests/abstract_json.cpp | 326 ++++++++++++++++++++++++ modules/core/tests/chunk.cpp | 4 +- modules/core/tests/converter.cpp | 2 +- modules/core/tests/equilibrium.cpp | 8 +- modules/core/tests/flatten.cpp | 8 +- modules/core/tests/iterator.cpp | 30 +-- modules/core/tests/math.cpp | 12 +- modules/core/tests/memory.cpp | 2 +- modules/core/tests/particles.cpp | 40 +-- modules/core/tests/vtk_write.cpp | 4 +- 12 files changed, 960 insertions(+), 55 deletions(-) create mode 100644 modules/core/tests/abstract_data.cpp create mode 100644 modules/core/tests/abstract_foundation.cpp create mode 100644 modules/core/tests/abstract_json.cpp (limited to 'modules/core/tests') diff --git a/modules/core/tests/abstract_data.cpp b/modules/core/tests/abstract_data.cpp new file mode 100644 index 0000000..1ee3d62 --- /dev/null +++ b/modules/core/tests/abstract_data.cpp @@ -0,0 +1,390 @@ +#include + +#include "../c++/abstract/data.hpp" +#include "../c++/abstract/data_math.hpp" +#include "../c++/abstract/iterator.hpp" +#include "../c++/abstract/math.hpp" + +namespace { +namespace ts { +using namespace kel::lbm::sch; +} + +using kel::lbm::data; +using kel::lbm::enc::Native; + +SAW_TEST("Abstract/Data/Primitive"){ + data a{4u}; + data b{6u}; + + SAW_EXPECT(a.get() == 4u, "value round trips"); + SAW_EXPECT((a + b).get() == 10u, "addition"); + SAW_EXPECT((b - a).get() == 2u, "subtraction"); + SAW_EXPECT((a * b).get() == 24u, "multiplication"); + SAW_EXPECT((b / a).get() == 1u, "integer division"); + + a += b; + SAW_EXPECT(a.get() == 10u, "compound addition mutates"); + a -= b; + SAW_EXPECT(a.get() == 4u, "compound subtraction mutates"); + + SAW_EXPECT(a < b, "ordering"); + SAW_EXPECT(b > a, "reverse ordering"); + SAW_EXPECT(a != b, "inequality"); + SAW_EXPECT(a.equals(data{4u}), "equality"); + + ++a; + SAW_EXPECT(a.get() == 5u, "increment"); +} + +SAW_TEST("Abstract/Data/PrimitiveCast"){ + data d{2.75}; + auto as_int = d.cast_to(); + SAW_EXPECT(as_int.get() == 2, "float to int truncates"); + + data small{255u}; + auto wide = small.cast_to(); + SAW_EXPECT(wide.get() == 255u, "widening preserves the value"); +} + +SAW_TEST("Abstract/Data/Bool"){ + data t{true}; + data f{false}; + + SAW_EXPECT(t.get(), "true round trips"); + SAW_EXPECT((!t).get() == false, "negation"); + SAW_EXPECT((t && f).get() == false, "conjunction"); + SAW_EXPECT((t || f).get() == true, "disjunction"); + SAW_EXPECT(t != f, "inequality"); +} + +SAW_TEST("Abstract/Data/MixedPrecision"){ + // Stored as float, read as double. + using TMixed = ts::MixedPrecision; + + data v{1.5}; + SAW_EXPECT(v.get() == 1.5, "a value exactly representable in float survives"); + + data w{2.5}; + SAW_EXPECT((v + w).get() == 4.0, "arithmetic happens at interface precision"); + + static_assert(std::is_same_v, + "reads come back as the interface type"); +} + +SAW_TEST("Abstract/Data/FixedArrayRankOne"){ + data> arr{1u,2u,3u}; + + SAW_EXPECT(arr.at({0u}).get() == 1u, "single index access"); + SAW_EXPECT(arr.at({2u}).get() == 3u, "last element"); + SAW_EXPECT(arr.at({{1u}}).get() == 2u, "index array access"); + SAW_EXPECT(arr.flat_at(1u).get() == 2u, "flat access"); + + arr.at({1u}).set(9u); + SAW_EXPECT(arr.at({1u}).get() == 9u, "writes through"); + + SAW_EXPECT(arr.size().get() == 3u, "size"); + SAW_EXPECT(decltype(arr)::get_dim_size<0u>() == 3u, "compile time extent"); +} + +SAW_TEST("Abstract/Data/FixedArrayRankTwo"){ + data> arr; + + SAW_EXPECT(arr.size().get() == 6u, "flat size is the product of extents"); + + // Row major: index {i,j} sits at i + j*2. + arr.at({{1u,2u}}).set(7u); + SAW_EXPECT(arr.at({{1u,2u}}).get() == 7u, "two dimensional access"); + SAW_EXPECT(arr.flat_at(1u + 2u*2u).get() == 7u, "and its flat position"); + + SAW_EXPECT(decltype(arr)::get_dim_size<0u>() == 2u, "first extent"); + SAW_EXPECT(decltype(arr)::get_dim_size<1u>() == 3u, "second extent"); +} + +SAW_TEST("Abstract/Data/FixedArrayRankZero"){ + // The empty product is one, so a rank zero shape still holds one element. + data> scalar_like; + SAW_EXPECT(scalar_like.size().get() == 1u, "rank zero holds exactly one element"); + + scalar_like.at({}).set(3.5); + SAW_EXPECT(scalar_like.at({}).get() == 3.5, "addressed with an empty index"); +} + +SAW_TEST("Abstract/Data/Array"){ + // Extents are runtime, so they come in as the metadata. + data> arr{data>{{3u}}}; + + SAW_EXPECT(arr.size().get() == 3u, "sized from its metadata"); + SAW_EXPECT(arr.get_dim_size(0u) == 3u, "extent is readable"); + + arr.at({1u}).set(5u); + SAW_EXPECT(arr.at({1u}).get() == 5u, "single index access"); + SAW_EXPECT(arr.at({{1u}}).get() == 5u, "index array access"); + + auto eov = arr.add(data{8u}); + SAW_EXPECT(eov.is_value(), "appending to the last dimension works"); + SAW_EXPECT(arr.size().get() == 4u, "and grows it"); + SAW_EXPECT(arr.at({3u}).get() == 8u, "appended value lands last"); +} + +SAW_TEST("Abstract/Data/ArrayFromIntegralExtents"){ + data> arr{2u, 3u}; + + SAW_EXPECT(arr.size().get() == 6u, "extents multiply out"); + arr.at({{1u,2u}}).set(4u); + SAW_EXPECT(arr.at({{1u,2u}}).get() == 4u, "two dimensional access"); +} + +SAW_TEST("Abstract/Data/String"){ + data s{std::string{"hello"}}; + + SAW_EXPECT(s.size().get() == 5u, "length"); + SAW_EXPECT(s.get_at({1u}) == 'e', "indexed read"); + + s.set_at({0u}, 'H'); + SAW_EXPECT(s.stl_view() == std::string_view{"Hello"}, "indexed write"); + + // A String is described by its length, so building one from metadata + // gives a string of that length. + data sized{data{3u}}; + SAW_EXPECT(sized.size().get() == 3u, "sized from its metadata"); +} + +SAW_TEST("Abstract/Data/Tuple"){ + data> t; + + t.get<0u>().set(7u); + t.get<1u>().set(true); + + SAW_EXPECT(t.get<0u>().get() == 7u, "first member"); + SAW_EXPECT(t.get<1u>().get() == true, "second member"); + SAW_EXPECT(decltype(t)::size() == 2u, "member count"); +} + +SAW_TEST("Abstract/Data/Struct"){ + using TStruct = ts::Struct< + ts::Member, + ts::Member + >; + + data s; + s.get<"count">().set(12u); + s.get<"weight">().set(0.25); + + SAW_EXPECT(s.get<"count">().get() == 12u, "member by name"); + SAW_EXPECT(s.get<"weight">().get() == 0.25, "second member by name"); + SAW_EXPECT(s.get_at_index<0u>().get() == 12u, "and by position"); + SAW_EXPECT(decltype(s)::size().get() == 2u, "member count"); +} + +SAW_TEST("Abstract/Data/Union"){ + using TUnion = ts::Union< + ts::Member, + ts::Member + >; + + data u; + u.init<"number">().set(3u); + + SAW_EXPECT(u.holds_alternative<"number">(), "holds the initialised member"); + SAW_EXPECT(!u.holds_alternative<"flag">(), "and not the other one"); + SAW_EXPECT(u.get<"number">().get() == 3u, "value reads back"); + + u.init<"flag">().set(true); + SAW_EXPECT(u.holds_alternative<"flag">(), "switching alternative"); + SAW_EXPECT(u.get<"flag">().get() == true, "new value reads back"); +} + +SAW_TEST("Abstract/Data/Tensor"){ + data> a{1.0, 2.0, 3.0}; + data> b{4.0, 5.0, 6.0}; + + SAW_EXPECT(a.at({{0u}}).get() == 1.0, "component access"); + SAW_EXPECT(a.flat_at(2u).get() == 3.0, "flat access"); + + auto sum = a + b; + SAW_EXPECT(sum.flat_at(0u).get() == 5.0, "componentwise addition"); + SAW_EXPECT(sum.flat_at(2u).get() == 9.0, "componentwise addition, last"); + + auto diff = b - a; + SAW_EXPECT(diff.flat_at(1u).get() == 3.0, "componentwise subtraction"); + + auto neg = -a; + SAW_EXPECT(neg.flat_at(0u).get() == -1.0, "negation"); +} + +SAW_TEST("Abstract/Data/TensorScalarOps"){ + data> v{3.0, 4.0}; + auto two = kel::lbm::math::make_scalar(2.0); + + auto scaled = v * two; + SAW_EXPECT(scaled.flat_at(0u).get() == 6.0, "scalar multiply"); + + auto halved = v / two; + SAW_EXPECT(halved.flat_at(1u).get() == 2.0, "scalar divide"); +} + +SAW_TEST("Abstract/Data/ScalarTensor"){ + // Rank zero: one element, addressed with an empty index. + data> s{data{2.5}}; + + SAW_EXPECT(s.at({}).get() == 2.5, "empty index access"); + SAW_EXPECT(s.flat_at(0u).get() == 2.5, "flat access"); + SAW_EXPECT(decltype(s)::size().get() == 1u, "holds one element"); +} + +SAW_TEST("Abstract/Math/DotAndNorm"){ + using namespace kel::lbm; + + auto a = math::make_vector(3.0, 4.0); + auto b = math::make_vector(1.0, 0.0); + + auto d = math::dot(a, b); + SAW_EXPECT(d.flat_at(0u).get() == 3.0, "dot product"); + + auto n = math::norm(a); + SAW_EXPECT(n.flat_at(0u).get() == 5.0, "3-4-5 triangle"); +} + +SAW_TEST("Abstract/Math/Normalize"){ + using namespace kel::lbm; + + auto a = math::make_vector(0.0, 2.0); + auto u = math::normalize(a); + + SAW_EXPECT(u.flat_at(0u).get() == 0.0, "x component"); + SAW_EXPECT(u.flat_at(1u).get() == 1.0, "y component is unit length"); + + // A zero vector has no direction to preserve, so it comes back unchanged + // rather than dividing by zero. + auto zero = math::make_vector(0.0, 0.0); + auto uz = math::normalize(zero); + SAW_EXPECT(uz.flat_at(0u).get() == 0.0, "zero vector survives untouched"); +} + +SAW_TEST("Abstract/Math/Cross"){ + using namespace kel::lbm; + + auto x = math::make_vector(1.0, 0.0, 0.0); + auto y = math::make_vector(0.0, 1.0, 0.0); + + auto z = math::cross(x, y); + SAW_EXPECT(z.flat_at(2u).get() == 1.0, "x cross y is z"); + + // In two dimensions only the normal component survives. + auto a = math::make_vector(1.0, 0.0); + auto b = math::make_vector(0.0, 1.0); + auto s = math::cross(a, b); + SAW_EXPECT(s.flat_at(0u).get() == 1.0, "2d cross is a scalar"); +} + +SAW_TEST("Abstract/Math/Vectorize"){ + using namespace kel::lbm; + + data> arr{1.0, 2.0, 3.0}; + auto v = math::vectorize_data(arr); + + SAW_EXPECT(v.flat_at(0u).get() == 1.0, "first component carried over"); + SAW_EXPECT(v.flat_at(2u).get() == 3.0, "last component carried over"); +} + +SAW_TEST("Abstract/Math/MatrixVectorMultiply"){ + using namespace kel::lbm; + + // Column major by the row-major-index convention used in get_flat_index: + // at({{i,j}}) is element i of column j. + data> m; + m.at({{0u,0u}}).set(1.0); + m.at({{0u,1u}}).set(2.0); + m.at({{1u,0u}}).set(3.0); + m.at({{1u,1u}}).set(4.0); + + auto v = math::make_vector(1.0, 1.0); + auto r = math::multiply(m, v); + + SAW_EXPECT(r.flat_at(0u).get() == 3.0, "first row sums to 3"); + SAW_EXPECT(r.flat_at(1u).get() == 7.0, "second row sums to 7"); +} + +SAW_TEST("Abstract/Math/SumAndFill"){ + using namespace kel::lbm; + + data> v; + math::fill(v, data{2.0}); + + SAW_EXPECT(v.flat_at(1u).get() == 2.0, "fill writes every element"); + + auto s = math::sum(v); + SAW_EXPECT(s.flat_at(0u).get() == 6.0, "sum over the tensor"); +} + +SAW_TEST("Abstract/Iterator/RankOne"){ + using namespace kel::lbm; + + uint64_t visits = 0u; + auto eov = rank_iterator<3u>::in_fixed_bounds( + [&](const index_data<1u>& index) -> error_or { + SAW_EXPECT(index.flat_at(0u).get() == visits, "visited in order"); + ++visits; + return make_void(); + }); + + SAW_EXPECT(eov.is_value(), "iteration succeeded"); + SAW_EXPECT(visits == 3u, "visited every position"); +} + +SAW_TEST("Abstract/Iterator/RankTwo"){ + using namespace kel::lbm; + + // This is the case that could not compile against forstio's iterator, + // because it indexed a rank two index array with a bare integer. + uint64_t visits = 0u; + uint64_t last_i = 0u; + uint64_t last_j = 0u; + + auto eov = rank_iterator<2u,3u>::in_fixed_bounds( + [&](const index_data<2u>& index) -> error_or { + last_i = index.flat_at(0u).get(); + last_j = index.flat_at(1u).get(); + ++visits; + return make_void(); + }); + + SAW_EXPECT(eov.is_value(), "iteration succeeded"); + SAW_EXPECT(visits == 6u, "visited every position of a 2x3 shape"); + SAW_EXPECT(last_i == 1u, "finished on the last i"); + SAW_EXPECT(last_j == 2u, "finished on the last j"); +} + +SAW_TEST("Abstract/Iterator/RankZero"){ + using namespace kel::lbm; + + uint64_t visits = 0u; + auto eov = rank_iterator<>::in_fixed_bounds( + [&](const index_data<0u>&) -> error_or { + ++visits; + return make_void(); + }); + + SAW_EXPECT(eov.is_value(), "iteration succeeded"); + SAW_EXPECT(visits == 1u, "rank zero still has one position"); +} + +SAW_TEST("Abstract/Iterator/PropagatesError"){ + using namespace kel::lbm; + + uint64_t visits = 0u; + auto eov = rank_iterator<5u>::in_fixed_bounds( + [&](const index_data<1u>&) -> error_or { + ++visits; + if(visits == 2u){ + return make_error(); + } + return make_void(); + }); + + SAW_EXPECT(eov.is_error(), "the error reaches the caller"); + SAW_EXPECT(eov.get_error().is_type(), "and keeps its type"); + SAW_EXPECT(visits == 2u, "iteration stopped at the failure"); +} +} diff --git a/modules/core/tests/abstract_foundation.cpp b/modules/core/tests/abstract_foundation.cpp new file mode 100644 index 0000000..aea967d --- /dev/null +++ b/modules/core/tests/abstract_foundation.cpp @@ -0,0 +1,189 @@ +#include + +#include "../c++/abstract/common.hpp" +#include "../c++/abstract/error.hpp" +#include "../c++/abstract/schema.hpp" +#include "../c++/abstract/stringify_schema.hpp" +#include "../c++/abstract/templates.hpp" + +namespace { +namespace ts { +using namespace kel::lbm::sch; +} + +SAW_TEST("Abstract/Templates/CtMultiply"){ + using namespace kel::lbm; + + static_assert(ct_multiply::value == 1u, "empty pack is the identity"); + static_assert(ct_multiply::value == 3u, ""); + static_assert(ct_multiply::value == 24u, ""); +} + +SAW_TEST("Abstract/Templates/ParameterPack"){ + using namespace kel::lbm; + + static_assert(std::is_same_v::type, int>, ""); + static_assert(std::is_same_v::type, bool>, ""); + + static_assert(parameter_pack_index::value == 1u, ""); + + static_assert(parameter_pack_value<1u, uint64_t, 7u, 9u, 11u>::value == 9u, ""); +} + +SAW_TEST("Abstract/Templates/KeyPack"){ + using namespace kel::lbm; + + static_assert(parameter_key_pack_index<"b", "a", "b", "c">::value == 1u, ""); + static_assert(parameter_key_pack_type<2u, "a", "b", "c">::literal == string_literal{"c"}, ""); +} + +SAW_TEST("Abstract/Templates/DigitConversion"){ + using namespace kel::lbm; + + static_assert(ct_convert_to_digits<0u,10u>::literal == string_literal{"0"}, ""); + static_assert(ct_convert_to_digits<7u,10u>::literal == string_literal{"7"}, ""); + static_assert(ct_convert_to_digits<1024u,10u>::literal == string_literal{"1024"}, ""); +} + +SAW_TEST("Abstract/StringLiteral/Concat"){ + using namespace kel::lbm; + + static_assert((string_literal{"foo"} + string_literal{"bar"}) == string_literal{"foobar"}, ""); + static_assert(string_literal{"foo"}.view() == std::string_view{"foo"}, ""); +} + +SAW_TEST("Abstract/Schema/Meta"){ + using namespace kel::lbm; + + // Compile time shapes carry nothing at runtime. + static_assert(std::is_same_v, ""); + static_assert(std::is_same_v::Meta, ts::Void>, ""); + + // A dynamically sized Array is described by its extents. + static_assert(std::is_same_v< + ts::Array::Meta, + ts::FixedArray>, ""); + + // A String is described by its length. + static_assert(std::is_same_v, ""); + + // A Tensor is addressed through a rank sized index. + static_assert(std::is_same_v< + ts::Vector::Meta, + ts::FixedArray>, ""); + static_assert(std::is_same_v< + ts::Scalar::Meta, + ts::FixedArray>, ""); + + // Structural schemas recurse into their members. + using TStruct = ts::Struct< + ts::Member, + ts::Member,"values"> + >; + static_assert(std::is_same_v< + TStruct::Meta, + ts::Struct< + ts::Member, + ts::Member,"values"> + >>, ""); + + static_assert(std::is_same_v< + ts::Tuple::Meta, + ts::Tuple>, ""); +} + +SAW_TEST("Abstract/Schema/PrimitiveShape"){ + using namespace kel::lbm; + + static_assert(ts::UInt64::Bytes == 8u, ""); + static_assert(std::is_same_v, ""); + static_assert(std::is_same_v, ""); + static_assert(std::is_same_v, ""); + + using TMixed = ts::MixedPrecision; + static_assert(std::is_same_v, ""); + static_assert(std::is_same_v, ""); + + static_assert(ts::FixedArray::FlatSize == 6u, ""); + static_assert(ts::FixedArray::Rank == 2u, ""); + static_assert(ts::Scalar::FlatSize == 1u, "rank zero holds one element"); +} + +SAW_TEST("Abstract/SchemaStringify/Primitive"){ + using namespace kel::lbm; + + static_assert(schema_to_string::literal + == string_literal{"Primitive"}, ""); + static_assert(schema_to_string::literal + == string_literal{"Primitive"}, ""); +} + +SAW_TEST("Abstract/SchemaStringify/Composite"){ + using namespace kel::lbm; + + static_assert(schema_to_string>::literal + == string_literal{"FixedArray,2,3>"}, ""); + + static_assert(schema_to_string>::literal + == string_literal{"Tuple"}, ""); +} + +SAW_TEST("Abstract/Error/MakeError"){ + using namespace kel::lbm; + + auto err = make_error("missing thing"); + SAW_EXPECT(err.failed(), "a not_found is an error"); + SAW_EXPECT(err.is_recoverable(), "not_found is recoverable"); + SAW_EXPECT(err.is_type(), "error keeps its type"); + SAW_EXPECT(!err.is_type(), "and is distinguishable"); + SAW_EXPECT(err.get_message() == std::string_view{"missing thing"}, "message survives"); +} + +SAW_TEST("Abstract/Error/Critical"){ + using namespace kel::lbm; + + auto err = make_error(); + SAW_EXPECT(err.is_critical(), "out_of_memory is critical"); + SAW_EXPECT(!err.is_recoverable(), "and therefore not recoverable"); +} + +SAW_TEST("Abstract/Error/ErrorOr"){ + using namespace kel::lbm; + + error_or good{uint64_t{42u}}; + SAW_EXPECT(good.is_value(), "holds a value"); + SAW_EXPECT(!good.is_error(), "and not an error"); + SAW_EXPECT(good.get_value() == 42u, "value round trips"); + + error_or bad{make_error()}; + SAW_EXPECT(bad.is_error(), "holds an error"); + SAW_EXPECT(!bad.is_value(), "and not a value"); + SAW_EXPECT(bad.get_error().is_type(), "error type survives"); +} + +SAW_TEST("Abstract/Error/VoidPath"){ + using namespace kel::lbm; + + error_or ok{make_void()}; + SAW_EXPECT(ok.is_value(), "make_void is a value, not an error"); +} + +SAW_TEST("Abstract/Common/Pointers"){ + using namespace kel::lbm; + + auto owned = heap(7u); + SAW_EXPECT(*owned == 7u, "heap constructs in place"); + + ptr empty; + SAW_EXPECT(!empty.is_valid(), "a default ptr is empty"); + + uint64_t backing = 3u; + ptr filled{backing}; + SAW_EXPECT(filled.is_valid(), "a bound ptr is valid"); + SAW_EXPECT(filled() == 3u, "and reads through"); + + ref r{backing}; + r() = 9u; + SAW_EXPECT(backing == 9u, "ref writes through to its referent"); +} +} diff --git a/modules/core/tests/abstract_json.cpp b/modules/core/tests/abstract_json.cpp new file mode 100644 index 0000000..9592158 --- /dev/null +++ b/modules/core/tests/abstract_json.cpp @@ -0,0 +1,326 @@ +#include + +#include "../c++/abstract/json.hpp" + +namespace { +namespace ts { +using namespace kel::lbm::sch; +} + +using kel::lbm::codec; +using kel::lbm::data; +using kel::lbm::enc::Json; +using kel::lbm::enc::Native; + +template +std::string encode_to_string(const data& value, bool pretty = false){ + data out; + codec c; + c.set_pretty(pretty); + auto eov = c.encode(value, out); + SAW_EXPECT(eov.is_value(), "encoding succeeded"); + return out.stl_string(); +} + +SAW_TEST("Abstract/Json/EncodePrimitives"){ + SAW_EXPECT(encode_to_string(data{42u}) == "42", "unsigned"); + SAW_EXPECT(encode_to_string(data{-7}) == "-7", "signed"); + SAW_EXPECT(encode_to_string(data{true}) == "true", "bool true"); + SAW_EXPECT(encode_to_string(data{false}) == "false", "bool false"); +} + +SAW_TEST("Abstract/Json/EncodeString"){ + data s{std::string{"a\"b\\c\nd"}}; + // Quote, backslash and newline all have to come back escaped. + SAW_EXPECT(encode_to_string(s) == "\"a\\\"b\\\\c\\nd\"", "escapes applied"); +} + +SAW_TEST("Abstract/Json/DecodePrimitives"){ + data u; + codec cu; + SAW_EXPECT(cu.decode(data{std::string{"123"}}, u).is_value(), "decoded"); + SAW_EXPECT(u.get() == 123u, "unsigned round trips"); + + data f; + codec cf; + SAW_EXPECT(cf.decode(data{std::string{"-0.5e1"}}, f).is_value(), "decoded"); + SAW_EXPECT(f.get() == -5.0, "exponent form is understood"); +} + +SAW_TEST("Abstract/Json/FloatRoundTrip"){ + // max_digits10 precision is what makes this exact rather than approximate. + const double original = 0.1234567890123456789; + data in{original}; + + data out; + codec c; + auto eov = c.decode(data{encode_to_string(in)}, out); + SAW_EXPECT(eov.is_value(), "round trip decoded"); + SAW_EXPECT(out.get() == in.get(), "a double survives a save and load cycle"); +} + +SAW_TEST("Abstract/Json/IntegerRejectsFraction"){ + // Truncating 1.5 into an integer field would lose data quietly. + data u; + codec c; + auto eov = c.decode(data{std::string{"1.5"}}, u); + SAW_EXPECT(eov.is_error(), "a fractional value is not an integer"); +} + +SAW_TEST("Abstract/Json/UnsignedRejectsNegative"){ + data u; + codec c; + auto eov = c.decode(data{std::string{"-1"}}, u); + SAW_EXPECT(eov.is_error(), "a negative value is not unsigned"); +} + +SAW_TEST("Abstract/Json/EncodeAndDecodeStruct"){ + using TStruct = ts::Struct< + ts::Member, + ts::Member + >; + + data in; + in.get<"count">().set(3u); + in.get<"weight">().set(0.5); + + auto text = encode_to_string(in); + SAW_EXPECT(text == "{\"count\":3,\"weight\":0.5}", "object layout"); + + data out; + codec c; + auto eov = c.decode(data{text}, out); + SAW_EXPECT(eov.is_value(), "decoded"); + SAW_EXPECT(out.get<"count">().get() == 3u, "count round trips"); + SAW_EXPECT(out.get<"weight">().get() == 0.5, "weight round trips"); +} + +SAW_TEST("Abstract/Json/StructIgnoresUnknownKeys"){ + using TStruct = ts::Struct>; + + data out; + codec c; + // A nested unknown value has to be skipped whole, not just one token. + auto eov = c.decode( + data{std::string{ + "{\"extra\":{\"a\":[1,2,{\"b\":\"x\"}]},\"count\":9}"}}, + out); + + SAW_EXPECT(eov.is_value(), "unknown members are skipped"); + SAW_EXPECT(out.get<"count">().get() == 9u, "known member still lands"); +} + +SAW_TEST("Abstract/Json/StructMissingMember"){ + using TStruct = ts::Struct< + ts::Member, + ts::Member + >; + + // Lenient by default: a config states only its deviations. + data lenient; + codec c; + auto eov = c.decode(data{std::string{"{\"count\":1}"}}, lenient); + SAW_EXPECT(eov.is_value(), "a missing member is allowed by default"); + SAW_EXPECT(lenient.get<"other">().get() == 0u, "and keeps its default"); + + data strict; + codec cs; + cs.set_strict(true); + auto eov_s = cs.decode(data{std::string{"{\"count\":1}"}}, strict); + SAW_EXPECT(eov_s.is_error(), "strict decoding demands every member"); +} + +SAW_TEST("Abstract/Json/FixedArray"){ + using TArr = ts::FixedArray; + + data in{1u,2u,3u}; + auto text = encode_to_string(in); + SAW_EXPECT(text == "[1,2,3]", "array layout"); + + data out; + codec c; + SAW_EXPECT(c.decode(data{text}, out).is_value(), "decoded"); + SAW_EXPECT(out.flat_at(2u).get() == 3u, "elements round trip"); +} + +SAW_TEST("Abstract/Json/FixedArrayLengthMismatch"){ + using TArr = ts::FixedArray; + data out; + + codec c_short; + SAW_EXPECT(c_short.decode(data{std::string{"[1,2]"}}, out).is_error(), + "too few elements is an error, there is no default to fill with"); + + codec c_long; + SAW_EXPECT(c_long.decode(data{std::string{"[1,2,3,4]"}}, out).is_error(), + "too many elements is an error, there is nowhere to put them"); +} + +SAW_TEST("Abstract/Json/DynamicArrayGrows"){ + using TArr = ts::Array; + + data out; + codec c; + SAW_EXPECT(c.decode(data{std::string{"[5,6,7,8]"}}, out).is_value(), "decoded"); + SAW_EXPECT(out.size().get() == 4u, "array grew to fit the document"); + SAW_EXPECT(out.at({3u}).get() == 8u, "last element landed"); +} + +SAW_TEST("Abstract/Json/Tuple"){ + using TTup = ts::Tuple; + + data in; + in.get<0u>().set(4u); + in.get<1u>().set(true); + + auto text = encode_to_string(in); + SAW_EXPECT(text == "[4,true]", "tuple is an array"); + + data out; + codec c; + SAW_EXPECT(c.decode(data{text}, out).is_value(), "decoded"); + SAW_EXPECT(out.get<0u>().get() == 4u, "first element"); + SAW_EXPECT(out.get<1u>().get() == true, "second element"); +} + +SAW_TEST("Abstract/Json/Tensor"){ + using TVec = ts::Vector; + + data in{1.0, 2.0, 3.0}; + auto text = encode_to_string(in); + + data out; + codec c; + SAW_EXPECT(c.decode(data{text}, out).is_value(), "decoded"); + SAW_EXPECT(out.flat_at(1u).get() == 2.0, "components round trip"); +} + +SAW_TEST("Abstract/Json/Nested"){ + using TInner = ts::Struct>; + using TOuter = ts::Struct< + ts::Member, + ts::Member,"pair"> + >; + + data in; + in.get<"inner">().get<"n">().set(1u); + in.get<"pair">().flat_at(0u).set(2u); + in.get<"pair">().flat_at(1u).set(3u); + + auto text = encode_to_string(in); + SAW_EXPECT(text == "{\"inner\":{\"n\":1},\"pair\":[2,3]}", "nested layout"); + + data out; + codec c; + SAW_EXPECT(c.decode(data{text}, out).is_value(), "decoded"); + SAW_EXPECT(out.get<"inner">().get<"n">().get() == 1u, "nested member"); + SAW_EXPECT(out.get<"pair">().flat_at(1u).get() == 3u, "nested array"); +} + +SAW_TEST("Abstract/Json/Whitespace"){ + using TStruct = ts::Struct>; + + data out; + codec c; + auto eov = c.decode( + data{std::string{" {\n\t\"count\" : 5\r\n} "}}, out); + + SAW_EXPECT(eov.is_value(), "whitespace around tokens is ignored"); + SAW_EXPECT(out.get<"count">().get() == 5u, "value still read"); +} + +SAW_TEST("Abstract/Json/RejectsTrailingContent"){ + data out; + codec c; + auto eov = c.decode(data{std::string{"1 2"}}, out); + SAW_EXPECT(eov.is_error(), "content after the value means a malformed document"); +} + +SAW_TEST("Abstract/Json/RejectsMalformed"){ + using TStruct = ts::Struct>; + data out; + + codec c_unclosed; + SAW_EXPECT(c_unclosed.decode(data{std::string{"{\"count\":1"}}, out).is_error(), + "unterminated object"); + + codec c_empty; + SAW_EXPECT(c_empty.decode(data{std::string{""}}, out).is_error(), + "empty input"); + + codec c_garbage; + SAW_EXPECT(c_garbage.decode(data{std::string{"{\"count\":}"}}, out).is_error(), + "missing value"); +} + +SAW_TEST("Abstract/Json/DepthLimit"){ + using TArr = ts::Array; + + // The guard is against pathological nesting in untrusted input; a flat + // document sits well inside it. + data out; + kel::lbm::codec_config cfg; + cfg.depth = 1u; + codec c{cfg}; + SAW_EXPECT(c.decode(data{std::string{"[1,2]"}}, out).is_value(), + "one level of nesting is within a depth of one"); +} + +SAW_TEST("Abstract/Json/StringEscapeRoundTrip"){ + data in{std::string{"tab\there \"quoted\" back\\slash"}}; + + data out; + codec c; + auto eov = c.decode(data{encode_to_string(in)}, out); + + SAW_EXPECT(eov.is_value(), "decoded"); + SAW_EXPECT(out.stl_view() == in.stl_view(), "escapes survive a round trip"); +} + +SAW_TEST("Abstract/Json/UnicodeEscape"){ + data out; + codec c; + // U+00E9 encodes to two UTF-8 bytes. + auto eov = c.decode(data{std::string{"\"\\u00e9\""}}, out); + + SAW_EXPECT(eov.is_value(), "decoded"); + SAW_EXPECT(out.size().get() == 2u, "decoded to UTF-8 rather than one byte"); +} + +SAW_TEST("Abstract/Json/PrettyPrint"){ + using TStruct = ts::Struct>; + + data in; + in.get<"count">().set(1u); + + auto text = encode_to_string(in, true); + SAW_EXPECT(text == "{\n \"count\": 1\n}", "pretty layout"); + + // Pretty output still has to parse back. + data out; + codec c; + SAW_EXPECT(c.decode(data{text}, out).is_value(), "reparsed"); + SAW_EXPECT(out.get<"count">().get() == 1u, "value preserved"); +} + +SAW_TEST("Abstract/Json/LbmConfigShape"){ + // The shape config.hpp actually loads. + using TConfig = ts::Struct< + ts::Member, + ts::Member, + ts::Member + >; + + data out; + codec c; + auto eov = c.decode( + data{std::string{ + "{\"delta_x\":0.01,\"kinematic_viscosity\":1.5e-5,\"delta_t\":0.001}"}}, + out); + + SAW_EXPECT(eov.is_value(), "a config document decodes"); + SAW_EXPECT(out.get<"delta_x">().get() == 0.01, "delta_x"); + SAW_EXPECT(out.get<"kinematic_viscosity">().get() == 1.5e-5, "viscosity"); + SAW_EXPECT(out.get<"delta_t">().get() == 0.001, "delta_t"); +} +} diff --git a/modules/core/tests/chunk.cpp b/modules/core/tests/chunk.cpp index 008d4c6..72d1341 100644 --- a/modules/core/tests/chunk.cpp +++ b/modules/core/tests/chunk.cpp @@ -4,7 +4,7 @@ namespace { namespace sch { -using namespace saw::schema; +using namespace kel::lbm::sch; } SAW_TEST("Chunk Ghost size 0"){ @@ -39,7 +39,7 @@ SAW_TEST("Chunk 2D setup"){ using TestChunk = lbm::sch::Chunk; - auto chunk = saw::heap>(); + auto chunk = kel::lbm::heap>(); chunk->at({{0u,0u}}).set(1.0f); SAW_EXPECT((chunk->ghost_at({{1u,1u}}).get() == 1.0f),"Check if ghost is at shifted spot."); } diff --git a/modules/core/tests/converter.cpp b/modules/core/tests/converter.cpp index 4fc536f..fa4a46c 100644 --- a/modules/core/tests/converter.cpp +++ b/modules/core/tests/converter.cpp @@ -18,7 +18,7 @@ SAW_TEST("Si Meter to Lbm Meter"){ {0.1} }; - saw::data> si_m{1.0}; + kel::lbm::data> si_m{1.0}; auto lbm_m = converter.meter_si_to_lbm(si_m); SAW_EXPECT(lbm_m.handle().get() == 10.0, "Correct si to lbm conversion"); diff --git a/modules/core/tests/equilibrium.cpp b/modules/core/tests/equilibrium.cpp index 7d36600..ad11699 100644 --- a/modules/core/tests/equilibrium.cpp +++ b/modules/core/tests/equilibrium.cpp @@ -11,15 +11,15 @@ void check_equilibrium(){ using dfi = lbm::df_info; - saw::data> rho; + kel::lbm::data> rho; rho.at({}).set(1.0); - saw::data> vel; - for(saw::data i{0u}; i.get() < Descriptor::D; ++i){ + kel::lbm::data> vel; + for(kel::lbm::data i{0u}; i.get() < Descriptor::D; ++i){ vel.at({{i}}) = {0.0}; } auto eq = lbm::equilibrium(rho,vel); - for(saw::data i{0u}; i.get() < Descriptor::Q; ++i){ + for(kel::lbm::data i{0u}; i.get() < Descriptor::Q; ++i){ SAW_EXPECT(eq.at(i).get() == dfi::weights[i.get()], std::string{"No momentum and normalized rho should be exactly the weights: "} + std::to_string(eq.at(i).get()) + std::string{" "} + std::to_string(dfi::weights[i.get()])); } } diff --git a/modules/core/tests/flatten.cpp b/modules/core/tests/flatten.cpp index 84b3fa4..3da05b5 100644 --- a/modules/core/tests/flatten.cpp +++ b/modules/core/tests/flatten.cpp @@ -4,15 +4,15 @@ namespace { namespace sch { -using namespace saw::schema; +using namespace kel::lbm::sch; } SAW_TEST("Flatten Index Stride"){ using namespace kel; - constexpr saw::data zero = lbm::flatten_index::stride<0u>({{2u,4u,3u}}); - constexpr saw::data one = lbm::flatten_index::stride<1u>({{2u,4u,3u}}); - constexpr saw::data two = lbm::flatten_index::stride<2u>({{2u,4u,3u}}); + constexpr kel::lbm::data zero = lbm::flatten_index::stride<0u>({{2u,4u,3u}}); + constexpr kel::lbm::data one = lbm::flatten_index::stride<1u>({{2u,4u,3u}}); + constexpr kel::lbm::data two = lbm::flatten_index::stride<2u>({{2u,4u,3u}}); SAW_EXPECT(zero.get() == 1u, "Zero is correct"); SAW_EXPECT(one.get() == 2u, "Zero is correct"); diff --git a/modules/core/tests/iterator.cpp b/modules/core/tests/iterator.cpp index 919c12c..4c1f877 100644 --- a/modules/core/tests/iterator.cpp +++ b/modules/core/tests/iterator.cpp @@ -12,10 +12,10 @@ using namespace kel::lbm::sch; SAW_TEST("Old Iterate"){ using namespace kel; - saw::data> start{{0u,0u}}; - saw::data> end{{3u,3u}}; + kel::lbm::data> start{{0u,0u}}; + kel::lbm::data> end{{3u,3u}}; - lbm::iterate_over([](const saw::data>& index){ + lbm::iterate_over([](const kel::lbm::data>& index){ std::cout<<"Index: "< > tup; - lbm::ct_tuple_iterator::Schema>::apply([](auto& val) -> saw::error_or { - return saw::make_void(); + lbm::ct_tuple_iterator::Schema>::apply([](auto& val) -> kel::lbm::error_or { + return kel::lbm::make_void(); },tup); } SAW_TEST("Old Iterate with Distance 1"){ using namespace kel; - saw::data> start{{0u,0u}}; - saw::data> end{{4u,4u}}; + kel::lbm::data> start{{0u,0u}}; + kel::lbm::data> end{{4u,4u}}; - lbm::iterate_over([](const saw::data>& index){ + lbm::iterate_over([](const kel::lbm::data>& index){ std::cout<<"Index: "<> start{{0u,0u}}; - saw::data> end{{3u,3u}}; + kel::lbm::data> start{{0u,0u}}; + kel::lbm::data> end{{3u,3u}}; - lbm::iterator<2u>::apply([](const saw::data>& index){ + lbm::iterator<2u>::apply([](const kel::lbm::data>& index){ std::cout<<"Index: "<> start{{0u,0u}}; - saw::data> end{{4u,4u}}; + kel::lbm::data> start{{0u,0u}}; + kel::lbm::data> end{{4u,4u}}; - lbm::iterator<2u>::apply([](const saw::data>& index){ + lbm::iterator<2u>::apply([](const kel::lbm::data>& index){ std::cout<<"Index: "<,2u>> field; + kel::lbm::data,2u>> field; { field.at({{0u}}).at({{0u}}).set(-1.0); field.at({{1u}}).at({{0u}}).set(2.0); } - saw::data> pos; + kel::lbm::data> pos; pos.at({{0u}}).set(0.3); } SAW_TEST("Math/Floor Index and Fraction from Position"){ using namespace kel; - saw::data> pos; + kel::lbm::data> pos; { pos.at({{0u}}) = 43.999; @@ -48,14 +48,14 @@ SAW_TEST("Math/Floor Index and Fraction from Position"){ SAW_TEST("Math/Floor Index and Fraction from Position and Bounded"){ using namespace kel; - saw::data> pos; + kel::lbm::data> pos; { pos.at({{0u}}) = 43.999; pos.at({{1u}}) = -50.0; } - saw::data> bound; + kel::lbm::data> bound; { bound.at({{0u}}) = 32u; bound.at({{1u}}) = 16u; diff --git a/modules/core/tests/memory.cpp b/modules/core/tests/memory.cpp index d59d212..f0b1499 100644 --- a/modules/core/tests/memory.cpp +++ b/modules/core/tests/memory.cpp @@ -4,7 +4,7 @@ namespace { namespace sch { -using namespace saw::schema; +using namespace kel::lbm::sch; using TStruct = Struct< Member, diff --git a/modules/core/tests/particles.cpp b/modules/core/tests/particles.cpp index e060097..823bcd7 100644 --- a/modules/core/tests/particles.cpp +++ b/modules/core/tests/particles.cpp @@ -12,7 +12,7 @@ using T = Float64; SAW_TEST("Verlet step 2D - Planar"){ using namespace kel; - saw::data> particle; + kel::lbm::data> particle; auto& body = particle.template get<"rigid_body">(); auto& pos = body.template get<"position">(); auto& pos_old = body.template get<"position_old">(); @@ -22,7 +22,7 @@ SAW_TEST("Verlet step 2D - Planar"){ acc.at({{0}}).set({1.0}); - saw::data> dt; + kel::lbm::data> dt; dt.at({}).set(0.5); lbm::verlet_step_lambda(particle,dt); @@ -33,7 +33,7 @@ SAW_TEST("Verlet step 2D - Planar"){ SAW_TEST("No Collision Spheroid 2D"){ using namespace kel; - saw::data> part_a; + kel::lbm::data> part_a; { auto& body = part_a.template get<"rigid_body">(); auto& pos = body.template get<"position">(); @@ -42,7 +42,7 @@ SAW_TEST("No Collision Spheroid 2D"){ pos.at({{1u}}) = 0.2; } - saw::data> part_b; + kel::lbm::data> part_b; { auto& body = part_b.template get<"rigid_body">(); auto& pos = body.template get<"position">(); @@ -59,7 +59,7 @@ SAW_TEST("No Collision Spheroid 2D"){ SAW_TEST("Collision Spheroid 2D"){ using namespace kel; - saw::data> part_a; + kel::lbm::data> part_a; { auto& body = part_a.template get<"rigid_body">(); auto& pos = body.template get<"position">(); @@ -72,7 +72,7 @@ SAW_TEST("Collision Spheroid 2D"){ pos.at({{1u}}) = 0.2; } - saw::data> part_b; + kel::lbm::data> part_b; { auto& body = part_b.template get<"rigid_body">(); auto& pos = body.template get<"position">(); @@ -93,7 +93,7 @@ SAW_TEST("Collision Spheroid 2D"){ SAW_TEST("Moving particles 2D"){ using namespace kel; - saw::data> part_a; + kel::lbm::data> part_a; { auto& body = part_a.template get<"rigid_body">(); auto& pos = body.template get<"position">(); @@ -110,7 +110,7 @@ SAW_TEST("Moving particles 2D"){ acc.at({{0u}}) = 0.1; } - saw::data> part_b; + kel::lbm::data> part_b; { auto& body = part_b.template get<"rigid_body">(); auto& pos = body.template get<"position">(); @@ -127,7 +127,7 @@ SAW_TEST("Moving particles 2D"){ acc.at({{0u}}) = -0.1; } - saw::data> dt; + kel::lbm::data> dt; dt.at({}).set(0.5); bool has_collided = false; for(uint64_t i = 0u; i < 32u; ++i){ @@ -147,7 +147,7 @@ SAW_TEST("Moving particles 2D"){ SAW_TEST("Particle Matrix Rotation"){ using namespace kel; - saw::data> part_a; + kel::lbm::data> part_a; { auto& body = part_a.template get<"rigid_body">(); auto& pos = body.template get<"position">(); @@ -164,7 +164,7 @@ SAW_TEST("Particle Matrix Rotation"){ SAW_TEST("Particle Collision Impulse"){ using namespace kel; - saw::data> part_a; + kel::lbm::data> part_a; { auto& body = part_a.template get<"rigid_body">(); auto& pos = body.template get<"position">(); @@ -181,7 +181,7 @@ SAW_TEST("Particle Collision Impulse"){ acc.at({{0u}}) = 0.1; } - saw::data> part_b; + kel::lbm::data> part_b; { auto& body = part_b.template get<"rigid_body">(); auto& pos = body.template get<"position">(); @@ -210,15 +210,15 @@ SAW_TEST("Minor Test for mask"){ auto& grid = mask.template get<"grid">(); - for(saw::data i{0u}; i < grid.template get_dim_size<0>(); ++i){ - for(saw::data j{0u}; j < grid.template get_dim_size<1>(); ++j){ + for(kel::lbm::data i{0u}; i < grid.template get_dim_size<0>(); ++i){ + for(kel::lbm::data j{0u}; j < grid.template get_dim_size<1>(); ++j){ std::cout<> reference_mask{{{4+2,4+2}}}; + //kel::lbm::data> reference_mask{{{4+2,4+2}}}; //reference_mask.at({{0,0}}); } */ @@ -228,7 +228,7 @@ SAW_TEST("Verlet integration test 2D"){ lbm::particle_system> system; { - saw::data> particle; + kel::lbm::data> particle; auto& rb = particle.template get<"rigid_body">(); auto& acc = rb.template get<"acceleration">(); auto& pos = rb.template get<"position">(); @@ -245,21 +245,21 @@ SAW_TEST("Verlet integration test 2D"){ auto& rb = p.template get<"rigid_body">(); auto& pos = rb.template get<"position">(); - for(saw::data i{0u}; i < saw::data{2u}; ++i){ + for(kel::lbm::data i{0u}; i < kel::lbm::data{2u}; ++i){ std::cout<{1e-1}); + system.step(kel::lbm::data{1e-1}); { auto& p = system.at({0u}); auto& rb = p.template get<"rigid_body">(); auto& pos = rb.template get<"position">(); - for(saw::data i{0u}; i < saw::data{2u}; ++i){ + for(kel::lbm::data i{0u}; i < kel::lbm::data{2u}; ++i){ std::cout<> radi,dense; + kel::lbm::data> radi,dense; radi.at({}) = 2.2f; dense.at({}) = 1.0f; auto spheroid_pgrp = lbm::create_spheroid_particle_group(radi,dense,{8u}); diff --git a/modules/core/tests/vtk_write.cpp b/modules/core/tests/vtk_write.cpp index 1de25bf..b2a5682 100644 --- a/modules/core/tests/vtk_write.cpp +++ b/modules/core/tests/vtk_write.cpp @@ -44,7 +44,7 @@ SAW_TEST("VTK Write test example"){ std::stringstream sstream; - saw::data, 2>> cells{{{2u,2u}}}; + kel::lbm::data, 2>> cells{{{2u,2u}}}; auto& cell_0 = cells.at({{{0,0}}}); cell_0.template get<"momentum">()= {{0.5,-0.1}}; @@ -69,7 +69,7 @@ SAW_TEST("VTK Write raw test example"){ std::stringstream sstream; - saw::data, 2>> cells{{{2u,2u}}}; + kel::lbm::data, 2>> cells{{{2u,2u}}}; auto meta = cells.dims(); -- cgit v1.3.1