aboutsummaryrefslogtreecommitdiff
path: root/bitwise_test.go
blob: 18b8265b61187a1309272078f30411e38c93516f (plain) (blame)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>

package main

import (
	"testing"
)

func TestValidateBitZeros(t *testing.T) {
	tests := []struct {
		name     string
		bs       []byte
		n        uint
		expected bool
	}{
		{
			name:     "First 8 bits are zeros",
			bs:       []byte{0x00, 0x01},
			n:        8,
			expected: true,
		},
		{
			name:     "First 8 bits are not all zeros",
			bs:       []byte{0x01, 0x00},
			n:        8,
			expected: false,
		},
		{
			name:     "First 16 bits are zeros",
			bs:       []byte{0x00, 0x00, 0x01},
			n:        16,
			expected: true,
		},
		{
			name:     "First 16 bits are not all zeros",
			bs:       []byte{0x01, 0x00, 0x00},
			n:        16,
			expected: false,
		},
		{
			name:     "First 9 bits are zeros",
			bs:       []byte{0x00, 0x01},
			n:        9,
			expected: true,
		},
		{
			name:     "First 9 bits are not all zeros",
			bs:       []byte{0x01, 0x01},
			n:        9,
			expected: false,
		},
		{
			name:     "First 10 bits are zeros",
			bs:       []byte{0x00, 0x20},
			n:        10,
			expected: true,
		},
		{
			name:     "First 10 bits are not all zeros",
			bs:       []byte{0x00, 0x40},
			n:        10,
			expected: false,
		},
		{
			name:     "First 24 bits are zeros",
			bs:       []byte{0x00, 0x00, 0x00, 0x01},
			n:        24,
			expected: true,
		},
		{
			name:     "First 24 bits are not all zeros",
			bs:       []byte{0x00, 0x01, 0x00, 0x00},
			n:        24,
			expected: false,
		},
		{
			name:     "Checking zero bits",
			bs:       []byte{0xFF, 0xFF},
			n:        0,
			expected: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := validateBitZeros(tt.bs, tt.n)
			if got != tt.expected {
				t.Errorf("validateBitZeros(%v, %v) = %v; want %v", tt.bs, tt.n, got, tt.expected)
			}
		})
	}
}