1
2
3
4
5 package field
6
7 import (
8 "testing"
9 "testing/quick"
10 )
11
12 func checkAliasingOneArg(f func(v, x *Element) *Element) func(v, x Element) bool {
13 return func(v, x Element) bool {
14 x1, v1 := x, x
15
16
17 if out := f(&v, &x); out != &v && isInBounds(out) {
18 return false
19 }
20
21
22 if out := f(&v1, &v1); out != &v1 || v1 != v {
23 return false
24 }
25
26
27 return x == x1
28 }
29 }
30
31 func checkAliasingTwoArgs(f func(v, x, y *Element) *Element) func(v, x, y Element) bool {
32 return func(v, x, y Element) bool {
33 x1, y1, v1 := x, y, Element{}
34
35
36 if out := f(&v, &x, &y); out != &v && isInBounds(out) {
37 return false
38 }
39
40
41 v1 = x
42 if out := f(&v1, &v1, &y); out != &v1 || v1 != v {
43 return false
44 }
45
46 v1 = y
47 if out := f(&v1, &x, &v1); out != &v1 || v1 != v {
48 return false
49 }
50
51
52 if out := f(&v, &x, &x); out != &v {
53 return false
54 }
55
56
57 v1 = x
58 if out := f(&v1, &v1, &x); out != &v1 || v1 != v {
59 return false
60 }
61
62 v1 = x
63 if out := f(&v1, &x, &v1); out != &v1 || v1 != v {
64 return false
65 }
66
67 v1 = x
68 if out := f(&v1, &v1, &v1); out != &v1 || v1 != v {
69 return false
70 }
71
72
73 return x == x1 && y == y1
74 }
75 }
76
77
78
79
80
81
82
83
84
85
86
87 func TestAliasing(t *testing.T) {
88 type target struct {
89 name string
90 oneArgF func(v, x *Element) *Element
91 twoArgsF func(v, x, y *Element) *Element
92 }
93 for _, tt := range []target{
94 {name: "Absolute", oneArgF: (*Element).Absolute},
95 {name: "Invert", oneArgF: (*Element).Invert},
96 {name: "Negate", oneArgF: (*Element).Negate},
97 {name: "Set", oneArgF: (*Element).Set},
98 {name: "Square", oneArgF: (*Element).Square},
99 {name: "Pow22523", oneArgF: (*Element).Pow22523},
100 {
101 name: "Mult32",
102 oneArgF: func(v, x *Element) *Element {
103 return v.Mult32(x, 0xffffffff)
104 },
105 },
106 {name: "Multiply", twoArgsF: (*Element).Multiply},
107 {name: "Add", twoArgsF: (*Element).Add},
108 {name: "Subtract", twoArgsF: (*Element).Subtract},
109 {
110 name: "SqrtRatio",
111 twoArgsF: func(v, x, y *Element) *Element {
112 r, _ := v.SqrtRatio(x, y)
113 return r
114 },
115 },
116 {
117 name: "Select0",
118 twoArgsF: func(v, x, y *Element) *Element {
119 return v.Select(x, y, 0)
120 },
121 },
122 {
123 name: "Select1",
124 twoArgsF: func(v, x, y *Element) *Element {
125 return v.Select(x, y, 1)
126 },
127 },
128 } {
129 var err error
130 switch {
131 case tt.oneArgF != nil:
132 err = quick.Check(checkAliasingOneArg(tt.oneArgF), quickCheckConfig(256))
133 case tt.twoArgsF != nil:
134 err = quick.Check(checkAliasingTwoArgs(tt.twoArgsF), quickCheckConfig(256))
135 }
136 if err != nil {
137 t.Errorf("%v: %v", tt.name, err)
138 }
139 }
140 }
141
View as plain text