rt_arithmetic_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. res, err = UnitTestEvalAndAST(
  64. `-5.2 - 2.2`, nil,
  65. `
  66. minus
  67. minus
  68. number: 5.2
  69. number: 2.2
  70. `[1:])
  71. if err != nil || res != -7.4 {
  72. t.Error("Unexpected result: ", res, err)
  73. return
  74. }
  75. res, err = UnitTestEvalAndAST(
  76. `+ 5.2 * 2`, nil,
  77. `
  78. times
  79. plus
  80. number: 5.2
  81. number: 2
  82. `[1:])
  83. if err != nil || res != 10.4 {
  84. t.Error("Unexpected result: ", res, err)
  85. return
  86. }
  87. res, err = UnitTestEvalAndAST(
  88. `5.2 / 2`, nil,
  89. `
  90. div
  91. number: 5.2
  92. number: 2
  93. `[1:])
  94. if err != nil || res != 2.6 {
  95. t.Error("Unexpected result: ", res, err)
  96. return
  97. }
  98. res, err = UnitTestEvalAndAST(
  99. `5.2 // 2`, nil,
  100. `
  101. divint
  102. number: 5.2
  103. number: 2
  104. `[1:])
  105. if err != nil || res != 2. {
  106. t.Error("Unexpected result: ", res, err)
  107. return
  108. }
  109. res, err = UnitTestEvalAndAST(
  110. `5.2 % 2`, nil,
  111. `
  112. modint
  113. number: 5.2
  114. number: 2
  115. `[1:])
  116. if err != nil || res != 1. {
  117. t.Error("Unexpected result: ", res, err)
  118. return
  119. }
  120. }