File: math_test.go

package info (click to toggle)
golang-github-cilium-ebpf 0.17.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,684 kB
  • sloc: ansic: 1,259; makefile: 127; python: 113; awk: 29; sh: 24
file content (74 lines) | stat: -rw-r--r-- 1,319 bytes parent folder | download
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
package internal

import (
	"fmt"
	"go/importer"
	"regexp"
	"slices"
	"strings"
	"testing"

	"github.com/go-quicktest/qt"
)

func TestPow(t *testing.T) {
	tests := []struct {
		n int
		r bool
	}{
		{0, false},
		{1, true},
		{2, true},
		{3, false},
		{4, true},
		{5, false},
		{8, true},
	}

	for _, tt := range tests {
		t.Run(fmt.Sprintf("%d", tt.n), func(t *testing.T) {
			if want, got := tt.r, IsPow(tt.n); want != got {
				t.Errorf("unexpected result for n %d; want: %v, got: %v", tt.n, want, got)
			}
		})
	}
}

func TestIntegerConstraint(t *testing.T) {
	rgx := regexp.MustCompile(`^(u)?int([0-9]*|ptr)?$`)

	pkg, err := importer.Default().Import("reflect")
	if err != nil {
		t.Fatal(err)
	}

	for _, name := range pkg.Scope().Names() {
		name = strings.ToLower(name)
		if !rgx.MatchString(name) {
			continue
		}

		if !slices.Contains(integers, name) {
			t.Errorf("Go type %s is not in the list of known integer types", name)
		}
	}
}

func TestBetween(t *testing.T) {
	tests := []struct {
		val, a, b, r int
	}{
		{0, 1, 2, 1},
		{1, 1, 2, 1},
		{2, 1, 2, 2},
		{3, 1, 2, 2},
		{4, 10, 5, 5},
		{11, 10, 5, 10},
	}

	for _, tt := range tests {
		t.Run(fmt.Sprintf("%d between %d and %d", tt.val, tt.a, tt.b), func(t *testing.T) {
			qt.Assert(t, qt.Equals(Between(tt.val, tt.a, tt.b), tt.r))
		})
	}
}