@@ -22,24 +22,35 @@ func TestUint64ToUint32(t *testing.T) {
22
22
name string
23
23
input uint64
24
24
want uint32
25
- wantErr bool
25
+ errType error
26
26
}{
27
- {"Zero" , 0 , 0 , false },
28
- {"Max uint32" , 4294967295 , 4294967295 , false },
29
- {"Overflow" , 4294967296 , 0 , true },
30
- {"Large overflow" , 18446744073709551615 , 0 , true },
31
- {"Mid-range value" , 2147483648 , 2147483648 , false },
27
+ {"Zero" , 0 , 0 , nil },
28
+ {"Max uint32" , 4294967295 , 4294967295 , nil },
29
+ {"Overflow" , 4294967296 , 0 , & OutOfRangeError {} },
30
+ {"Large overflow" , 18446744073709551615 , 0 , & OutOfRangeError {} },
31
+ {"Mid-range value" , 2147483648 , 2147483648 , nil },
32
32
}
33
33
34
34
for _ , tt := range tests {
35
35
t .Run (tt .name , func (t * testing.T ) {
36
36
got , err := Uint64ToUint32 (tt .input )
37
- if (err != nil ) != tt .wantErr {
38
- t .Errorf ("Uint64ToUint32() error = %v, wantErr %v" , err , tt .wantErr )
39
- return
40
- }
41
- if got != tt .want {
42
- t .Errorf ("Uint64ToUint32() = %v, want %v" , got , tt .want )
37
+ if tt .errType != nil {
38
+ // Check if the error is of the expected type
39
+ if err == nil {
40
+ t .Errorf ("Expected error type %T, got no error" , tt .errType )
41
+ } else if _ , ok := err .(interface { Error () string }); ! ok {
42
+ t .Errorf ("Expected error type %T, got %T" , tt .errType , err )
43
+ } else if _ , ok := err .(* OutOfRangeError ); ! ok {
44
+ t .Errorf ("Expected error type *OutOfRangeError, got %T" , err )
45
+ }
46
+ } else {
47
+ if err != nil {
48
+ t .Errorf ("Uint64ToUint32() error = %v, want no error" , err )
49
+ return
50
+ }
51
+ if got != tt .want {
52
+ t .Errorf ("Uint64ToUint32() = %v, want %v" , got , tt .want )
53
+ }
43
54
}
44
55
})
45
56
}
0 commit comments