123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- /*
- * ECAL
- *
- * Copyright 2020 Matthias Ladkau. All rights reserved.
- *
- * This Source Code Form is subject to the terms of the MIT
- * License, If a copy of the MIT License was not distributed with this
- * file, You can obtain one at https://opensource.org/licenses/MIT.
- */
- package interpreter
- import (
- "testing"
- )
- func TestSimpleArithmetics(t *testing.T) {
- res, err := UnitTestEvalAndAST(
- `1 + 2`, nil,
- `
- plus
- number: 1
- number: 2
- `[1:])
- if err != nil || res != 3. {
- t.Error("Unexpected result: ", res, err)
- return
- }
- res, err = UnitTestEvalAndAST(
- `1 + 2 + 3`, nil,
- `
- plus
- plus
- number: 1
- number: 2
- number: 3
- `[1:])
- if err != nil || res != 6. {
- t.Error("Unexpected result: ", res, err)
- return
- }
- res, err = UnitTestEvalAndAST(
- `1 - 2 + 3`, nil,
- `
- plus
- minus
- number: 1
- number: 2
- number: 3
- `[1:])
- if err != nil || res != 2. {
- t.Error("Unexpected result: ", res, err)
- return
- }
- res, err = UnitTestEvalAndAST(
- `1 - 2`, nil,
- `
- minus
- number: 1
- number: 2
- `[1:])
- if err != nil || res != -1. {
- t.Error("Unexpected result: ", res, err)
- return
- }
- }
- func TestSimpleArithmetics2(t *testing.T) {
- res, err := UnitTestEvalAndAST(
- `-5.2 - 2.2`, nil,
- `
- minus
- minus
- number: 5.2
- number: 2.2
- `[1:])
- if err != nil || res != -7.4 {
- t.Error("Unexpected result: ", res, err)
- return
- }
- res, err = UnitTestEvalAndAST(
- `+ 5.2 * 2`, nil,
- `
- times
- plus
- number: 5.2
- number: 2
- `[1:])
- if err != nil || res != 10.4 {
- t.Error("Unexpected result: ", res, err)
- return
- }
- res, err = UnitTestEvalAndAST(
- `5.2 / 2`, nil,
- `
- div
- number: 5.2
- number: 2
- `[1:])
- if err != nil || res != 2.6 {
- t.Error("Unexpected result: ", res, err)
- return
- }
- res, err = UnitTestEvalAndAST(
- `5.2 // 2`, nil,
- `
- divint
- number: 5.2
- number: 2
- `[1:])
- if err != nil || res != 2. {
- t.Error("Unexpected result: ", res, err)
- return
- }
- res, err = UnitTestEvalAndAST(
- `5.2 % 2`, nil,
- `
- modint
- number: 5.2
- number: 2
- `[1:])
- if err != nil || res != 1. {
- t.Error("Unexpected result: ", res, err)
- return
- }
- }
|