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