Skip to content

Commit 63a4256

Browse files
committed
pkg/math: add tests which show the API constants
consts.txtar shows the constant values exposed via the CUE std API, and Example_constants shows the same ones exposed via the Go API. Note that we're not particular about the precision used by the Go test; we mainly aim to see whether the values look roughly correct or not. This is in preparation for https://cuelang.org/cl/557322, which will fix one of the constants to not truncate to an integer. Note that Log2E and Log10E are truncated to the integers 1 and 0, a bug which will be fixed in the next commit. Signed-off-by: Daniel Martí <[email protected]> Change-Id: Ie737f7a7ff7a609cdb9fc74df64bddc2e423f00f Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/557321 Reviewed-by: Paul Jolly <[email protected]> Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 3a101ba commit 63a4256

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

pkg/math/math_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,46 @@
1515
package math_test
1616

1717
import (
18+
"fmt"
1819
"testing"
1920

2021
"cuelang.org/go/pkg/internal/builtintest"
22+
"cuelang.org/go/pkg/math"
2123
)
2224

2325
func TestBuiltin(t *testing.T) {
2426
builtintest.Run("math", t)
2527
}
28+
29+
func Example_constants() {
30+
show := func(name string, value any) {
31+
fmt.Printf("% 7s: %v\n", name, value)
32+
}
33+
34+
show("E", math.E)
35+
show("Pi", math.Pi)
36+
show("Phi", math.Phi)
37+
38+
show("Sqrt2", math.Sqrt2)
39+
show("SqrtE", math.SqrtE)
40+
show("SqrtPi", math.SqrtPi)
41+
show("SqrtPhi", math.SqrtPhi)
42+
43+
show("Ln2", math.Ln2)
44+
show("Log2E", math.Log2E)
45+
show("Ln10", math.Ln10)
46+
show("Log10E", math.Log10E)
47+
48+
// Output:
49+
// E: 2.718281828459045
50+
// Pi: 3.141592653589793
51+
// Phi: 1.618033988749895
52+
// Sqrt2: 1.4142135623730951
53+
// SqrtE: 1.6487212707001282
54+
// SqrtPi: 1.772453850905516
55+
// SqrtPhi: 1.272019649514069
56+
// Ln2: 0.6931471805599453
57+
// Log2E: 1
58+
// Ln10: 2.302585092994046
59+
// Log10E: 0
60+
}

pkg/math/testdata/consts.txtar

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
-- in.cue --
2+
import "math"
3+
4+
E: math.E
5+
Pi: math.Pi
6+
Phi: math.Phi
7+
8+
Sqrt2: math.Sqrt2
9+
SqrtE: math.SqrtE
10+
SqrtPi: math.SqrtPi
11+
SqrtPhi: math.SqrtPhi
12+
13+
Ln2: math.Ln2
14+
Log2E: math.Log2E
15+
Ln10: math.Ln10
16+
Log10E: math.Log10E
17+
-- out/math --
18+
E: 2.71828182845904523536028747135266249775724709369995957496696763
19+
Pi: 3.14159265358979323846264338327950288419716939937510582097494459
20+
Phi: 1.61803398874989484820458683436563811772030917980576286213544861
21+
Sqrt2: 1.41421356237309504880168872420969807856967187537694807317667974
22+
SqrtE: 1.64872127070012814684865078781416357165377610071014801157507931
23+
SqrtPi: 1.77245385090551602729816748334114518279754945612238712821380779
24+
SqrtPhi: 1.27201964951406896425242246173749149171560804184009624861664038
25+
Ln2: 0.693147180559945309417232121458176568075500134360255254120680009
26+
Log2E: 1.442695040888963407359924681001892137426645954152985934135449408
27+
Ln10: 2.3025850929940456840179914546843642076011014886287729760333278
28+
Log10E: 0.43429448190325182765112891891660508229439700580366656611445378
29+

pkg/math/testdata/gen.txtar

+4-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
-- in.cue --
44
import "math"
55

6-
t1: math.Pi
76
t2: math.Floor(math.Pi)
87
t3: math.Pi(3)
98
t6: math.Jacobi(1000, 2000)
@@ -26,15 +25,14 @@ t33: math.Dim(5, 7.2)
2625
-- out/math --
2726
Errors:
2827
t3: cannot call non-function math.Pi (type float):
29-
./in.cue:5:6
28+
./in.cue:4:6
3029
t6: error in call to math.Jacobi: big: invalid 2nd argument to Int.Jacobi: need odd integer but got 2000:
31-
./in.cue:6:6
30+
./in.cue:5:6
3231
t8: cannot use 2.0E+400 (type float) as float64 in argument 0 to math.Asin: value was rounded up:
33-
./in.cue:8:6
34-
./in.cue:8:16
32+
./in.cue:7:6
33+
./in.cue:7:16
3534

3635
Result:
37-
t1: 3.14159265358979323846264338327950288419716939937510582097494459
3836
t2: 3
3937
t3: _|_ // t3: cannot call non-function math.Pi (type float)
4038
t6: _|_ // t6: error in call to math.Jacobi: big: invalid 2nd argument to Int.Jacobi: need odd integer but got 2000

0 commit comments

Comments
 (0)