Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
uid_test.cc
Go to the documentation of this file.
1
9#include <gtest/gtest.h>
10#include <uid.H>
11#include <string>
12#include <cstring>
13#include <vector>
14#include <set>
15
16using namespace Aleph;
17
18// =============================================================================
19// Basic Functionality Tests
20// =============================================================================
21
23 Uid id;
24 // Default constructor leaves members uninitialized or zero-initialized depending on implementation details
25 // but should be safe to call.
26 // We can't assert specific values unless we know the implementation initializes them.
27 // Based on the code, members are mutable but not initialized in default ctor.
28 // However, it should be a valid object.
29 (void)id;
30}
31
33 IPv4_Address ip = 3232235777; // 192.168.1.1 as uint32_t
34 uint64_t counter = 1234567890123456789ULL;
35 uint32_t port = 8080;
36
37 Uid id(ip, counter, port);
38
39 EXPECT_EQ(id.getIpAddr(), ip);
40 EXPECT_EQ(id.get_counter(), counter);
41 EXPECT_EQ(id.get_port_number(), port);
42
43 // Random number should be initialized
44 // We can't predict it, but we can check it's accessible
45 (void)id.get_random_number();
46}
47
49 IPv4_Address ip = 167772161; // 10.0.0.1 as uint32_t
50 uint64_t counter = 42;
51 uint32_t port = 1234;
52
53 Uid id1(ip, counter, port);
54
55 // Create a copy manually to ensure same random number
56 // Since random number is generated in constructor, we can't just construct another one
57 // with same parameters and expect equality (random numbers would differ).
58 // We need to test copy construction or string round-trip.
59
60 // Let's test self-equality first
62
63 // Test copy
64 Uid id2 = id1;
66
67 // Test inequality (different counter)
68 Uid id3(ip, counter + 1, port);
70}
71
73 IPv4_Address ip = 2130706433; // 127.0.0.1 as uint32_t
74 uint64_t counter = 987654321;
75 uint32_t port = 5000;
76
77 Uid original(ip, counter, port);
78
79 char buffer[Uid::stringSize];
80 char* result = original.getStringUid(buffer, sizeof(buffer));
81
82 ASSERT_NE(result, nullptr);
83 EXPECT_EQ(result, buffer);
84 EXPECT_EQ(strlen(buffer), static_cast<size_t>(Uid::stringSize - 1));
85
86 // Reconstruct from string
87 Uid reconstructed(buffer);
88
90 EXPECT_EQ(reconstructed.getIpAddr(), ip);
91 EXPECT_EQ(reconstructed.get_counter(), counter);
92 EXPECT_EQ(reconstructed.get_port_number(), port);
93 EXPECT_EQ(reconstructed.get_random_number(), original.get_random_number());
94}
95
96// =============================================================================
97// Exception Handling and Edge Cases
98// =============================================================================
99
101 IPv4_Address ip = 2130706433; // 127.0.0.1
102 // Use explicit types to avoid ambiguity with deprecated constructor
103 uint64_t counter = 1;
104 uint32_t port = 1;
105 Uid id(ip, counter, port);
106
107 char small_buffer[10]; // Too small
108
109 // Should throw range_error (ah_range_error_if macro)
110 EXPECT_THROW(id.getStringUid(small_buffer, sizeof(small_buffer)), std::range_error);
111}
112
114 // Verify that consecutive UIDs are different (due to random number)
115 IPv4_Address ip = 2130706433; // 127.0.0.1
116 uint64_t counter = 1;
117 uint32_t port = 1;
118
119 std::set<std::string> uids;
120 const int num_uids = 100;
121
122 for (int i = 0; i < num_uids; ++i) {
123 Uid id(ip, counter, port); // Same params, different random number
124 char buffer[Uid::stringSize];
125 id.getStringUid(buffer, sizeof(buffer));
126 uids.insert(std::string(buffer));
127 }
128
129 // All generated UIDs should be unique
130 EXPECT_EQ(uids.size(), static_cast<size_t>(num_uids));
131}
132
133// =============================================================================
134// End of tests for Uid
135// =============================================================================
T & insert(const T &item)
Insert a new item by copy.
Definition htlist.H:1502
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
Unique identifier for distributed systems.
Definition uid.H:95
static constexpr int stringSize
Required buffer size for string representation.
Definition uid.H:109
#define TEST(name)
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
uint32_t IPv4_Address
Type alias for IPv4 addresses stored as 32-bit integers.
Definition ahDefs.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
Unique identifier generation for distributed systems.