Skip to content

Commit

Permalink
pkg/math: add tests which show the API constants
Browse files Browse the repository at this point in the history
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í <mvdan@mvdan.cc>
Change-Id: Ie737f7a7ff7a609cdb9fc74df64bddc2e423f00f
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/557321
Reviewed-by: Paul Jolly <paul@myitcv.io>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
mvdan committed Aug 17, 2023
1 parent 3a101ba commit 63a4256
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
35 changes: 35 additions & 0 deletions pkg/math/math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,46 @@
package math_test

import (
"fmt"
"testing"

"cuelang.org/go/pkg/internal/builtintest"
"cuelang.org/go/pkg/math"
)

func TestBuiltin(t *testing.T) {
builtintest.Run("math", t)
}

func Example_constants() {
show := func(name string, value any) {
fmt.Printf("% 7s: %v\n", name, value)
}

show("E", math.E)
show("Pi", math.Pi)
show("Phi", math.Phi)

show("Sqrt2", math.Sqrt2)
show("SqrtE", math.SqrtE)
show("SqrtPi", math.SqrtPi)
show("SqrtPhi", math.SqrtPhi)

show("Ln2", math.Ln2)
show("Log2E", math.Log2E)
show("Ln10", math.Ln10)
show("Log10E", math.Log10E)

// Output:
// E: 2.718281828459045
// Pi: 3.141592653589793
// Phi: 1.618033988749895
// Sqrt2: 1.4142135623730951
// SqrtE: 1.6487212707001282
// SqrtPi: 1.772453850905516
// SqrtPhi: 1.272019649514069
// Ln2: 0.6931471805599453
// Log2E: 1
// Ln10: 2.302585092994046
// Log10E: 0
}
29 changes: 29 additions & 0 deletions pkg/math/testdata/consts.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- in.cue --
import "math"

E: math.E
Pi: math.Pi
Phi: math.Phi

Sqrt2: math.Sqrt2
SqrtE: math.SqrtE
SqrtPi: math.SqrtPi
SqrtPhi: math.SqrtPhi

Ln2: math.Ln2
Log2E: math.Log2E
Ln10: math.Ln10
Log10E: math.Log10E
-- out/math --
E: 2.71828182845904523536028747135266249775724709369995957496696763
Pi: 3.14159265358979323846264338327950288419716939937510582097494459
Phi: 1.61803398874989484820458683436563811772030917980576286213544861
Sqrt2: 1.41421356237309504880168872420969807856967187537694807317667974
SqrtE: 1.64872127070012814684865078781416357165377610071014801157507931
SqrtPi: 1.77245385090551602729816748334114518279754945612238712821380779
SqrtPhi: 1.27201964951406896425242246173749149171560804184009624861664038
Ln2: 0.693147180559945309417232121458176568075500134360255254120680009
Log2E: 1.442695040888963407359924681001892137426645954152985934135449408
Ln10: 2.3025850929940456840179914546843642076011014886287729760333278
Log10E: 0.43429448190325182765112891891660508229439700580366656611445378

10 changes: 4 additions & 6 deletions pkg/math/testdata/gen.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
-- in.cue --
import "math"

t1: math.Pi
t2: math.Floor(math.Pi)
t3: math.Pi(3)
t6: math.Jacobi(1000, 2000)
Expand All @@ -26,15 +25,14 @@ t33: math.Dim(5, 7.2)
-- out/math --
Errors:
t3: cannot call non-function math.Pi (type float):
./in.cue:5:6
./in.cue:4:6
t6: error in call to math.Jacobi: big: invalid 2nd argument to Int.Jacobi: need odd integer but got 2000:
./in.cue:6:6
./in.cue:5:6
t8: cannot use 2.0E+400 (type float) as float64 in argument 0 to math.Asin: value was rounded up:
./in.cue:8:6
./in.cue:8:16
./in.cue:7:6
./in.cue:7:16

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

0 comments on commit 63a4256

Please sign in to comment.