Skip to content

Commit a3ce9b7

Browse files
committed
Math/Endian
1 parent 09915d1 commit a3ce9b7

8 files changed

Lines changed: 75 additions & 27 deletions

File tree

modules/Math/Endian.mpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export module CppUtils.Math.Endian;
2+
3+
import std;
4+
5+
export namespace CppUtils::Math
6+
{
7+
template<std::integral T>
8+
[[nodiscard]] inline constexpr auto toBigEndian(T value) -> T
9+
{
10+
if constexpr (std::endian::native == std::endian::big)
11+
return value;
12+
else
13+
return std::byteswap(value);
14+
}
15+
16+
template<std::integral T>
17+
[[nodiscard]] inline constexpr auto toLittleEndian(T value) -> T
18+
{
19+
if constexpr (std::endian::native == std::endian::little)
20+
return value;
21+
else
22+
return std::byteswap(value);
23+
}
24+
}

modules/Math/Math.mpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export module CppUtils.Math;
22

33
export import CppUtils.Math.Concept;
4+
export import CppUtils.Math.Endian;
45
export import CppUtils.Math.Float;
56
export import CppUtils.Math.Random;

modules/Network/Socket.mpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module;
1717
export module CppUtils.Network.Socket;
1818

1919
import std;
20+
import CppUtils.Math.Endian;
2021
import CppUtils.System.Error;
2122
import CppUtils.Type.Concept;
2223
import CppUtils.Chrono.Concept;
@@ -273,7 +274,7 @@ export namespace CppUtils::Network
273274
{
274275
auto* ipv4 = reinterpret_cast<sockaddr_in*>(std::addressof(address.storage));
275276
ipv4->sin_family = AF_INET;
276-
ipv4->sin_port = ::htons(port);
277+
ipv4->sin_port = Math::toBigEndian(port);
277278

278279
if (ip.empty())
279280
ipv4->sin_addr.s_addr = INADDR_ANY;
@@ -286,7 +287,7 @@ export namespace CppUtils::Network
286287
{
287288
auto* ipv6 = reinterpret_cast<sockaddr_in6*>(std::addressof(address.storage));
288289
ipv6->sin6_family = AF_INET6;
289-
ipv6->sin6_port = ::htons(port);
290+
ipv6->sin6_port = Math::toBigEndian(port);
290291

291292
if (ip.empty())
292293
ipv6->sin6_addr = in6addr_any;

tests/Math/Endian.mpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export module CppUtils.UnitTests.Math.Endian;
2+
3+
import std;
4+
import CppUtils;
5+
6+
export namespace CppUtils::UnitTest::Math::Endian
7+
{
8+
inline auto _ = TestSuite{"Endian", {"UnitTest"}, [](auto& suite) {
9+
constexpr auto value = std::uint32_t{0x12'34'56'78u};
10+
11+
suite.addTest("toBigEndian", [&] {
12+
constexpr auto expectedBig = (std::endian::native == std::endian::big) ? 0x12'34'56'78u : 0x78'56'34'12u;
13+
suite.expectEqual(CppUtils::Math::toBigEndian(value), expectedBig);
14+
});
15+
16+
suite.addTest("toLittleEndian", [&] {
17+
constexpr auto expectedLittle = (std::endian::native == std::endian::little) ? 0x12'34'56'78u : 0x78'56'34'12u;
18+
suite.expectEqual(CppUtils::Math::toLittleEndian(value), expectedLittle);
19+
});
20+
}};
21+
}

tests/Math/Float.mpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@ export module CppUtils.UnitTests.Math.Float;
33
import std;
44
import CppUtils;
55

6-
export namespace CppUtils::UnitTest::Math::Random
6+
export namespace CppUtils::UnitTest::Math::Float
77
{
8-
inline auto _ = TestSuite{"Random", {"UnitTest"}, [](auto& suite) {
9-
suite.addTest("getRandomNumberInInterval", [&] {
10-
constexpr auto min = 1;
11-
constexpr auto max = 10;
12-
for (int i = 0; i < 100; ++i)
13-
{
14-
auto randomNumber = CppUtils::Math::getRandomNumberInInterval(min, max);
15-
suite.expect(randomNumber >= min and randomNumber <= max);
16-
}
8+
inline auto _ = TestSuite{"Math/Float", {"UnitTest"}, [](auto& suite) {
9+
suite.addTest("absolute", [&] {
10+
suite.expectEqual(CppUtils::Math::absolute(0), 0u);
11+
suite.expectEqual(CppUtils::Math::absolute(1), 1u);
12+
suite.expectEqual(CppUtils::Math::absolute(-1), 1u);
1713
});
1814
}};
1915
}

tests/Math/Math.mpp

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/Math/Random.mpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export module CppUtils.UnitTests.Math.Random;
2+
3+
import std;
4+
import CppUtils;
5+
6+
export namespace CppUtils::UnitTest::Math::Random
7+
{
8+
inline auto _ = TestSuite{"Random", {"UnitTest"}, [](auto& suite) {
9+
suite.addTest("getRandomNumberInInterval", [&] {
10+
constexpr auto min = 1;
11+
constexpr auto max = 10;
12+
for (int i = 0; i < 100; ++i)
13+
{
14+
auto randomNumber = CppUtils::Math::getRandomNumberInInterval(min, max);
15+
suite.expect(randomNumber >= min and randomNumber <= max);
16+
}
17+
});
18+
}};
19+
}

tests/UnitTests.mpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export import CppUtils.UnitTests.Language.CallStackCompiler;
2323
export import CppUtils.UnitTests.Language.TreeParser;
2424
export import CppUtils.UnitTests.ChronoLogger;
2525
export import CppUtils.UnitTests.Logger;
26+
export import CppUtils.UnitTests.Math.Endian;
2627
export import CppUtils.UnitTests.Math.Float;
2728
export import CppUtils.UnitTests.Math.Random;
2829
export import CppUtils.UnitTests.Memory;

0 commit comments

Comments
 (0)