@@ -80,20 +80,54 @@ func TestGenerateUniqueIDOrder(t *testing.T) {
8080 }
8181}
8282
83- // TestMapToUniqueUnorderedID verifies that the mapping preserves the ones count.
84- func TestMapToUniqueUnorderedID (t * testing.T ) {
83+ func TestMapToUnorderedUniqueInt (t * testing.T ) {
8584 defer leaktest .AfterTest (t )()
86- for i := 0 ; i < 30 ; i ++ {
87- // RandInput is [0][63 random bits].
88- randInput := uint64 (rand .Int63 ())
89- output := mapToUnorderedUniqueInt (randInput )
90-
91- inputOnesCount := bits .OnesCount64 (randInput )
92- outputOnesCount := bits .OnesCount64 (output )
93- require .Equalf (t , inputOnesCount , outputOnesCount , "input: %b, output: " +
94- "%b\n Expected: %d, got: %d" , randInput , output , inputOnesCount ,
95- outputOnesCount )
96- }
85+ defer log .Scope (t ).Close (t )
86+
87+ t .Run ("preserves number of one bits" , func (t * testing.T ) {
88+ for i := 0 ; i < 30 ; i ++ {
89+ // RandInput is [0][63 random bits].
90+ randInput := uint64 (rand .Int63 ())
91+ output := mapToUnorderedUniqueInt (randInput )
92+
93+ inputOnesCount := bits .OnesCount64 (randInput )
94+ outputOnesCount := bits .OnesCount64 (output )
95+ require .Equalf (t , inputOnesCount , outputOnesCount , "input: %b, output: " +
96+ "%b\n Expected: %d, got: %d" , randInput , output , inputOnesCount ,
97+ outputOnesCount )
98+ }
99+ })
100+
101+ t .Run ("correctly reverses timestamp" , func (t * testing.T ) {
102+ for name , tc := range map [string ]struct {
103+ input uint64
104+ expected uint64
105+ }{
106+ "asymmetrical timestamp" : {
107+ input : 0b0101100000000000000000000000000000000000000000000000000000000001 ,
108+ expected : 0b0000000000000000000000000000000000000000000001101000000000000001 ,
109+ },
110+ "symmetrical timestamp" : {
111+ input : 0b0100000000000000000000000000000000000000000000001000000000000101 ,
112+ expected : 0b0100000000000000000000000000000000000000000000001000000000000101 ,
113+ },
114+ "max timestamp" : {
115+ input : 0b0111111111111111111111111111111111111111111111111000000000000001 ,
116+ expected : 0b0111111111111111111111111111111111111111111111111000000000000001 ,
117+ },
118+ } {
119+ t .Run (name , func (t * testing.T ) {
120+ actual := mapToUnorderedUniqueInt (tc .input )
121+ require .Equalf (t ,
122+ tc .expected , actual ,
123+ "actual unordered unique int does not match expected:\n " +
124+ "%064b (expected)\n " +
125+ "%064b (actual)" ,
126+ tc .expected , actual ,
127+ )
128+ })
129+ }
130+ })
97131}
98132
99133// TestSerialNormalizationWithUniqueUnorderedID makes sure that serial
0 commit comments