/red-system/tests/source/units/math-mixed-test.reds

http://github.com/dockimbel/Red · Redscript · 495 lines · 379 code · 116 blank · 0 comment · 4 complexity · 3ca513e318dba896a74750ce078cd90d MD5 · raw file

  1. Red/System [
  2. Title: "Red/System math mixed tests script"
  3. Author: "Nenad Rakocevic"
  4. File: %math-mixed-test.reds
  5. Rights: "Copyright (C) 2011 Nenad Rakocevic. All rights reserved."
  6. License: "BSD-3 - https://github.com/dockimbel/Red/blob/origin/BSD-3-License.txt"
  7. ]
  8. #include %../../../../quick-test/quick-test.reds
  9. ~~~start-file~~~ "math-mixed"
  10. ===start-group=== "Math implicit type castings"
  11. --test-- "math-implicit-cast-1" --assert 10 - #"^C" = 7
  12. --test-- "math-implicit-cast-2" --assert -1 - #"^C" = -4
  13. --test-- "math-implicit-cast-3" --assert 10 + #"^C" = 13
  14. --test-- "math-implicit-cast-4" --assert -1 + #"^C" = 2
  15. --test-- "math-implicit-cast-5" --assert 10 * #"^C" = 30
  16. --test-- "math-implicit-cast-6" --assert -1 * #"^C" = -3
  17. --test-- "math-implicit-cast-7" --assert 10 / #"^C" = 3
  18. --test-- "math-implicit-cast-8" --assert -10 / #"^C" = -3
  19. --test-- "math-implicit-cast-9" --assert 1000 - #"^C" = 997
  20. byte: #"^C"
  21. --test-- "math-implicit-cast-10" --assert 10 - byte = 7
  22. --test-- "math-implicit-cast-11" --assert -1 - byte = -4
  23. --test-- "math-implicit-cast-12" --assert 10 + byte = 13
  24. --test-- "math-implicit-cast-13" --assert -1 + byte = 2
  25. --test-- "math-implicit-cast-14" --assert 10 * byte = 30
  26. --test-- "math-implicit-cast-15" --assert -1 * byte = -3
  27. --test-- "math-implicit-cast-16" --assert 10 / byte = 3
  28. --test-- "math-implicit-cast-17" --assert -10 / byte = -3
  29. --test-- "math-implicit-cast-18" --assert 1000 - byte = 997
  30. --test-- "math-implicit-cast-19" --assert 1000 or (as-integer byte) = 1003
  31. ===end-group===
  32. ===start-group=== "Simple nested expressions"
  33. ident: func [i [integer!] return: [integer!]][i]
  34. a: 1
  35. b: 2
  36. c: 3
  37. --test-- "math-nested-1"
  38. --assert 1 + (3 - 2) = 2
  39. --test-- "math-nested-2"
  40. --assert a + (3 - 2) = 2
  41. --test-- "math-nested-3"
  42. --assert a + (3 - b) = 2
  43. --test-- "math-nested-4"
  44. --assert a + (c - 2) = 2
  45. --test-- "math-nested-5"
  46. --assert a + (c - b) = b
  47. --test-- "math-nested-6"
  48. --assert (3 - 1) + (3 - 2) = 3
  49. --test-- "math-nested-7"
  50. --assert (c - a) + (c - b) = c
  51. --test-- "math-nested-8"
  52. --assert ((3 - 1) - (3 - 2)) - ((3 - 1) - (3 - 2)) = 0
  53. --test-- "math-nested-9"
  54. --assert ((c - a) - (c - b)) - ((c - a) - (c - b)) = 0
  55. --test-- "math-nested-10"
  56. --assert (3 * 1) + (3 * 2) = 9
  57. --test-- "math-nested-11"
  58. --assert (3 / 1) + (3 / 2) = 4
  59. --test-- "math-nested-12"
  60. --assert ((3 * 1) - (3 * 2)) * ((3 * 1) - (3 * 2)) = 9
  61. --test-- "math-nested-13"
  62. --assert (c * a) + (c * b) = 9
  63. --test-- "math-nested-14"
  64. --assert (3 * ident 1) + (3 * ident 2) = 9
  65. --test-- "math-nested-15"
  66. --assert ((ident 3) * ident 1) + ((ident 3) * ident 2) = 9
  67. foo-nested: func [
  68. /local a b c
  69. ][
  70. a: 2
  71. b: 3
  72. c: 4
  73. --test-- "loc-math-nested-1"
  74. --assert 1 + (3 - 2) = 2
  75. --test-- "loc-math-nested-2"
  76. --assert a + (3 - 2) = 3
  77. --test-- "loc-math-nested-3"
  78. --assert a + (3 - b) = 2
  79. --test-- "loc-math-nested-4"
  80. --assert a + (c - 2) = 4
  81. --test-- "loc-math-nested-5"
  82. --assert a + (c - b) = b
  83. --test-- "loc-math-nested-6"
  84. --assert (3 - 1) + (3 - 2) = 3
  85. --test-- "loc-math-nested-7"
  86. --assert (c - a) + (c - b) = b
  87. --test-- "loc-math-nested-8"
  88. --assert ((3 - 1) - (3 - 2)) - ((3 - 1) - (3 - 2)) = 0
  89. --test-- "loc-math-nested-9"
  90. --assert ((c - a) - (c - b)) - ((c - a) - (c - b)) = 0
  91. ]
  92. foo-nested
  93. ===end-group===
  94. ===start-group=== "Mixed nested expressions"
  95. fooA: func [a [int-ptr!] return: [int-ptr!]][a]
  96. fooB: func [b [integer!] return: [integer!]][b]
  97. s: declare struct! [
  98. a [int-ptr!]
  99. b [integer!]
  100. c [int-ptr!]
  101. ]
  102. s/b: 3
  103. size: 2
  104. --test-- "math-mixed-1"
  105. s/a: as int-ptr! 1000h
  106. s/a: s/a + 2 ;-- reg/imm
  107. --assert s/a = as int-ptr! 1008h
  108. --test-- "math-mixed-2"
  109. s/a: as int-ptr! 1000h
  110. i: 2 + as-integer s/a ;-- imm/reg
  111. --assert i = 1002h
  112. --test-- "math-mixed-3"
  113. s/a: as int-ptr! 1000h
  114. s/a: (as int-ptr! 2) + as-integer s/a ;-- imm/reg
  115. --assert s/a = as int-ptr! 4002h
  116. --test-- "math-mixed-4"
  117. s/a: as int-ptr! s/b + size ;-- reg/ref
  118. --assert s/a = as int-ptr! 05h
  119. --test-- "math-mixed-5"
  120. i: size + s/b ;-- ref/reg
  121. --assert i = 05h
  122. --test-- "math-mixed-6"
  123. s/a: as int-ptr! size + s/b ;-- ref/reg
  124. --assert s/a = as int-ptr! 05h
  125. --test-- "math-mixed-7"
  126. i: s/b + size - 1 ;-- (reg/ref)/imm
  127. --assert i = 04h
  128. --test-- "math-mixed-8"
  129. i: s/b - 1 + size ;-- (reg/imm)/ref
  130. --assert i = 04h
  131. --test-- "math-mixed-9"
  132. s/a: as int-ptr! 1000h
  133. s/a: s/a + size - 1 ;-- (reg/ref)/imm
  134. --assert s/a = as int-ptr! 1004h
  135. --test-- "math-mixed-10"
  136. s/a: as int-ptr! 1000h
  137. s/a: s/a - 1 + size ;-- (reg/imm)/ref
  138. --assert s/a = as int-ptr! 1004h
  139. --test-- "math-mixed-11"
  140. s/a: as int-ptr! 1000h
  141. s/c: s/a + s/b + 2 ;-- (reg/reg)/imm
  142. --assert s/c = as int-ptr! 1014h
  143. --test-- "math-mixed-12"
  144. s/a: as int-ptr! 1000h
  145. s/c: s/a + s/b + size ;-- (reg/reg)/ref
  146. --assert s/c = as int-ptr! 1014h
  147. --test-- "math-mixed-13"
  148. s/a: as int-ptr! 1000h
  149. s/c: s/a + s/b + s/b ;-- (reg/reg)/reg
  150. --assert s/c = as int-ptr! 1018h
  151. --test-- "math-mixed-14"
  152. s/a: as int-ptr! 1000h
  153. s/a: (fooA s/a) + 2 ;-- reg/imm
  154. --assert s/a = as int-ptr! 1008h
  155. --test-- "math-mixed-15"
  156. s/a: as int-ptr! 1000h
  157. i: 2 + as-integer fooA s/a ;-- imm/reg
  158. --assert i = 1002h
  159. --test-- "math-mixed-16"
  160. s/a: as int-ptr! 1000h
  161. s/a: (as int-ptr! 2) + as-integer fooA s/a ;-- imm/reg
  162. --assert s/a = as int-ptr! 4002h
  163. --test-- "math-mixed-17"
  164. s/a: (as int-ptr! fooB s/b) + size ;-- reg/ref
  165. --assert s/a = as int-ptr! 0Bh
  166. --test-- "math-mixed-18"
  167. i: size + fooB s/b ;-- ref/reg
  168. --assert i = 05h
  169. --test-- "math-mixed-19"
  170. s/a: (as int-ptr! size) + fooB s/b ;-- ref/reg
  171. --assert s/a = as int-ptr! 0Eh
  172. --test-- "math-mixed-20"
  173. i: (fooB s/b) + size - 1 ;-- (reg/ref)/imm
  174. --assert i = 04h
  175. --test-- "math-mixed-21"
  176. i: (fooB s/b) - 1 + size ;-- (reg/imm)/ref
  177. --assert i = 04h
  178. --test-- "math-mixed-22"
  179. s/a: as int-ptr! 1000h
  180. s/a: (fooA s/a) + size - 1 ;-- (reg/ref)/imm
  181. --assert s/a = as int-ptr! 1004h
  182. --test-- "math-mixed-23"
  183. s/a: as int-ptr! 1000h
  184. s/a: (fooA s/a) - 1 + size ;-- (reg/imm)/ref
  185. --assert s/a = as int-ptr! 1004h
  186. --test-- "math-mixed-24"
  187. s/a: as int-ptr! 1000h
  188. s/c: (fooA s/a) + s/b + 2 ;-- (reg/reg)/imm
  189. --assert s/c = as int-ptr! 1014h
  190. --test-- "math-mixed-25"
  191. s/a: as int-ptr! 1000h
  192. s/c: (fooA s/a) + s/b + size ;-- (reg/reg)/ref
  193. --assert s/c = as int-ptr! 1014h
  194. --test-- "math-mixed-26"
  195. s/a: as int-ptr! 1000h
  196. s/c: (fooA s/a) + (fooB s/b) + s/b ;-- (reg/reg)/reg
  197. --assert s/c = as int-ptr! 1018h
  198. --test-- "math-mixed-27"
  199. s/a: as int-ptr! 1000h
  200. s/c: (fooA s/a) + (fooB s/b) + 2 ;-- (reg/reg)/imm
  201. --assert s/c = as int-ptr! 1014h
  202. --test-- "math-mixed-28"
  203. s/a: as int-ptr! 1000h
  204. s/c: (fooA s/a) + (fooB s/b) + size ;-- (reg/reg)/ref
  205. --assert s/c = as int-ptr! 1014h
  206. --test-- "math-mixed-29"
  207. s/a: as int-ptr! 1000h
  208. s/c: (fooA s/a) + s/b + (fooB s/b) ;-- (reg/reg)/reg
  209. --assert s/c = as int-ptr! 1018h
  210. --test-- "math-mixed-30"
  211. s/a: as int-ptr! 1000h
  212. i: s/b + (fooA s/a) + (fooB s/b) ;-- (reg/reg)/reg
  213. --assert i = 1006h
  214. --test-- "math-mixed-31"
  215. s/a: as int-ptr! 1000h
  216. i: 2 + (fooA s/a) + (fooB s/b) ;-- (imm/reg)/reg
  217. --assert i = 1005h
  218. --test-- "math-mixed-32"
  219. s/a: as int-ptr! 1000h
  220. i: 2 + (fooA s/a) + size ;-- (imm/reg)/ref
  221. --assert i = 1004h
  222. --test-- "math-mixed-33"
  223. s/a: as int-ptr! 1000h
  224. i: 2 + (fooA s/a) + 3 ;-- (imm/reg)/imm
  225. --assert i = 1005h
  226. --test-- "math-mixed-34"
  227. s/a: as int-ptr! 1000h
  228. s/a: s/a - 2 ;-- reg/imm
  229. --assert s/a = as int-ptr! 0FF8h
  230. --test-- "math-mixed-35"
  231. s/a: as int-ptr! 1000h
  232. i: 2000h - as-integer s/a ;-- imm/reg
  233. --assert i = 1000h
  234. --test-- "math-mixed-36"
  235. s/a: as int-ptr! 1000h
  236. s/a: (as int-ptr! 6000h) - as-integer s/a ;-- imm/reg
  237. --assert s/a = as int-ptr! 2000h
  238. --test-- "math-mixed-37"
  239. s/a: as int-ptr! s/b - size ;-- reg/ref
  240. --assert s/a = as int-ptr! 01h
  241. --test-- "math-mixed-38"
  242. i: size - s/b ;-- ref/reg
  243. --assert i = -1
  244. --test-- "math-mixed-39"
  245. s/a: (as int-ptr! size) - s/b ;-- ref/reg
  246. --assert s/a = as int-ptr! -10
  247. --test-- "math-mixed-40"
  248. i: s/b - size - 1 ;-- (reg/ref)/imm
  249. --assert i = 0
  250. --test-- "math-mixed-41"
  251. i: s/b - 1 - size ;-- (reg/imm)/ref
  252. --assert i = 0
  253. --test-- "math-mixed-42"
  254. s/a: as int-ptr! 1000h
  255. s/a: s/a - size - 1 ;-- (reg/ref)/imm
  256. --assert s/a = as int-ptr! 0FF4h
  257. --test-- "math-mixed-43"
  258. s/a: as int-ptr! 1000h
  259. s/a: s/a - 1 - size ;-- (reg/imm)/ref
  260. --assert s/a = as int-ptr! 0FF4h
  261. --test-- "math-mixed-44"
  262. s/a: as int-ptr! 1000h
  263. s/c: s/a - s/b - 2 ;-- (reg/reg)/imm
  264. --assert s/c = as int-ptr! 0FECh
  265. --test-- "math-mixed-45"
  266. s/a: as int-ptr! 1000h
  267. s/c: s/a - s/b - size ;-- (reg/reg)/ref
  268. --assert s/c = as int-ptr! 0FECh
  269. --test-- "math-mixed-46"
  270. s/a: as int-ptr! 1000h
  271. s/c: s/a - s/b - s/b ;-- (reg/reg)/reg
  272. --assert s/c = as int-ptr! 0FE8h
  273. --test-- "math-mixed-47"
  274. s/a: as int-ptr! 1000h
  275. s/a: (fooA s/a) - 2 ;-- reg/imm
  276. --assert s/a = as int-ptr! 0FF8h
  277. --test-- "math-mixed-48"
  278. s/a: as int-ptr! 1000h
  279. i: 2 - as-integer fooA s/a ;-- imm/reg
  280. --assert i = FFFFF002h
  281. --test-- "math-mixed-49"
  282. s/a: as int-ptr! 1000h
  283. s/a: (as int-ptr! 2) - as-integer fooA s/a ;-- imm/reg
  284. --assert s/a = as int-ptr! FFFFC002h
  285. --test-- "math-mixed-50"
  286. s/a: (as int-ptr! fooB s/b) - size ;-- reg/ref
  287. --assert s/a = as int-ptr! -5
  288. --test-- "math-mixed-51"
  289. i: size - fooB s/b ;-- ref/reg
  290. --assert i = -1
  291. --test-- "math-mixed-52"
  292. s/a: (as int-ptr! size) - fooB s/b ;-- ref/reg
  293. --assert s/a = as int-ptr! -10
  294. --test-- "math-mixed-53"
  295. i: (fooB s/b) - size - 1 ;-- (reg/ref)/imm
  296. --assert i = 0
  297. --test-- "math-mixed-54"
  298. i: (fooB s/b) - 1 - size ;-- (reg/imm)/ref
  299. --assert i = 0
  300. --test-- "math-mixed-55"
  301. s/a: as int-ptr! 1000h
  302. s/a: (fooA s/a) - size - 1 ;-- (reg/ref)/imm
  303. --assert s/a = as int-ptr! 0FF4h
  304. --test-- "math-mixed-56"
  305. s/a: as int-ptr! 1000h
  306. s/a: (fooA s/a) - 1 - size ;-- (reg/imm)/ref
  307. --assert s/a = as int-ptr! 0FF4h
  308. --test-- "math-mixed-57"
  309. s/a: as int-ptr! 1000h
  310. s/c: (fooA s/a) - s/b - 2 ;-- (reg/reg)/imm
  311. --assert s/c = as int-ptr! 0FECh
  312. --test-- "math-mixed-58"
  313. s/a: as int-ptr! 1000h
  314. s/c: (fooA s/a) - s/b - size ;-- (reg/reg)/ref
  315. --assert s/c = as int-ptr! 0FECh
  316. --test-- "math-mixed-59"
  317. s/a: as int-ptr! 1000h
  318. s/c: (fooA s/a) - (fooB s/b) - s/b ;-- (reg/reg)/reg
  319. --assert s/c = as int-ptr! 0FE8h
  320. --test-- "math-mixed-60"
  321. s/a: as int-ptr! 1000h
  322. s/c: (fooA s/a) - (fooB s/b) - 2 ;-- (reg/reg)/imm
  323. --assert s/c = as int-ptr! 0FECh
  324. --test-- "math-mixed-61"
  325. s/a: as int-ptr! 1000h
  326. s/c: (fooA s/a) - (fooB s/b) - size ;-- (reg/reg)/ref
  327. --assert s/c = as int-ptr! 0FECh
  328. --test-- "math-mixed-62"
  329. s/a: as int-ptr! 1000h
  330. s/c: (fooA s/a) - s/b - (fooB s/b) ;-- (reg/reg)/reg
  331. --assert s/c = as int-ptr! 0FE8h
  332. --test-- "math-mixed-63"
  333. s/a: as int-ptr! 1000h
  334. i: s/b - (fooA s/a) - (fooB s/b) ;-- (reg/reg)/reg
  335. --assert i = FFFFF000h
  336. --test-- "math-mixed-64"
  337. s/a: as int-ptr! 1000h
  338. i: 2 - (fooA s/a) - (fooB s/b) ;-- (imm/reg)/reg
  339. --assert i = FFFFEFFFh
  340. --test-- "math-mixed-65"
  341. s/a: as int-ptr! 1000h
  342. i: 2 - (fooA s/a) - size ;-- (imm/reg)/ref
  343. --assert i = FFFFF000h
  344. --test-- "math-mixed-66"
  345. s/a: as int-ptr! 1000h
  346. i: 2 - (fooA s/a) - 3 ;-- (imm/reg)/imm
  347. --assert i = FFFFEFFFh
  348. --test-- "math-mixed-67"
  349. s/a: as int-ptr! 1000h
  350. s/c: as int-ptr! 1010h
  351. i: as-integer s/c - s/a ;-- reg/reg (pointer - pointer)
  352. --assert i = 10h
  353. --test-- "math-mixed-68"
  354. s/a: as int-ptr! 1000h
  355. s/c: as int-ptr! 1010h
  356. i: as-integer (s/c - s/a) + 1 ;-- reg/reg (pointer - pointer)
  357. --assert i = 14h
  358. --test-- "math-mixed-69"
  359. s/a: as int-ptr! 00154E28h
  360. s/c: as int-ptr! 00150018h
  361. i: (as-integer (s/a - s/c) + 1) / 4 ;-- reg/reg (pointer - pointer)
  362. --assert i = 4997
  363. --test-- "math-mixed-70"
  364. p: declare struct! [
  365. a [byte-ptr!]
  366. c [int-ptr!]
  367. ]
  368. p/a: as byte-ptr! 001F0014h
  369. p/c: as int-ptr! 001D0004h
  370. i: as-integer (p/a - ((as byte-ptr! p/c) + 16)) ;-- reg/(reg/imm)
  371. --assert i = 00020000h
  372. --test-- "math-mixed-71"
  373. p/a: as byte-ptr! 001F0014h
  374. p/c: as int-ptr! 001D0014h
  375. series: p/c
  376. sz: 32
  377. --assert ((as byte-ptr! series) + sz) < p/a ;-- (ref/ref)/reg
  378. ===end-group===
  379. ~~~end-file~~~