Example #1
0
func Float64ContextSpec(c gospec.Context) {
	c.Specify("Float64 context works properly.", func() {
		context := polish.MakeContext()
		polish.AddFloat64MathContext(context)
		v1 := math.E * math.Pi * math.Exp(1.23456-math.Log10(77))
		res, err := context.Eval("* e * pi ^ e - 1.23456 log10 77.0")
		c.Assume(len(res), Equals, 1)
		c.Assume(err, Equals, nil)
		c.Expect(res[0].Float(), IsWithin(1e-9), v1)
		res, err = context.Eval("< e pi")
		c.Assume(len(res), Equals, 1)
		c.Assume(err, Equals, nil)
		c.Expect(res[0].Bool(), Equals, true)
	})
}
Example #2
0
func ParseOrderSpec(c gospec.Context) {
	c.Specify("Parse order can be changed.", func() {
		context := polish.MakeContext()
		polish.AddFloat64MathContext(context)
		context.SetParseOrder(polish.Float) // Only parse as floats
		res, err := context.Eval("    +           1                      3")
		c.Assume(len(res), Equals, 1)
		c.Assume(err, Equals, nil)
		c.Expect(res[0].Float(), Equals, 4.0)

		context = polish.MakeContext()
		polish.AddIntMathContext(context)
		context.SetParseOrder(polish.Float) // Only parse as floats
		res, err = context.Eval("    +           1                      3")
		c.Assume(len(res), Equals, 0)
		c.Assume(err, Not(Equals), nil)
	})
}
Example #3
0
func Float64AndBooleanContextSpec(c gospec.Context) {
	c.Specify("Boolean context works properly with a float64 context.", func() {
		context := polish.MakeContext()
		polish.AddFloat64MathContext(context)
		polish.AddBooleanContext(context)
		res, err := context.Eval("&& < e pi >= log2 pi log2 e")
		c.Assume(len(res), Equals, 1)
		c.Assume(err, Equals, nil)
		c.Expect(res[0].Bool(), Equals, true)
		res, err = context.Eval("^^ < 3.0 4.0 < 4.0 3.0")
		c.Assume(len(res), Equals, 1)
		c.Assume(err, Equals, nil)
		c.Expect(res[0].Bool(), Equals, true)
		res, err = context.Eval("^^ < 3.0 4.0 < 3.0 4.0")
		c.Assume(len(res), Equals, 1)
		c.Assume(err, Equals, nil)
		c.Expect(res[0].Bool(), Equals, false)
		res, err = context.Eval("! ^^ < 3.0 4.0 < 3.0 4.0")
		c.Assume(len(res), Equals, 1)
		c.Assume(err, Equals, nil)
		c.Expect(res[0].Bool(), Equals, true)
	})
}