| 1 | package internal |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "go/importer" |
| 6 | "regexp" |
| 7 | "slices" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/go-quicktest/qt" |
| 12 | ) |
| 13 | |
| 14 | func TestPow(t *testing.T) { |
| 15 | tests := []struct { |
| 16 | n int |
| 17 | r bool |
| 18 | }{ |
| 19 | {0, false}, |
| 20 | {1, true}, |
| 21 | {2, true}, |
| 22 | {3, false}, |
| 23 | {4, true}, |
| 24 | {5, false}, |
| 25 | {8, true}, |
| 26 | } |
| 27 | |
| 28 | for _, tt := range tests { |
| 29 | t.Run(fmt.Sprintf("%d", tt.n), func(t *testing.T) { |
| 30 | if want, got := tt.r, IsPow(tt.n); want != got { |
| 31 | t.Errorf("unexpected result for n %d; want: %v, got: %v", tt.n, want, got) |
| 32 | } |
| 33 | }) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestIntegerConstraint(t *testing.T) { |
| 38 | rgx := regexp.MustCompile(`^(u)?int([0-9]*|ptr)?$`) |
| 39 | |
| 40 | pkg, err := importer.Default().Import("reflect") |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | |
| 45 | for _, name := range pkg.Scope().Names() { |
| 46 | name = strings.ToLower(name) |
| 47 | if !rgx.MatchString(name) { |
| 48 | continue |
| 49 | } |
| 50 | |
| 51 | if !slices.Contains(integers, name) { |
| 52 | t.Errorf("Go type %s is not in the list of known integer types", name) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func TestBetween(t *testing.T) { |
| 58 | tests := []struct { |
| 59 | val, a, b, r int |
| 60 | }{ |
| 61 | {0, 1, 2, 1}, |
| 62 | {1, 1, 2, 1}, |
| 63 | {2, 1, 2, 2}, |
| 64 | {3, 1, 2, 2}, |
| 65 | {4, 10, 5, 5}, |
| 66 | {11, 10, 5, 10}, |
| 67 | } |
| 68 | |
| 69 | for _, tt := range tests { |
| 70 | t.Run(fmt.Sprintf("%d between %d and %d", tt.val, tt.a, tt.b), func(t *testing.T) { |
| 71 | qt.Assert(t, qt.Equals(Between(tt.val, tt.a, tt.b), tt.r)) |
| 72 | }) |
| 73 | } |
| 74 | } |