/red-system/tests/source/units/cast-test.reds

http://github.com/dockimbel/Red · Redscript · 473 lines · 384 code · 89 blank · 0 comment · 5 complexity · da0af4e25c979f67312fb9459377e821 MD5 · raw file

  1. Red/System [
  2. Title: "Red/System datatype casting test script"
  3. Author: "Nenad Rakocevic & Peter W A Wood"
  4. File: %cast-test.reds
  5. Rights: "Copyright (C) 2011 Nenad Rakocevic & Peter W A Wood. 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~~~ "cast"
  10. ===start-group=== "cast from byte!"
  11. --test-- "byte-cast-1"
  12. --assert 65 = as integer! #"A"
  13. --test-- "byte-cast-2"
  14. cast-b: #"A"
  15. cast-i: 65
  16. --assert cast-i = as integer! cast-b
  17. --test-- "byte-cast-3"
  18. --assert false = as logic! #"^(00)"
  19. --test-- "byte-cast-4"
  20. cast-b: #"^(00)"
  21. l: false
  22. --assert l = as logic! cast-b
  23. --test-- "byte-cast-5"
  24. --assert true = as logic! #"A"
  25. --test-- "byte-cast-6"
  26. cast-b: #"A"
  27. l: true
  28. --assert l = as logic! cast-b
  29. --test-- "byte-cast-7"
  30. --assert true = as logic! #"^(FF)"
  31. --test-- "byte-cast-8"
  32. cast-b: #"^(FF)"
  33. l: true
  34. --assert l = as logic! cast-b
  35. --test-- "byte-cast-9" ;-- issue #158
  36. a: as-byte 10h
  37. b: as-byte 80h
  38. --assert 10h = as-integer a
  39. --test-- "byte-cast-10" ;-- issue #159
  40. a: as-byte 10h
  41. b: as-byte 80h
  42. c: as-integer a
  43. --assert 10h = c
  44. --test-- "byte-cast-11" ;-- issue #160
  45. a: as-byte 1
  46. b: as-byte 2
  47. --assert (as byte-ptr! (as-integer a) * 2) = (as byte-ptr! 2)
  48. --assert (as byte-ptr! (as-integer a) << 2) = (as byte-ptr! 4)
  49. --test-- "byte-cast-12" ;-- issue #161
  50. a: as-byte 1
  51. b: as-byte 2
  52. c: (as-integer b) << 16 or as-integer a
  53. --assert 00020001h = c
  54. --test-- "byte-cast-13" ;-- issue #162
  55. sb: declare struct! [
  56. a [byte!]
  57. b [byte!]
  58. ]
  59. sb/a: as-byte 0
  60. sb/b: as-byte 1
  61. --assert not as-logic sb/a
  62. --test-- "byte-cast-13" ;-- issue #150
  63. s!: alias struct! [
  64. a [byte!]
  65. b [byte!]
  66. c [byte!]
  67. d [byte!]
  68. ]
  69. t: declare s!
  70. t/a: as-byte 1
  71. t/b: as-byte 1
  72. t/c: as-byte 0
  73. t/d: as-byte 0
  74. h: as-integer t/a
  75. --assert h = 1
  76. ===end-group===
  77. ===start-group=== "cast from integer!"
  78. comment {
  79. --test-- "int-cast-1"
  80. --assert #"^(00)" = as byte! 0
  81. }
  82. --test-- "int-cast-2"
  83. i: 0
  84. cast-test-b: #"^(00)"
  85. --assert cast-test-b = as byte! i
  86. --test-- "int-cast-3"
  87. --assert #"^(01)" = as byte! 1
  88. --test-- "int-cast-4"
  89. i: 1
  90. cast-test-b: #"^(01)"
  91. --assert cast-test-b = as byte! i
  92. --test-- "int-cast-5"
  93. --assert #"^(FF)" = as byte! 255
  94. --test-- "int-cast-6"
  95. i: 255
  96. cast-test-b: #"^(FF)"
  97. --assert cast-test-b = as byte! i
  98. --test-- "int-cast-7"
  99. --assert #"^(00)" = as byte! 256
  100. --test-- "int-cast-8"
  101. i: 256
  102. cast-test-b: #"^(00)"
  103. --assert cast-test-b = as byte! i
  104. --test-- "int-cast-9"
  105. --assert false = as logic! 0
  106. --test-- "int-cast-10"
  107. i: 0
  108. l: false
  109. --assert l = as logic! i
  110. --test-- "int-cast-11"
  111. --assert true = as logic! FFFFFFFFh
  112. --test-- "int-cast-12"
  113. i: FFFFFFFFh
  114. l: true
  115. --assert l = as logic! i
  116. --test-- "int-cast-13"
  117. --assert true = as logic! 1
  118. --test-- "int-cast-14"
  119. i: 1
  120. l: true
  121. --assert l = as logic! i
  122. --test-- "int-cast-15"
  123. cs: "Hello"
  124. cs2: ""
  125. i: as integer! cs
  126. i: i + 1
  127. cs2: as c-string! i
  128. --assert cs2/1 = #"e"
  129. --assert 4 = length? cs2
  130. --test-- "int-cast-16" ;; This test assumes 32-bit target
  131. i: 1
  132. p: declare pointer! [integer!]
  133. p: as [pointer! [integer!]] i
  134. P: P + 1
  135. i2: as integer! p
  136. --assert i2 = 5
  137. --test-- "int-cast-17" ;; This test assumes 32-bit target
  138. ;; currently fails as p-int-cast is not declared
  139. i: 1
  140. p-int-cast-17: as [pointer! [integer!]] i
  141. P-int-cast-17: P-int-cast-17 + 1
  142. i2: as integer! p-int-cast-17
  143. --assert i2 = 5
  144. --test-- "int-cast-18"
  145. i: 1
  146. s: declare struct! [
  147. a [integer!]
  148. b [integer!]
  149. ]
  150. s: as [struct! [a [integer!] b [integer!]]] i
  151. s: s + 1
  152. i2: as integer! s
  153. --assert i2 = 9
  154. --test-- "int-cast-19"
  155. ic19-logic: either as-logic 0 [
  156. false
  157. ][
  158. true
  159. ]
  160. --assert ic19-logic
  161. --test-- "int-cast-20"
  162. ic20-dummy: func [return: [integer!]][0]
  163. ic20-logic: either as-logic ic20-dummy [
  164. false
  165. ][
  166. true
  167. ]
  168. --assert ic20-logic
  169. ===end-group===
  170. ===start-group=== "cast from logic!"
  171. --test-- "logic-cast-1"
  172. --assert #"^(01)" = as byte! true
  173. --test-- "logic-cast-2"
  174. cast-test-b: #"^(01)"
  175. l: true
  176. --assert cast-test-b = as byte! l
  177. --test-- "logic-cast-3"
  178. --assert #"^(00)" = as byte! false
  179. --test-- "logic-cast-4"
  180. cast-test-b: #"^(00)"
  181. l: false
  182. --assert cast-test-b = as byte! l
  183. --test-- "logic-cast-5"
  184. --assert 1 = as integer! true
  185. --test-- "logic-cast-6"
  186. i: 1
  187. l: true
  188. --assert i = as integer! l
  189. --test-- "logic-cast-7"
  190. --assert 0 = as integer! false
  191. --test-- "logic-cast-8"
  192. i: 0
  193. l: false
  194. --assert i = as integer! l
  195. ===end-group===
  196. ===start-group=== "cast c-string! tests"
  197. --test-- "c-string-cast-1"
  198. csc1-str: "Hello, Nenad"
  199. i: 0
  200. i: as integer! csc1-str
  201. i: i + 7
  202. csc1-str: as c-string! i
  203. --assert csc1-str/1 = #"N"
  204. --assert csc1-str/2 = #"e"
  205. --assert csc1-str/3 = #"n"
  206. --assert csc1-str/4 = #"a"
  207. --assert csc1-str/5 = #"d"
  208. comment {
  209. --test-- "c-string-cast-2"
  210. --assert false = as logic! ""
  211. }
  212. --test-- "c-string-cast-3"
  213. csc3-str: ""
  214. --assert true = as logic! csc3-str
  215. --test-- "c-string-cast-4"
  216. --assert true = as logic! "Any old iron, any old iron"
  217. --test-- "c-string-cast-5"
  218. csc5-str: "Why not?"
  219. --assert true = as logic! csc5-str
  220. --test-- "c-string-cast-6"
  221. csc6-str: "Tour de France"
  222. csc6-p: declare pointer! [integer!]
  223. csc6-p: as [pointer! [integer!]] csc6-str
  224. csc6-p: csc6-p + 2
  225. csc6-str2: as c-string! csc6-p
  226. --assert csc6-str2/1 = #"F"
  227. --assert csc6-str2/2 = #"r"
  228. --assert csc6-str2/3 = #"a"
  229. --assert csc6-str2/4 = #"n"
  230. --assert csc6-str2/5 = #"c"
  231. --assert csc6-str2/6 = #"e"
  232. --test-- "C-string-cast-7"
  233. csc7-struct: declare struct! [
  234. c1 [byte!]
  235. c2 [byte!]
  236. c3 [byte!]
  237. c4 [byte!]
  238. c5 [byte!]
  239. ]
  240. csc7-str: "Peter"
  241. csc7-struct: as [struct! [
  242. c1 [byte!] c2 [byte!] c3 [byte!] c4 [byte!] c5 [byte!]
  243. ]] csc7-str
  244. --assert csc7-struct/c1 = #"P"
  245. --assert csc7-struct/c2 = #"e"
  246. --assert csc7-struct/c3 = #"t"
  247. --assert csc7-struct/c4 = #"e"
  248. --assert csc7-struct/c5 = #"r"
  249. ===end-group===
  250. ===start-group=== "cast from pointer!"
  251. --test-- "csp-1"
  252. csp1-p: declare pointer! [integer!]
  253. csp1-p: as [pointer! [integer!]] 256
  254. i: 0
  255. i: as integer! csp1-p
  256. --assert i = 256
  257. --test-- "csp-2"
  258. csp2-p: declare pointer! [integer!]
  259. csp2-p: as [pointer! [integer!]] 0
  260. --assert false = as logic! csp2-p
  261. --test-- "csp-3"
  262. csp3-p: declare pointer! [integer!]
  263. csp3-p: as [pointer! [integer!]] 1
  264. --assert true = as logic! csp3-p
  265. --test-- "csp-4"
  266. csp4-p: declare pointer! [integer!]
  267. csp4-p: as [pointer! [integer!]] FFFFFFFFh
  268. --assert true = as logic! csp4-p
  269. --test-- "csp-5"
  270. csp5-p: declare pointer! [integer!]
  271. csp5-p: as [pointer! [integer!]] 7FFFFFFFh
  272. --assert true = as logic! csp5-p
  273. ;; No test for pointer! to c-string! as it would simply
  274. ;; duplicate the one of c-string! to pointer!
  275. --test-- "csp-6"
  276. csp6-p: declare pointer! [integer!]
  277. csp6-s: declare struct! [
  278. a [integer!]
  279. b [integer!]
  280. ]
  281. csp6-s/a: 1
  282. csp6-s/b: 2
  283. csp6-p: as [pointer! [integer!]] csp6-s
  284. --assert csp6-p/value = 1
  285. csp6-p: csp6-p + 1
  286. --assert csp6-p/value = 2
  287. csp6-p: csp6-p - 1
  288. csp6-s: as [struct! [a [integer!] b [integer!]]] csp6-p
  289. --assert csp6-s/a = 1
  290. csp6-p: csp6-p + 1
  291. csp6-s: as [struct! [a [integer!] b [integer!]]] csp6-p
  292. --assert csp6-s/a = 2
  293. ===end-group===
  294. ===start-group=== "cast from struct!"
  295. ;; no test for cast to integer as it would simply
  296. ;; duplicate the test from integer! to struct!
  297. --test-- "cfstruc-1"
  298. cfs1-struct: declare struct! [
  299. a [integer!]
  300. b [integer!]
  301. ]
  302. cfs1-struct: as [struct! [a [integer!] b [integer!]]] 0
  303. --assert false = as logic! cfs1-struct
  304. --test-- "cfstruc-2"
  305. cfs2-struct: declare struct! [
  306. a [integer!]
  307. b [integer!]
  308. ]
  309. --assert true = as logic! cfs2-struct
  310. ;; no test for cast to c-string! as it would simply
  311. ;; duplicate the test from c-string! to struct!
  312. ;; no test for cast to pointer! as it would simply
  313. ;; duplicate the test from pointer! to struct!
  314. ===end-group===
  315. ===start-group=== "byte-integer-cast"
  316. --test-- "bic-1"
  317. bic-a: as-byte 1
  318. --assert 1 = as-integer bic-a
  319. --test-- "bic-2"
  320. bic-a: as-byte 1
  321. --assert 65536 = ((as-integer bic-a) << 16)
  322. --test-- "bic-3"
  323. bic-a: as-byte 2
  324. --assert 131072 = ((as-integer bic-a) << 16)
  325. --test-- "bic-4"
  326. bic-a: #"^(01)"
  327. --assert 65537 = (65536 or (as-integer bic-a))
  328. --test-- "bic-5"
  329. bic-a: #"^(01)"
  330. bic-b: #"^(02)"
  331. --assert 131073 = ((as-integer bic-b) << 16 or as-integer bic-a)
  332. --test-- "bic-6"
  333. bic-a: #"^(01)"
  334. bic-b: #"^(02)"
  335. --assert 131073 = ((as-integer bic-b) << 16 or as-integer bic-a)
  336. --test-- "bic-7"
  337. bic-a: #"^(01)"
  338. bic-b: #"^(02)"
  339. --assert (as byte-ptr! 131073) = as byte-ptr! ((as-integer bic-b) << 16 or as-integer bic-a)
  340. --test-- "bic-8"
  341. bic-a: as-byte 1
  342. bic-b: as-byte 2
  343. --assert 131073 = ((as-integer bic-b) << 16 or as-integer bic-a)
  344. --test-- "bic-9"
  345. bic-a: as-byte 1
  346. bic-b: as-byte 2
  347. --assert (as byte-ptr! 131073) = as byte-ptr! ((as-integer bic-b) << 16 or as-integer bic-a)
  348. --test-- "bic-10"
  349. bic-s: declare struct! [
  350. bic-a [byte!]
  351. bic-b [byte!]
  352. bic-c [integer!]
  353. bic-d [byte!]
  354. bic-e [byte!]
  355. bic-f [integer!]
  356. ]
  357. bic-s/bic-a: as-byte 0
  358. bic-s/bic-b: as-byte 1
  359. bic-s/bic-c: 2
  360. bic-s/bic-d: as-byte 3
  361. bic-s/bic-e: as-byte 255
  362. bic-s/bic-f: 255
  363. --assert false = as-logic bic-s/bic-a
  364. --assert true = as-logic bic-s/bic-b
  365. --assert bic-s/bic-e = as-byte bic-s/bic-f
  366. --assert bic-s/bic-f = as-integer bic-s/bic-e
  367. ===end-group===
  368. ===start-group=== "Cast in conditional tests"
  369. cic-r: false
  370. --test-- "cic-1"
  371. cic-d: as-byte 0
  372. either as-logic cic-d [cic-r: false] [cic-r: true]
  373. --assert cic-r
  374. --test-- "cic-2"
  375. cic-s: declare struct! [
  376. a [byte!]
  377. b [byte!]
  378. ]
  379. cic-s/a: as-byte 0
  380. cic-s/b: as-byte 1
  381. either as-logic cic-s/a [cic-r: false] [cic-r: true]
  382. --assert cic-r
  383. ===end-group===
  384. ~~~end-file~~~