test-base.joy 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. (* test-base.joy -- tests for base.joy
  2. Copyright © 2016 Eric Bavier <bavier@member.fsf.org>
  3. Joy is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free
  5. Software Foundation; either version 3 of the License, or (at your
  6. option) any later version.
  7. Joy is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  9. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  10. License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Joy. If not, see <http://www.gnu.org/licenses/>.
  13. *)
  14. "base" include
  15. "inicheck" include
  16. DEFINE
  17. test-swons == "swons" [[3] 2 swons car] satisfies [2 =] ? ;
  18. test-unswons == "unswons" [[2 3] unswons] satisfies [2 =] ? ;
  19. test-car == "car" [[1 2] car] satisfies [1 =] ? ;
  20. test-cdr == "cdr" [[1 2] cdr car] satisfies [2 =] ? ;
  21. test-cadr == "cadr" [[1 2] cadr] satisfies [2 =] ? ;
  22. test-first ==
  23. "first" [[1 2 3] first] satisfies [1 =] ? ;
  24. test-second ==
  25. "second" [[1 2 3] second] satisfies [2 =] ? ;
  26. test-third ==
  27. "third" [[1 2 3] third] satisfies [3 =] ? ;
  28. test-booleans ==
  29. "true" [true] satisfies [1 0 choice 1 =] ?
  30. "false" [false] satisfies [1 0 choice 0 =] ? ;
  31. test-leaf ==
  32. "numeric leaf" [2 leaf] satisfies [true =] ?
  33. "char leaf" ['b leaf] satisfies [true =] ?
  34. "string leaf" ["foo" leaf] satisfies [false =] ?
  35. "list leaf" [[1 2] leaf] satisfies [false =] ? ;
  36. test-null ==
  37. "numeric null(0)" [0 null] satisfies [true =] ?
  38. "numeric non-null" [1 null] satisfies [false =] ?
  39. "null list" [[] null] satisfies [true =] ?
  40. "non-null list" [[1] null] satisfies [false =] ? ;
  41. test-nulld ==
  42. "numeric nulld" [0 1 nulld] satisfies [pop true =] ?
  43. "numeric non-nulld" [1 0 nulld] satisfies [pop false =] ?
  44. "list nulld" [[] [1] nulld] satisfies [pop true =] ?
  45. "list non-nulld" [[1] [] nulld] satisfies [pop false =] ? ;
  46. test-newstack ==
  47. "newstack" [newstack] satisfies [stack null] ? ;
  48. test-i ==
  49. "i id" [1 [] i] satisfies [1 =] ?
  50. "i atom" [[1] i] satisfies [1 =] ?
  51. "i pop" [1 2 [pop] i] satisfies [1 =] ?
  52. "i +" [1 2 [+] i] satisfies [3 =] ?
  53. "i2 id" [1 2 [] [] i2] satisfies [2 =] ?
  54. "i2 +" [1 2 [3 +] [2 +] i2] satisfies [=] ? ;
  55. test-dip ==
  56. "dip id" [1 2 [] dip] satisfies [pop 1 =] ?
  57. "dip atom" [2 [1] dip] satisfies [pop 1 =] ?
  58. "dip pop" [1 2 3 [pop] dip] satisfies [3 =] ?
  59. "dip pop 2" [1 2 3 [pop] dip] satisfies [pop 1 =] ? ;
  60. test-b ==
  61. "b +" [1 2 3 [+] [+] b] satisfies [6 =] ?
  62. "b" [4 [2 +] [3 -] b] satisfies [3 =] ? ;
  63. test-cleave ==
  64. "cleave" [2 [1 +] [4 +] cleave] satisfies [[3 =] [6 =] i2 and] ? ;
  65. test-branch ==
  66. "branch true" [true [1] [0] branch] satisfies [1 =] ?
  67. "branch false" [false [1] [0] branch] satisfies [0 =] ?
  68. "ifte true" [1 [0 >] [1] [0] ifte] satisfies [1 =] ?
  69. "ifte false" [0 [0 >] [1] [0] ifte] satisfies [0 =] ?
  70. "ifte restore" [2 [pop true] [2 +] [] ifte] satisfies [4 =] ? ;
  71. test-logic ==
  72. "not true" [true not] satisfies [false =] ?
  73. "not false" [false not]satisfies [true =] ?
  74. "or tt" [true true] satisfies [or] ?
  75. "or tf" [true false] satisfies [or] ?
  76. "or ft" [false true] satisfies [or] ?
  77. "or ff" [false false] satisfies [or not] ?
  78. "and tt" [true true] satisfies [and] ?
  79. "and tf" [true false] satisfies [and not] ?
  80. "and ft" [false true] satisfies [and not] ?
  81. "and ff" [false false] satisfies [and not] ?
  82. "xor tt" [true true] satisfies [xor not] ?
  83. "xor tf" [true false] satisfies [xor] ?
  84. "xor ft" [false true] satisfies [xor] ?
  85. "xor ff" [false false] satisfies [xor not] ? ;
  86. test-pop ==
  87. "pop2" [1 2 3 pop2] satisfies [1 =] ?
  88. "popop" [1 2 3 pop2] satisfies [1 =] ?
  89. "popd" [1 2 3 popd] satisfies [3 =] ?
  90. "popd 2" [1 2 3 popd] satisfies [pop 1 =] ? ;
  91. test-dup ==
  92. "dup2" [2 3 dup2] satisfies [[2 =] dip 3 = and] ?
  93. "dupd" [2 3 dupd] satisfies [[2 =] dip 3 = and] ? ;
  94. test-roll ==
  95. "rollup" [1 2 3 rollup] satisfies [2 = [1 =] dip and
  96. [3 =] dip and] ?
  97. "rolldown" [1 2 3 rolldown] satisfies [1 = [3 =] dip and
  98. [2 =] dip and] ?
  99. "rotate" [1 2 3 rotate] satisfies [1 = [2 =] dip and
  100. [3 =] dip and] ? ;
  101. test-app ==
  102. "app2" [1 3 [1 +] app2] satisfies [4 = [2 =] dip and] ?
  103. "app3" [1 3 5 [2 >] app3] satisfies [and [true =]
  104. [false =] i2] ? ;
  105. test-maxima ==
  106. ">=" [2 3 >= 3 3 >= 4 3 >=] satisfies [and swap not and] ?
  107. "<=" [2 3 <= 3 3 <= 4 3 <=] satisfies [not and and] ?
  108. "!=" [2 3 != 3 3 !=] satisfies [not and] ?
  109. "max" [2 3 max -2 3 max] satisfies [[3 =] app2 and] ? ;
  110. test-arithmetic ==
  111. "*" [3 7 * 0 5 *] satisfies [[21 =] [null] i2 and] ?
  112. "divmod" [11 3 divmod] satisfies [[3 =] [2 =] i2 and] ?
  113. "/" [21 5 / 25 25 /] satisfies [[4 =] [1 =] i2 and] ?
  114. "%" [21 2 % 37 5 %] satisfies [[1 =] [2 =] i2 and] ?
  115. "exp" [2 3 exp 3 0 exp] satisfies [[8 =] [1 =] i2 and] ?
  116. ;
  117. test-linrec ==
  118. (* We test * and divmod here because they are currently
  119. implemented in joy itself using linear recursion. *)
  120. "fact" [0 fact 4 fact] satisfies [[1 =] [24 =] i2 and] ?
  121. "sum-up-to" [6 sum-up-to] satisfies [21 =] ? ;
  122. test-aggregates ==
  123. "step" [[1 2 3] [] step]
  124. satisfies [[2 =] [3 =] i2 and [1 =] dip and] ?
  125. "step +" [[1 3] [5 +] step] satisfies [[6 =] [8 =] i2 and] ?
  126. "reverse" [[1 2] reverse]
  127. satisfies [[[1 =] [2 =] i2 and] infra car] ?
  128. "fold +" [[1 4 5] 0 [+] fold] satisfies [10 =] ?
  129. "fold swons" [[1 4 5] [] [swons] fold]
  130. satisfies [unstack 5 = swap 4 = and [1 =] dip and] ?
  131. "fold or" [[false true false] false [or] fold]
  132. satisfies [true =] ?
  133. "fold and" [[true true false] true [and] fold]
  134. satisfies [false =] ?
  135. "sum" [[1 2 8 9] sum] satisfies [20 =] ?
  136. "product" [[1 2 8 9] product] satisfies [144 =] ?
  137. "size" [[] size [1 4 8 9] size] satisfies [[0 =] [4 =] i2 and] ?
  138. "map id" [[1 2] [] map]
  139. satisfies [[[2 =] [1 =] i2 and] infra car] ?
  140. "map +" [[1 2] [10 +] map]
  141. satisfies [[[12 =] [11 =] i2 and] infra car] ?
  142. "map >" [[3 7] [4 >] map]
  143. satisfies [[[true =] [false =] i2 and] infra car] ?
  144. "list-tail" [[1 2 3] [1 list-tail car] [2 list-tail car] cleave]
  145. satisfies [[2 =] [3 =] i2 and] ?
  146. "list-head" [[1 2 3] [2 list-head i] [1 list-head i] cleave]
  147. satisfies [[2 =] [1 =] i2 and] ?
  148. "at" [[1 2 3 4] [2 at] [1 at] cleave]
  149. satisfies [[3 =] [2 =] i2 and] ?
  150. "of" [[1 2 3 4] [2 swap of] [1 swap of] cleave]
  151. satisfies [[3 =] [2 =] i2 and] ?
  152. ;
  153. run-base-tests ==
  154. test-swons
  155. test-unswons
  156. test-car
  157. test-cdr
  158. test-cadr
  159. test-first
  160. test-second
  161. test-third
  162. test-booleans
  163. test-leaf
  164. test-null
  165. test-nulld
  166. test-newstack
  167. test-i
  168. test-dip
  169. test-b
  170. test-cleave
  171. test-branch
  172. test-logic
  173. test-pop
  174. test-dup
  175. test-roll
  176. test-maxima
  177. test-arithmetic
  178. test-linrec
  179. test-aggregates ;
  180. END
  181. run-base-tests