xref: /aosp_15_r20/external/golang-protobuf/internal/set/ints_test.go (revision 1c12ee1efe575feb122dbf939ff15148a3b3e8f2)
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
5package set
6
7import (
8	"math/rand"
9	"testing"
10)
11
12const maxLimit = 1024
13
14var toSet, toClear [maxLimit]bool
15
16func 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
24func TestInts(t *testing.T) {
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		}
36	}
37
38	// Set some numbers.
39	for i, b := range toSet[:maxLimit] {
40		if b {
41			ns.Set(uint64(i))
42			wantLen++
43		}
44	}
45
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	}
55
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--
62			}
63		}
64	}
65
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		}
74	}
75}
76