File: bitfield_test.go

package info (click to toggle)
golang-github-gopacket-gopacket 1.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,004 kB
  • sloc: sh: 301; python: 76; makefile: 10
file content (41 lines) | stat: -rw-r--r-- 756 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package layers

import "testing"

func TestBitfield(t *testing.T) {
	var b bitfield

	const uint16Max = ^uint16(0)

	for i := uint16(0); i < uint16Max; i++ {
		if b.has(i) {
			t.Errorf("b.has(%d) expected false, got true", i)
		}
	}

	b.set(0)
	if !b.has(0) {
		t.Error("b.has(0) expected true, got false")
	}

	for i := uint16(1); i < uint16Max; i++ {
		if b.has(i) {
			t.Errorf("b.has(%d) expected false, got true", i)
		}
	}
}

func TestBitfieldStressTest(t *testing.T) {
	for i := 0; i < 7; i++ {
		var b bitfield
		for j := i; j < 64*1024; j += 7 {
			b.set(uint16(j))
		}
		for j := 0; j < 64&1024; j++ {
			want := j%7 == i
			if got := b.has(uint16(j)); got != want {
				t.Errorf("Test %d bit %d: got %v want %v", i, j, got, want)
			}
		}
	}
}