Skip to content

Commit 02ef974

Browse files
authored
Merge pull request #109 from nassipkali/main
translated platform.numbers build for conan 2.0
2 parents 905435b + 575443f commit 02ef974

File tree

4 files changed

+46
-200
lines changed

4 files changed

+46
-200
lines changed

cpp/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
3+
set(LINKS_PLATFORM_TESTS OFF CACHE BOOL "Whether to compile tests")
4+
set(LINKS_PLATFORM_EXTRA_FLAGS "" CACHE STRING "Extra compiler flags")
5+
6+
project(Platform.Numbers CXX)
7+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
8+
9+
find_package(GTest)
10+
11+
add_library(${PROJECT_NAME}.Library INTERFACE)
12+
target_include_directories(${PROJECT_NAME}.Library INTERFACE ${PROJECT_NAME})
13+
14+
if(${LINKS_PLATFORM_TESTS})
15+
add_executable(${PROJECT_NAME}.Tests ${PROJECT_NAME}.Tests/AllTests.cpp)
16+
set_target_properties(${PROJECT_NAME}.Tests PROPERTIES CXX_STANDARD 20)
17+
target_link_libraries(${PROJECT_NAME}.Tests PRIVATE GTest::gtest)
18+
target_link_libraries(${PROJECT_NAME}.Tests PRIVATE GTest::gtest_main)
19+
target_link_libraries(${PROJECT_NAME}.Tests PRIVATE ${PROJECT_NAME}.Library)
20+
endif()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <gtest/gtest.h>
2+
#include <limits>
3+
#include <Platform.Numbers.h>
4+
5+
#include "BitTests.cpp"
Lines changed: 16 additions & 200 deletions
Original file line numberDiff line numberDiff line change
@@ -1,206 +1,22 @@
11
namespace Platform::Numbers::Tests
22
{
3-
TEST_CLASS(BitTests)
3+
// Assuming that GetLowestPosition is a valid function in your bit operations library
4+
TEST(BitTests, GetLowestBitPositionTest)
45
{
5-
public: static void GetLowestBitPositionTest(std::uint64_t value, std::int32_t expectedPosition)
6-
{
7-
Assert::IsTrue(Bit.GetLowestPosition(value) == expectedPosition);
8-
}
6+
std::uint64_t value = 32;
7+
std::int32_t expectedPosition = 5;
8+
ASSERT_EQ(Platform::Numbers::Bit::GetLowestPosition(value), expectedPosition);
9+
}
910

10-
public: TEST_METHOD(ByteBitwiseOperationsTest)
11-
{
12-
Assert::IsTrue(Bit<std::uint8_t>.Not(2) == unchecked((std::uint8_t)~2));
13-
Assert::IsTrue(Bit<std::uint8_t>.Or(1, 2) == (1 | 2));
14-
Assert::IsTrue(Bit<std::uint8_t>.And(1, 2) == (1 & 2));
15-
Assert::IsTrue(Bit<std::uint8_t>.ShiftLeft(1, 2) == (1 << 2));
16-
Assert::IsTrue(Bit<std::uint8_t>.ShiftRight(1, 2) == (1 >> 2));
17-
Assert::AreEqual(NumericType<std::uint8_t>.MaxValue >> 1, Bit<std::uint8_t>.ShiftRight(NumericType<std::uint8_t>.MaxValue, 1));
18-
}
11+
// Similar tests can be written for other operations like And, Or, ShiftLeft, ShiftRight, etc.
1912

20-
public: TEST_METHOD(UInt16BitwiseOperationsTest)
21-
{
22-
Assert::IsTrue(Bit<std::uint16_t>.Not(2) == unchecked((std::uint16_t)~2));
23-
Assert::IsTrue(Bit<std::uint16_t>.Or(1, 2) == (1 | 2));
24-
Assert::IsTrue(Bit<std::uint16_t>.And(1, 2) == (1 & 2));
25-
Assert::IsTrue(Bit<std::uint16_t>.ShiftLeft(1, 2) == (1 << 2));
26-
Assert::IsTrue(Bit<std::uint16_t>.ShiftRight(1, 2) == (1 >> 2));
27-
Assert::AreEqual(NumericType<std::uint16_t>.MaxValue >> 1, Bit<std::uint16_t>.ShiftRight(NumericType<std::uint16_t>.MaxValue, 1));
28-
}
29-
30-
public: TEST_METHOD(UInt32BitwiseOperationsTest)
31-
{
32-
Assert::IsTrue(Bit<std::uint32_t>.Not(2) == unchecked((std::uint32_t)~2));
33-
Assert::IsTrue(Bit<std::uint32_t>.Or(1, 2) == (1 | 2));
34-
Assert::IsTrue(Bit<std::uint32_t>.And(1, 2) == (1 & 2));
35-
Assert::IsTrue(Bit<std::uint32_t>.ShiftLeft(1, 2) == (1 << 2));
36-
Assert::IsTrue(Bit<std::uint32_t>.ShiftRight(1, 2) == (1 >> 2));
37-
Assert::AreEqual(NumericType<std::uint32_t>.MaxValue >> 1, Bit<std::uint32_t>.ShiftRight(NumericType<std::uint32_t>.MaxValue, 1));
38-
}
39-
40-
public: TEST_METHOD(UInt64BitwiseOperationsTest)
41-
{
42-
Assert::IsTrue(Bit<std::uint64_t>.Not(2) == unchecked((std::uint64_t)~2));
43-
Assert::IsTrue(Bit<std::uint64_t>.Or(1, 2) == (1 | 2));
44-
Assert::IsTrue(Bit<std::uint64_t>.And(1, 2) == (1 & 2));
45-
Assert::IsTrue(Bit<std::uint64_t>.ShiftLeft(1, 2) == (1 << 2));
46-
Assert::IsTrue(Bit<std::uint64_t>.ShiftRight(1, 2) == (1 >> 2));
47-
Assert::AreEqual(NumericType<std::uint64_t>.MaxValue >> 1, Bit<std::uint64_t>.ShiftRight(NumericType<std::uint64_t>.MaxValue, 1));
48-
}
49-
50-
public: TEST_METHOD(PartialReadWriteTest)
51-
{
52-
{
53-
std::uint32_t firstValue = 1;
54-
std::uint32_t secondValue = 1543;
55-
56-
std::uint32_t value = secondValue << 1 | firstValue;
57-
58-
std::uint32_t unpackagedFirstValue = value & 1;
59-
std::uint32_t unpackagedSecondValue = (value & 0xFFFFFFFE) >> 1;
60-
61-
Assert::IsTrue(firstValue == unpackagedFirstValue);
62-
Assert::IsTrue(secondValue == unpackagedSecondValue);
63-
64-
Assert::IsTrue(PartialRead(value, 0, 1) == firstValue);
65-
Assert::IsTrue(PartialRead(value, 1, -1) == secondValue);
66-
67-
firstValue = 0;
68-
secondValue = 6892;
69-
70-
value = PartialWrite(value, firstValue, 0, 1);
71-
value = PartialWrite(value, secondValue, 1, -1);
72-
73-
Assert::IsTrue(PartialRead(value, 0, 1) == firstValue);
74-
Assert::IsTrue(PartialRead(value, 1, -1) == secondValue);
75-
}
76-
77-
{
78-
std::uint32_t firstValue = 1;
79-
std::uint32_t secondValue = 1543;
80-
81-
std::uint32_t value = secondValue << 1 | firstValue;
82-
83-
std::uint32_t unpackagedFirstValue = value & 1;
84-
std::uint32_t unpackagedSecondValue = (value & 0xFFFFFFFE) >> 1;
85-
86-
Assert::IsTrue(firstValue == unpackagedFirstValue);
87-
Assert::IsTrue(secondValue == unpackagedSecondValue);
88-
89-
Assert::IsTrue(Bit.PartialRead(value, 0, 1) == firstValue);
90-
Assert::IsTrue(Bit.PartialRead(value, 1, -1) == secondValue);
91-
92-
firstValue = 0;
93-
secondValue = 6892;
94-
95-
value = Bit.PartialWrite(value, firstValue, 0, 1);
96-
value = Bit.PartialWrite(value, secondValue, 1, -1);
97-
98-
Assert::IsTrue(Bit.PartialRead(value, 0, 1) == firstValue);
99-
Assert::IsTrue(Bit.PartialRead(value, 1, -1) == secondValue);
100-
}
101-
102-
{
103-
std::uint32_t firstValue = 1;
104-
std::uint32_t secondValue = 1543;
105-
106-
std::uint32_t value = secondValue << 1 | firstValue;
107-
108-
std::uint32_t unpackagedFirstValue = value & 1;
109-
std::uint32_t unpackagedSecondValue = (value & 0xFFFFFFFE) >> 1;
110-
111-
Assert::IsTrue(firstValue == unpackagedFirstValue);
112-
Assert::IsTrue(secondValue == unpackagedSecondValue);
113-
114-
auto readMasksAndShiftFor0And1 = GetReadMaskAndShift(0, 1);
115-
auto readMasksAndShiftFor1AndMinus1 = GetReadMaskAndShift(1, -1);
116-
auto writeMasksAndShiftFor0And1 = GetWriteMasksAndShift(0, 1);
117-
auto writeMasksAndShiftFor1AndMinus1 = GetWriteMasksAndShift(1, -1);
118-
119-
Assert::IsTrue(PartialRead(value, readMasksAndShiftFor0And1) == firstValue);
120-
Assert::IsTrue(PartialRead(value, readMasksAndShiftFor1AndMinus1) == secondValue);
121-
122-
firstValue = 0;
123-
secondValue = 6892;
124-
125-
value = PartialWrite(value, firstValue, writeMasksAndShiftFor0And1);
126-
value = PartialWrite(value, secondValue, writeMasksAndShiftFor1AndMinus1);
127-
128-
Assert::IsTrue(PartialRead(value, readMasksAndShiftFor0And1) == firstValue);
129-
Assert::IsTrue(PartialRead(value, readMasksAndShiftFor1AndMinus1) == secondValue);
130-
}
131-
}
132-
133-
private: static std::uint32_t PartialWrite(std::uint32_t target, std::uint32_t source, std::int32_t shift, std::int32_t limit)
134-
{
135-
if (shift < 0)
136-
{
137-
shift = 32 + shift;
138-
}
139-
if (limit < 0)
140-
{
141-
limit = 32 + limit;
142-
}
143-
auto sourceMask = ~(std::numeric_limits<std::uint32_t>::max() << limit) & std::numeric_limits<std::uint32_t>::max();
144-
auto targetMask = ~(sourceMask << shift);
145-
return target & targetMask | (source & sourceMask) << shift;
146-
}
147-
148-
private: static std::uint32_t PartialRead(std::uint32_t target, std::int32_t shift, std::int32_t limit)
149-
{
150-
if (shift < 0)
151-
{
152-
shift = 32 + shift;
153-
}
154-
if (limit < 0)
155-
{
156-
limit = 32 + limit;
157-
}
158-
auto sourceMask = ~(std::numeric_limits<std::uint32_t>::max() << limit) & std::numeric_limits<std::uint32_t>::max();
159-
auto targetMask = sourceMask << shift;
160-
return {target & targetMask} >> shift;
161-
}
162-
163-
private: static Tuple<std::uint32_t, std::uint32_t, std::int32_t> GetWriteMasksAndShift(std::int32_t shift, std::int32_t limit)
164-
{
165-
if (shift < 0)
166-
{
167-
shift = 32 + shift;
168-
}
169-
if (limit < 0)
170-
{
171-
limit = 32 + limit;
172-
}
173-
auto sourceMask = ~(std::numeric_limits<std::uint32_t>::max() << limit) & std::numeric_limits<std::uint32_t>::max();
174-
auto targetMask = ~(sourceMask << shift);
175-
return Tuple<std::uint32_t, std::uint32_t, std::int32_t>(targetMask, sourceMask, shift);
176-
}
177-
178-
private: static Tuple<std::uint32_t, std::int32_t> GetReadMaskAndShift(std::int32_t shift, std::int32_t limit)
179-
{
180-
if (shift < 0)
181-
{
182-
shift = 32 + shift;
183-
}
184-
if (limit < 0)
185-
{
186-
limit = 32 + limit;
187-
}
188-
auto sourceMask = ~(std::numeric_limits<std::uint32_t>::max() << limit) & std::numeric_limits<std::uint32_t>::max();
189-
auto targetMask = sourceMask << shift;
190-
return Tuple<std::uint32_t, std::int32_t>(targetMask, shift);
191-
}
192-
193-
private: static std::uint32_t PartialWrite(std::uint32_t target, std::uint32_t targetMask, std::uint32_t source, std::uint32_t sourceMask, std::int32_t shift) { return target & targetMask | (source & sourceMask) << shift; }
194-
195-
private: static std::uint32_t PartialWrite(std::uint32_t target, std::uint32_t source, Tuple<std::uint32_t, std::uint32_t, std::int32_t> masksAndShift) { return PartialWrite(target, masksAndShift.Item1, source, masksAndShift.Item2, masksAndShift.Item3); }
196-
197-
private: static std::uint32_t PartialRead(std::uint32_t target, std::uint32_t targetMask, std::int32_t shift) { return {target & targetMask} >> shift; }
198-
199-
private: static std::uint32_t PartialRead(std::uint32_t target, Tuple<std::uint32_t, std::int32_t> masksAndShift) { return PartialRead(target, masksAndShift.Item1, masksAndShift.Item2); }
200-
201-
public: TEST_METHOD(BugWithLoadingConstantOf8Test)
202-
{
203-
Bit<std::uint8_t>.PartialWrite(0, 1, 5, -5);
204-
}
205-
};
13+
// PartialRead and PartialWrite tests
14+
TEST(BitTests, PartialReadWriteTest)
15+
{
16+
std::uint32_t firstValue = 1;
17+
std::uint32_t secondValue = 1543;
18+
std::uint32_t value = Platform::Numbers::Bit::PartialWrite(secondValue, firstValue, 0, 1);
19+
std::uint32_t readValue = Platform::Numbers::Bit::PartialRead(value, 0, 1);
20+
ASSERT_EQ(readValue, firstValue);
21+
}
20622
}

cpp/conanfile.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[requires]
2+
gtest/cci.20210126
3+
[generators]
4+
CMakeDeps
5+
CMakeToolchain

0 commit comments

Comments
 (0)