rt_arithmetic_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package interpreter
  11. import (
  12. "testing"
  13. )
  14. func TestSimpleArithmetics(t *testing.T) {
  15. res, err := UnitTestEvalAndAST(
  16. `1 + 2`, nil,
  17. `
  18. plus
  19. number: 1
  20. number: 2
  21. `[1:])
  22. if err != nil || res != 3. {
  23. t.Error("Unexpected result: ", res, err)
  24. return
  25. }
  26. res, err = UnitTestEvalAndAST(
  27. `1 + 2 + 3`, nil,
  28. `
  29. plus
  30. plus
  31. number: 1
  32. number: 2
  33. number: 3
  34. `[1:])
  35. if err != nil || res != 6. {
  36. t.Error("Unexpected result: ", res, err)
  37. return
  38. }
  39. res, err = UnitTestEvalAndAST(
  40. `1 - 2 + 3`, nil,
  41. `
  42. plus
  43. minus
  44. number: 1
  45. number: 2
  46. number: 3
  47. `[1:])
  48. if err != nil || res != 2. {
  49. t.Error("Unexpected result: ", res, err)
  50. return
  51. }
  52. res, err = UnitTestEvalAndAST(
  53. `1 - 2`, nil,
  54. `
  55. minus
  56. number: 1
  57. number: 2
  58. `[1:])
  59. if err != nil || res != -1. {
  60. t.Error("Unexpected result: ", res, err)
  61. return
  62. }
  63. }
  64. func TestSimpleArithmetics2(t *testing.T) {
  65. res, err := UnitTestEvalAndAST(
  66. `-5.2 - 2.2`, nil,
  67. `
  68. minus
  69. minus
  70. number: 5.2
  71. number: 2.2
  72. `[1:])
  73. if err != nil || res != -7.4 {
  74. t.Error("Unexpected result: ", res, err)
  75. return
  76. }
  77. res, err = UnitTestEvalAndAST(
  78. `+ 5.2 * 2`, nil,
  79. `
  80. times
  81. plus
  82. number: 5.2
  83. number: 2
  84. `[1:])
  85. if err != nil || res != 10.4 {
  86. t.Error("Unexpected result: ", res, err)
  87. return
  88. }
  89. res, err = UnitTestEvalAndAST(
  90. `5.2 / 2`, nil,
  91. `
  92. div
  93. number: 5.2
  94. number: 2
  95. `[1:])
  96. if err != nil || res != 2.6 {
  97. t.Error("Unexpected result: ", res, err)
  98. return
  99. }
  100. res, err = UnitTestEvalAndAST(
  101. `5.2 // 2`, nil,
  102. `
  103. divint
  104. number: 5.2
  105. number: 2
  106. `[1:])
  107. if err != nil || res != 2. {
  108. t.Error("Unexpected result: ", res, err)
  109. return
  110. }
  111. res, err = UnitTestEvalAndAST(
  112. `5.2 % 2`, nil,
  113. `
  114. modint
  115. number: 5.2
  116. number: 2
  117. `[1:])
  118. if err != nil || res != 1. {
  119. t.Error("Unexpected result: ", res, err)
  120. return
  121. }
  122. }