aboutsummaryrefslogtreecommitdiff
path: root/tests/core/encoding
diff options
context:
space:
mode:
authorFeoramund <161657516+Feoramund@users.noreply.github.com>2024-06-22 15:49:48 -0400
committerFeoramund <161657516+Feoramund@users.noreply.github.com>2024-06-22 18:21:32 -0400
commitd559feb7011fd1c75ae66c9aed38536dcd41a2cf (patch)
treec3c80113e60d22c2c5b34c6b673220cdc2982ea8 /tests/core/encoding
parentf6344577d3cf89bf1be6675bf34e8dd03752c9e1 (diff)
Add `uuid` test for timestamps
Diffstat (limited to 'tests/core/encoding')
-rw-r--r--tests/core/encoding/uuid/test_core_uuid.odin48
1 files changed, 48 insertions, 0 deletions
diff --git a/tests/core/encoding/uuid/test_core_uuid.odin b/tests/core/encoding/uuid/test_core_uuid.odin
index 9f2d9c312..895de3863 100644
--- a/tests/core/encoding/uuid/test_core_uuid.odin
+++ b/tests/core/encoding/uuid/test_core_uuid.odin
@@ -34,6 +34,54 @@ test_version_and_variant :: proc(t: ^testing.T) {
}
@(test)
+test_timestamps :: proc(t: ^testing.T) {
+ // This test makes sure that timestamps are recoverable and have not been
+ // overwritten by neighboring bits, taking into account precision loss.
+ context.random_generator = crypto.random_generator()
+
+ N :: max(i64)
+
+ max_time := time.Time { N }
+
+ mac: [6]byte
+ v1 := uuid.generate_v1(0, mac, max_time)
+ v6 := uuid.generate_v6(0, mac, max_time)
+ v7 := uuid.generate_v7(max_time)
+ // The counter version keeps its time in the same place as the basic version,
+ // this is just for the sake of completeness.
+ v7_counter := uuid.generate_v7(0, max_time)
+
+ v1_time := uuid.time_v1(v1)
+ v6_time := uuid.time_v6(v6)
+ v7_time := uuid.time_v7(v7)
+ v7_counter_time := uuid.time_v7(v7_counter)
+
+ // I hope the compiler doesn't ever optimize this out.
+ max_time_hns_resolution := time.Time { N / 100 * 100 }
+ max_time_ms_resolution := time.Time { N / 1e6 * 1e6 }
+
+ testing.expectf(t,
+ time.diff(max_time_hns_resolution, v1_time) == 0,
+ "v1 UUID timestamp is invalid, expected %x, got %x",
+ max_time_hns_resolution, v1_time)
+
+ testing.expectf(t,
+ time.diff(max_time_hns_resolution, v6_time) == 0,
+ "v6 UUID timestamp is invalid, expected %x, got %x",
+ max_time_hns_resolution, v6_time)
+
+ testing.expectf(t,
+ time.diff(max_time_ms_resolution, v7_time) == 0,
+ "v7 UUID timestamp is invalid, expected %x, got %x",
+ max_time_ms_resolution, v7_time)
+
+ testing.expectf(t,
+ time.diff(max_time_ms_resolution, v7_counter_time) == 0,
+ "v7 UUID (with counter) timestamp is invalid, expected %x, got %x",
+ max_time_ms_resolution, v7_counter_time)
+}
+
+@(test)
test_legacy_namespaced_uuids :: proc(t: ^testing.T) {
TEST_NAME :: "0123456789ABCDEF0123456789ABCDEF"