Joe Tsai | d55639e | 2018-08-09 13:35:22 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package set |
| 6 | |
| 7 | import ( |
| 8 | "math/rand" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | const maxLimit = 1024 |
| 13 | |
| 14 | var toSet, toClear [maxLimit]bool |
| 15 | |
| 16 | func init() { |
| 17 | r := rand.New(rand.NewSource(0)) |
| 18 | for i := 0; i < maxLimit; i++ { |
| 19 | toSet[i] = r.Intn(2) == 0 |
| 20 | toClear[i] = r.Intn(2) == 0 |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func TestInts(t *testing.T) { |
Joe Tsai | ac50359 | 2019-04-03 15:16:47 -0700 | [diff] [blame] | 25 | ns := new(Ints) |
| 26 | |
| 27 | // Check that set starts empty. |
| 28 | wantLen := 0 |
| 29 | if ns.Len() != wantLen { |
| 30 | t.Errorf("init: Len() = %d, want %d", ns.Len(), wantLen) |
| 31 | } |
| 32 | for i := 0; i < maxLimit; i++ { |
| 33 | if ns.Has(uint64(i)) { |
| 34 | t.Errorf("init: Has(%d) = true, want false", i) |
| 35 | } |
Joe Tsai | d55639e | 2018-08-09 13:35:22 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Joe Tsai | ac50359 | 2019-04-03 15:16:47 -0700 | [diff] [blame] | 38 | // Set some numbers. |
| 39 | for i, b := range toSet[:maxLimit] { |
| 40 | if b { |
| 41 | ns.Set(uint64(i)) |
| 42 | wantLen++ |
| 43 | } |
Joe Tsai | d55639e | 2018-08-09 13:35:22 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Joe Tsai | ac50359 | 2019-04-03 15:16:47 -0700 | [diff] [blame] | 46 | // Check that integers were set. |
| 47 | if ns.Len() != wantLen { |
| 48 | t.Errorf("after Set: Len() = %d, want %d", ns.Len(), wantLen) |
| 49 | } |
| 50 | for i := 0; i < maxLimit; i++ { |
| 51 | if got := ns.Has(uint64(i)); got != toSet[i] { |
| 52 | t.Errorf("after Set: Has(%d) = %v, want %v", i, got, !got) |
| 53 | } |
| 54 | } |
Joe Tsai | d55639e | 2018-08-09 13:35:22 -0700 | [diff] [blame] | 55 | |
Joe Tsai | ac50359 | 2019-04-03 15:16:47 -0700 | [diff] [blame] | 56 | // Clear some numbers. |
| 57 | for i, b := range toClear[:maxLimit] { |
| 58 | if b { |
| 59 | ns.Clear(uint64(i)) |
| 60 | if toSet[i] { |
| 61 | wantLen-- |
Joe Tsai | d55639e | 2018-08-09 13:35:22 -0700 | [diff] [blame] | 62 | } |
Joe Tsai | ac50359 | 2019-04-03 15:16:47 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
Joe Tsai | d55639e | 2018-08-09 13:35:22 -0700 | [diff] [blame] | 65 | |
Joe Tsai | ac50359 | 2019-04-03 15:16:47 -0700 | [diff] [blame] | 66 | // Check that integers were cleared. |
| 67 | if ns.Len() != wantLen { |
| 68 | t.Errorf("after Clear: Len() = %d, want %d", ns.Len(), wantLen) |
| 69 | } |
| 70 | for i := 0; i < maxLimit; i++ { |
| 71 | if got := ns.Has(uint64(i)); got != toSet[i] && !toClear[i] { |
| 72 | t.Errorf("after Clear: Has(%d) = %v, want %v", i, got, !got) |
| 73 | } |
Joe Tsai | d55639e | 2018-08-09 13:35:22 -0700 | [diff] [blame] | 74 | } |
| 75 | } |