|
2 | 2 |
|
3 | 3 | package dirent |
4 | 4 |
|
5 | | -import ( |
6 | | - "encoding/binary" |
7 | | - "fmt" |
8 | | - "math" |
9 | | - "math/rand" |
10 | | - "testing" |
11 | | -) |
12 | | - |
13 | | -func TestReadInt(t *testing.T) { |
14 | | - for _, sz := range []int{1, 2, 4, 8} { |
15 | | - rr := rand.New(rand.NewSource(1)) |
16 | | - t.Run(fmt.Sprintf("%d", sz), func(t *testing.T) { |
17 | | - var fn func() uint64 |
18 | | - switch sz { |
19 | | - case 1: |
20 | | - fn = func() uint64 { return uint64(rr.Int63n(256)) } |
21 | | - case 2: |
22 | | - fn = func() uint64 { return uint64(rr.Int63n(math.MaxUint16)) } |
23 | | - case 4: |
24 | | - fn = func() uint64 { return uint64(rr.Int63n(math.MaxUint32)) } |
25 | | - case 8: |
26 | | - fn = func() uint64 { return uint64(rr.Uint64()) } |
27 | | - default: |
28 | | - t.Fatal("invalid size:", sz) |
29 | | - } |
30 | | - buf := make([]byte, 8) |
31 | | - fails := 0 |
32 | | - for i := 0; i < 100; i++ { |
33 | | - want := fn() |
34 | | - binary.NativeEndian.PutUint64(buf[:], want) |
35 | | - got, ok := readInt(buf, 0, uintptr(sz)) |
36 | | - if got != want || !ok { |
37 | | - fails++ |
38 | | - t.Errorf("readInt(%q, 0, %d) = %d, %t; want: %d, %t", buf, sz, got, ok, want, true) |
39 | | - } |
40 | | - if fails >= 10 { |
41 | | - t.Fatal("too many errors:", fails) |
42 | | - } |
43 | | - } |
44 | | - }) |
45 | | - } |
46 | | - if i, ok := readInt(nil, 1, 1); i != 0 || ok { |
47 | | - t.Errorf("readInt(nil, 1, 1) = %d, %t; want: %d, %t", i, ok, 0, false) |
48 | | - } |
49 | | -} |
| 5 | +import "testing" |
50 | 6 |
|
51 | 7 | func TestReadIntSize(t *testing.T) { |
52 | 8 | if i, ok := readInt(nil, 1, 1); i != 0 || ok { |
|
0 commit comments