@@ -49,3 +49,51 @@ test "SegmentInfo.contains" {
49
49
try std .testing .expect (c .contains (b ));
50
50
try std .testing .expect (c .contains (c ));
51
51
}
52
+
53
+ pub const Item = packed struct (u64 ) {
54
+ id : u32 ,
55
+ hash : u32 ,
56
+
57
+ pub fn cmp (_ : void , a : Item , b : Item ) bool {
58
+ const xa : u64 = @bitCast (a );
59
+ const xb : u64 = @bitCast (b );
60
+ return xa < xb ;
61
+ }
62
+
63
+ pub fn cmpByHash (_ : void , a : Item , b : Item ) bool {
64
+ return a .hash < b .hash ;
65
+ }
66
+ };
67
+
68
+ test "Item binary" {
69
+ try std .testing .expectEqual (8 , @sizeOf (Item ));
70
+ try std .testing .expectEqual (64 , @bitSizeOf (Item ));
71
+ try std .testing .expectEqual (0 , @bitOffsetOf (Item , "id" ));
72
+ try std .testing .expectEqual (32 , @bitOffsetOf (Item , "hash" ));
73
+
74
+ const item1 = Item { .hash = 1 , .id = 2 };
75
+ const item2 = Item { .hash = 2 , .id = 1 };
76
+
77
+ const x1 : u64 = @bitCast (item1 );
78
+ const x2 : u64 = @bitCast (item2 );
79
+
80
+ try std .testing .expectEqual (0x0000000100000002 , x1 );
81
+ try std .testing .expectEqual (0x0000000200000001 , x2 );
82
+ }
83
+
84
+ test "Item array sort" {
85
+ var items = try std .testing .allocator .alloc (Item , 3 );
86
+ defer std .testing .allocator .free (items );
87
+
88
+ items [0 ] = Item { .hash = 2 , .id = 200 };
89
+ items [1 ] = Item { .hash = 2 , .id = 100 };
90
+ items [2 ] = Item { .hash = 1 , .id = 300 };
91
+
92
+ std .sort .insertion (Item , items , {}, Item .cmp );
93
+
94
+ try std .testing .expectEqualSlices (Item , &[_ ]Item {
95
+ Item { .hash = 1 , .id = 300 },
96
+ Item { .hash = 2 , .id = 100 },
97
+ Item { .hash = 2 , .id = 200 },
98
+ }, items );
99
+ }
0 commit comments