Skip to content

Commit

Permalink
tests: fix tests failing on 32-bit arch
Browse files Browse the repository at this point in the history
E.g. GOARCH=386 go test ./...

- Some tests were failing because of Int overflow;
- Test ExampleRego_custom_function_caching was failing
  because it was dependent of rand default seed on 64bits arch.

Signed-off-by: Olivier Lemasle <o.lemasle@gmail.com>
  • Loading branch information
olivierlemasle authored and tsandall committed Oct 28, 2020
1 parent d8947db commit c9e1d02
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
14 changes: 7 additions & 7 deletions rego/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"context"
"encoding/json"
"fmt"
"math/rand"
"os"
"strings"

Expand Down Expand Up @@ -785,24 +784,25 @@ func ExampleRego_custom_function_caching() {

type builtinCacheKey string

source := rand.NewSource(0)
i := 0

r := rego.New(
// An example query that uses a custom function.
rego.Query(`x = myrandom("foo"); y = myrandom("foo")`),
rego.Query(`x = mycounter("foo"); y = mycounter("foo")`),

// A custom function that uses caching.
rego.FunctionDyn(
&rego.Function{
Name: "myrandom",
Name: "mycounter",
Memoize: true,
Decl: types.NewFunction(
types.Args(types.S), // one string input
types.N, // one number output
),
},
func(_ topdown.BuiltinContext, args []*ast.Term) (*ast.Term, error) {
return ast.IntNumberTerm(int(source.Int63())), nil
i++
return ast.IntNumberTerm(i), nil
},
),
)
Expand All @@ -817,8 +817,8 @@ func ExampleRego_custom_function_caching() {

// Output:
//
// x: 8717895732742165505
// y: 8717895732742165505
// x: 1
// y: 1
}

func ExampleRego_custom_function_global() {
Expand Down
8 changes: 4 additions & 4 deletions topdown/parse_bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestNumBytes(t *testing.T) {
t.Run("SuccessfulParse", func(t *testing.T) {
tests := []struct {
note, rule string
expected int
expected int64
}{
{"zero", `0`, 0},
{"raw number", `12345`, 12345},
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestNumBytes(t *testing.T) {
})
}

func runNumBytesParseTest(t *testing.T, note, rule string, expected int) {
func runNumBytesParseTest(t *testing.T, note, rule string, expected int64) {
t.Helper()

num := parseIntFromString(t, rule)
Expand Down Expand Up @@ -139,7 +139,7 @@ func runExpectedFailureTest(t *testing.T, s string, expectedErr error) {
}
}

func parseIntFromString(t *testing.T, s string) int {
func parseIntFromString(t *testing.T, s string) int64 {
sVal := ast.StringTerm(s).Value
val, err := builtinNumBytes(sVal)

Expand All @@ -148,7 +148,7 @@ func parseIntFromString(t *testing.T, s string) int {
}

i := val.(ast.Number)
num, ok := i.Int()
num, ok := i.Int64()
if !ok {
t.Fatalf("numbytes err: could not parse value %s into int", val.String())
}
Expand Down
4 changes: 2 additions & 2 deletions util/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ func TestDFSPath(t *testing.T) {
}

t3 := newTestTraversal(g)
p3 := DFSPath(t3, t3.Equals, 1, 0xdeadbeef)
p3 := DFSPath(t3, t3.Equals, 1, 0xadbeef)
if len(p3) != 0 {
t.Errorf("Expected DFS(1,0xdeadbeef to be empty but got: %v", p3)
t.Errorf("Expected DFS(1,0xadbeef to be empty but got: %v", p3)
}

}

0 comments on commit c9e1d02

Please sign in to comment.