-
Notifications
You must be signed in to change notification settings - Fork 0
Add literal operators, factory functions, and tests for primitives #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
819add9
feat: Add literal operators for integral and floating-point types wit…
FrozenLemonTee 67670f1
feat: Add with function for creating primitive types with policies
FrozenLemonTee d7598f2
feat: Add unit tests for primitive factory and underlying literals fu…
FrozenLemonTee 9558432
feat: Add literal operators for size_t and ptrdiff_t with constexpr s…
FrozenLemonTee e48b259
feat: Add primitive type aliases for size_t and ptrdiff_t
FrozenLemonTee b0399c8
test: Add tests for size_t and ptrdiff_t literal operators and primit…
FrozenLemonTee 4943abc
refactor: Move make_primitive implementation to details namespace
FrozenLemonTee 7f5a6b0
feat: Add with function for creating primitive types with policies
FrozenLemonTee 11beede
test: Add test for policies tuple input in PrimitiveFactory
FrozenLemonTee d6a50d8
refactor: Separate literal operators and validation logic into new li…
FrozenLemonTee faa87cd
refactor: Import literals module into primitives for better organization
FrozenLemonTee 3c9841d
test: Add static assertions for literal overflow and precision loss p…
FrozenLemonTee 2b08bda
refactor: Enhance floating-point literal handling with range checks a…
FrozenLemonTee 0041343
test: Rename precision loss and underflow probes for clarity and cons…
FrozenLemonTee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| #include <gtest/gtest.h> | ||
| #include <type_traits> | ||
|
|
||
| import mcpplibs.primitives; | ||
|
|
||
| #include "../../support/underlying_custom_types.hpp" | ||
|
|
||
| using namespace mcpplibs::primitives; | ||
| using namespace mcpplibs::primitives::literals; | ||
| using namespace mcpplibs::primitives::test_support::underlying; | ||
|
|
||
| TEST(PrimitiveFactoryTest, MakesPrimitiveFromDeducedStdUnderlying) { | ||
| using expected_t = | ||
| types::I32<policy::value::checked, policy::error::expected>; | ||
|
|
||
| auto value = with<policy::value::checked, policy::error::expected>(42_i32); | ||
|
|
||
| static_assert(std::same_as<decltype(value), expected_t>); | ||
| EXPECT_EQ(value.load(), 42); | ||
| } | ||
|
|
||
| TEST(PrimitiveFactoryTest, UsesDefaultPoliciesWhenNoPolicyIsSpecified) { | ||
| using expected_t = types::I32<>; | ||
| using meta_t = meta::traits<expected_t>; | ||
|
|
||
| auto value = with(42_i32); | ||
|
|
||
| static_assert(std::same_as<decltype(value), expected_t>); | ||
| static_assert(std::same_as<meta_t::value_policy, | ||
| policy::defaults::value>); | ||
| static_assert( | ||
| std::same_as<meta_t::type_policy, policy::defaults::type>); | ||
| static_assert( | ||
| std::same_as<meta_t::error_policy, policy::defaults::error>); | ||
| static_assert(std::same_as<meta_t::concurrency_policy, | ||
| policy::defaults::concurrency>); | ||
| EXPECT_EQ(value.load(), 42); | ||
| } | ||
|
|
||
| TEST(PrimitiveFactoryTest, DeducesSizeAndDiffPrimitiveAliases) { | ||
| auto sizeValue = with(42_size); | ||
| auto diffValue = with(42_diff); | ||
|
|
||
| static_assert(std::same_as<decltype(sizeValue), types::Size<>>); | ||
| static_assert(std::same_as<decltype(diffValue), types::Diff<>>); | ||
| EXPECT_EQ(sizeValue.load(), static_cast<std::size_t>(42)); | ||
| EXPECT_EQ(diffValue.load(), static_cast<std::ptrdiff_t>(42)); | ||
| } | ||
|
|
||
| TEST(PrimitiveFactoryTest, AcceptsPoliciesTupleInput) { | ||
| using policies_t = | ||
| meta::traits<types::I32<policy::value::checked, | ||
| policy::error::expected>>::policies; | ||
| using expected_t = meta::make_primitive_t<std::int32_t, policies_t>; | ||
|
|
||
| auto value1 = with(policies_t{}, 42_i32); | ||
| auto value2 = with(meta::traits<decltype(value1)>::policies{}, 42_i32); | ||
|
|
||
| static_assert(std::same_as<decltype(value1), expected_t>); | ||
| static_assert(std::same_as<decltype(value2), expected_t>); | ||
| EXPECT_EQ(value1.load(), 42); | ||
| EXPECT_EQ(value2.load(), 42_i32); | ||
| } | ||
|
|
||
| TEST(PrimitiveFactoryTest, MakesPrimitiveFromDeducedCustomUnderlying) { | ||
| using expected_t = | ||
| primitive<UserInteger, policy::value::checked, policy::type::compatible>; | ||
|
|
||
| auto value = | ||
| with<policy::value::checked, policy::type::compatible>(UserInteger{7}); | ||
|
|
||
| static_assert(std::same_as<decltype(value), expected_t>); | ||
| EXPECT_EQ(value.load().value, 7); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <gtest/gtest.h> | ||
| #include <type_traits> | ||
|
|
||
| import mcpplibs.primitives.underlying; | ||
|
|
||
| using namespace mcpplibs::primitives::literals; | ||
|
|
||
| TEST(UnderlyingLiteralsTest, IntegerLiteralsReturnExpectedUnderlyingTypes) { | ||
| static_assert(std::same_as<decltype(42_u8), std::uint8_t>); | ||
| static_assert(std::same_as<decltype(42_u16), std::uint16_t>); | ||
| static_assert(std::same_as<decltype(42_u32), std::uint32_t>); | ||
| static_assert(std::same_as<decltype(42_u64), std::uint64_t>); | ||
| static_assert(std::same_as<decltype(42_size), std::size_t>); | ||
| static_assert(std::same_as<decltype(42_diff), std::ptrdiff_t>); | ||
| static_assert(std::same_as<decltype(42_i8), std::int8_t>); | ||
| static_assert(std::same_as<decltype(42_i16), std::int16_t>); | ||
| static_assert(std::same_as<decltype(42_i32), std::int32_t>); | ||
| static_assert(std::same_as<decltype(42_i64), std::int64_t>); | ||
|
|
||
| EXPECT_EQ(42_u8, static_cast<std::uint8_t>(42)); | ||
| EXPECT_EQ(42_u16, static_cast<std::uint16_t>(42)); | ||
| EXPECT_EQ(42_u32, static_cast<std::uint32_t>(42)); | ||
| EXPECT_EQ(42_u64, static_cast<std::uint64_t>(42)); | ||
| EXPECT_EQ(42_size, static_cast<std::size_t>(42)); | ||
| EXPECT_EQ(42_diff, static_cast<std::ptrdiff_t>(42)); | ||
| EXPECT_EQ(42_i8, static_cast<std::int8_t>(42)); | ||
| EXPECT_EQ(42_i16, static_cast<std::int16_t>(42)); | ||
| EXPECT_EQ(42_i32, static_cast<std::int32_t>(42)); | ||
| EXPECT_EQ(42_i64, static_cast<std::int64_t>(42)); | ||
| } | ||
|
|
||
| TEST(UnderlyingLiteralsTest, FloatingLiteralsReturnExpectedUnderlyingTypes) { | ||
| static_assert(std::same_as<decltype(1.25_f32), float>); | ||
| static_assert(std::same_as<decltype(1.25_f64), double>); | ||
| static_assert(std::same_as<decltype(1.25_f80), long double>); | ||
|
|
||
| EXPECT_FLOAT_EQ(1.25_f32, 1.25f); | ||
| EXPECT_DOUBLE_EQ(1.25_f64, 1.25); | ||
| EXPECT_EQ(1.25_f80, static_cast<long double>(1.25)); | ||
| EXPECT_FLOAT_EQ(2_f32, 2.0f); | ||
| EXPECT_DOUBLE_EQ(2_f64, 2.0); | ||
| EXPECT_EQ(2_f80, static_cast<long double>(2.0)); | ||
| } | ||
|
|
||
| TEST(UnderlyingLiteralsTest, CharacterLiteralsReturnExpectedUnderlyingTypes) { | ||
| static_assert(std::same_as<decltype('A'_uchar), unsigned char>); | ||
| static_assert(std::same_as<decltype(u8'A'_char8), char8_t>); | ||
| static_assert(std::same_as<decltype(u'A'_char16), char16_t>); | ||
| static_assert(std::same_as<decltype(U'A'_char32), char32_t>); | ||
| static_assert(std::same_as<decltype(L'A'_wchar), wchar_t>); | ||
|
|
||
| EXPECT_EQ('A'_uchar, static_cast<unsigned char>('A')); | ||
| EXPECT_EQ(u8'A'_char8, u8'A'); | ||
| EXPECT_EQ(u'A'_char16, u'A'); | ||
| EXPECT_EQ(U'A'_char32, U'A'); | ||
| EXPECT_EQ(L'A'_wchar, L'A'); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.