/red-system/tests/source/units/math-mixed-test.reds
Unknown | 495 lines | 379 code | 116 blank | 0 comment | 0 complexity | 3ca513e318dba896a74750ce078cd90d MD5 | raw file
1Red/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 9#include %../../../../quick-test/quick-test.reds 10 11~~~start-file~~~ "math-mixed" 12 13===start-group=== "Math implicit type castings" 14 15 --test-- "math-implicit-cast-1" --assert 10 - #"^C" = 7 16 --test-- "math-implicit-cast-2" --assert -1 - #"^C" = -4 17 --test-- "math-implicit-cast-3" --assert 10 + #"^C" = 13 18 --test-- "math-implicit-cast-4" --assert -1 + #"^C" = 2 19 --test-- "math-implicit-cast-5" --assert 10 * #"^C" = 30 20 --test-- "math-implicit-cast-6" --assert -1 * #"^C" = -3 21 --test-- "math-implicit-cast-7" --assert 10 / #"^C" = 3 22 --test-- "math-implicit-cast-8" --assert -10 / #"^C" = -3 23 --test-- "math-implicit-cast-9" --assert 1000 - #"^C" = 997 24 25 byte: #"^C" 26 --test-- "math-implicit-cast-10" --assert 10 - byte = 7 27 --test-- "math-implicit-cast-11" --assert -1 - byte = -4 28 --test-- "math-implicit-cast-12" --assert 10 + byte = 13 29 --test-- "math-implicit-cast-13" --assert -1 + byte = 2 30 --test-- "math-implicit-cast-14" --assert 10 * byte = 30 31 --test-- "math-implicit-cast-15" --assert -1 * byte = -3 32 --test-- "math-implicit-cast-16" --assert 10 / byte = 3 33 --test-- "math-implicit-cast-17" --assert -10 / byte = -3 34 --test-- "math-implicit-cast-18" --assert 1000 - byte = 997 35 36 --test-- "math-implicit-cast-19" --assert 1000 or (as-integer byte) = 1003 37 38===end-group=== 39 40 41===start-group=== "Simple nested expressions" 42 43 ident: func [i [integer!] return: [integer!]][i] 44 45 a: 1 46 b: 2 47 c: 3 48 49 --test-- "math-nested-1" 50 --assert 1 + (3 - 2) = 2 51 52 --test-- "math-nested-2" 53 --assert a + (3 - 2) = 2 54 55 --test-- "math-nested-3" 56 --assert a + (3 - b) = 2 57 58 --test-- "math-nested-4" 59 --assert a + (c - 2) = 2 60 61 --test-- "math-nested-5" 62 --assert a + (c - b) = b 63 64 --test-- "math-nested-6" 65 --assert (3 - 1) + (3 - 2) = 3 66 67 --test-- "math-nested-7" 68 --assert (c - a) + (c - b) = c 69 70 --test-- "math-nested-8" 71 --assert ((3 - 1) - (3 - 2)) - ((3 - 1) - (3 - 2)) = 0 72 73 --test-- "math-nested-9" 74 --assert ((c - a) - (c - b)) - ((c - a) - (c - b)) = 0 75 76 --test-- "math-nested-10" 77 --assert (3 * 1) + (3 * 2) = 9 78 79 --test-- "math-nested-11" 80 --assert (3 / 1) + (3 / 2) = 4 81 82 --test-- "math-nested-12" 83 --assert ((3 * 1) - (3 * 2)) * ((3 * 1) - (3 * 2)) = 9 84 85 --test-- "math-nested-13" 86 --assert (c * a) + (c * b) = 9 87 88 --test-- "math-nested-14" 89 --assert (3 * ident 1) + (3 * ident 2) = 9 90 91 --test-- "math-nested-15" 92 --assert ((ident 3) * ident 1) + ((ident 3) * ident 2) = 9 93 94 95 foo-nested: func [ 96 /local a b c 97 ][ 98 a: 2 99 b: 3 100 c: 4 101 102 --test-- "loc-math-nested-1" 103 --assert 1 + (3 - 2) = 2 104 105 --test-- "loc-math-nested-2" 106 --assert a + (3 - 2) = 3 107 108 --test-- "loc-math-nested-3" 109 --assert a + (3 - b) = 2 110 111 --test-- "loc-math-nested-4" 112 --assert a + (c - 2) = 4 113 114 --test-- "loc-math-nested-5" 115 --assert a + (c - b) = b 116 117 --test-- "loc-math-nested-6" 118 --assert (3 - 1) + (3 - 2) = 3 119 120 --test-- "loc-math-nested-7" 121 --assert (c - a) + (c - b) = b 122 123 --test-- "loc-math-nested-8" 124 --assert ((3 - 1) - (3 - 2)) - ((3 - 1) - (3 - 2)) = 0 125 126 --test-- "loc-math-nested-9" 127 --assert ((c - a) - (c - b)) - ((c - a) - (c - b)) = 0 128 ] 129 foo-nested 130 131===end-group=== 132 133===start-group=== "Mixed nested expressions" 134 135 fooA: func [a [int-ptr!] return: [int-ptr!]][a] 136 fooB: func [b [integer!] return: [integer!]][b] 137 138 s: declare struct! [ 139 a [int-ptr!] 140 b [integer!] 141 c [int-ptr!] 142 ] 143 s/b: 3 144 size: 2 145 146 --test-- "math-mixed-1" 147 s/a: as int-ptr! 1000h 148 s/a: s/a + 2 ;-- reg/imm 149 --assert s/a = as int-ptr! 1008h 150 151 --test-- "math-mixed-2" 152 s/a: as int-ptr! 1000h 153 i: 2 + as-integer s/a ;-- imm/reg 154 --assert i = 1002h 155 156 --test-- "math-mixed-3" 157 s/a: as int-ptr! 1000h 158 s/a: (as int-ptr! 2) + as-integer s/a ;-- imm/reg 159 --assert s/a = as int-ptr! 4002h 160 161 --test-- "math-mixed-4" 162 s/a: as int-ptr! s/b + size ;-- reg/ref 163 --assert s/a = as int-ptr! 05h 164 165 --test-- "math-mixed-5" 166 i: size + s/b ;-- ref/reg 167 --assert i = 05h 168 169 --test-- "math-mixed-6" 170 s/a: as int-ptr! size + s/b ;-- ref/reg 171 --assert s/a = as int-ptr! 05h 172 173 --test-- "math-mixed-7" 174 i: s/b + size - 1 ;-- (reg/ref)/imm 175 --assert i = 04h 176 177 --test-- "math-mixed-8" 178 i: s/b - 1 + size ;-- (reg/imm)/ref 179 --assert i = 04h 180 181 --test-- "math-mixed-9" 182 s/a: as int-ptr! 1000h 183 s/a: s/a + size - 1 ;-- (reg/ref)/imm 184 --assert s/a = as int-ptr! 1004h 185 186 --test-- "math-mixed-10" 187 s/a: as int-ptr! 1000h 188 s/a: s/a - 1 + size ;-- (reg/imm)/ref 189 --assert s/a = as int-ptr! 1004h 190 191 --test-- "math-mixed-11" 192 s/a: as int-ptr! 1000h 193 s/c: s/a + s/b + 2 ;-- (reg/reg)/imm 194 --assert s/c = as int-ptr! 1014h 195 196 --test-- "math-mixed-12" 197 s/a: as int-ptr! 1000h 198 s/c: s/a + s/b + size ;-- (reg/reg)/ref 199 --assert s/c = as int-ptr! 1014h 200 201 --test-- "math-mixed-13" 202 s/a: as int-ptr! 1000h 203 s/c: s/a + s/b + s/b ;-- (reg/reg)/reg 204 --assert s/c = as int-ptr! 1018h 205 206 --test-- "math-mixed-14" 207 s/a: as int-ptr! 1000h 208 s/a: (fooA s/a) + 2 ;-- reg/imm 209 --assert s/a = as int-ptr! 1008h 210 211 --test-- "math-mixed-15" 212 s/a: as int-ptr! 1000h 213 i: 2 + as-integer fooA s/a ;-- imm/reg 214 --assert i = 1002h 215 216 --test-- "math-mixed-16" 217 s/a: as int-ptr! 1000h 218 s/a: (as int-ptr! 2) + as-integer fooA s/a ;-- imm/reg 219 --assert s/a = as int-ptr! 4002h 220 221 --test-- "math-mixed-17" 222 s/a: (as int-ptr! fooB s/b) + size ;-- reg/ref 223 --assert s/a = as int-ptr! 0Bh 224 225 --test-- "math-mixed-18" 226 i: size + fooB s/b ;-- ref/reg 227 --assert i = 05h 228 229 --test-- "math-mixed-19" 230 s/a: (as int-ptr! size) + fooB s/b ;-- ref/reg 231 --assert s/a = as int-ptr! 0Eh 232 233 --test-- "math-mixed-20" 234 i: (fooB s/b) + size - 1 ;-- (reg/ref)/imm 235 --assert i = 04h 236 237 --test-- "math-mixed-21" 238 i: (fooB s/b) - 1 + size ;-- (reg/imm)/ref 239 --assert i = 04h 240 241 --test-- "math-mixed-22" 242 s/a: as int-ptr! 1000h 243 s/a: (fooA s/a) + size - 1 ;-- (reg/ref)/imm 244 --assert s/a = as int-ptr! 1004h 245 246 --test-- "math-mixed-23" 247 s/a: as int-ptr! 1000h 248 s/a: (fooA s/a) - 1 + size ;-- (reg/imm)/ref 249 --assert s/a = as int-ptr! 1004h 250 251 --test-- "math-mixed-24" 252 s/a: as int-ptr! 1000h 253 s/c: (fooA s/a) + s/b + 2 ;-- (reg/reg)/imm 254 --assert s/c = as int-ptr! 1014h 255 256 --test-- "math-mixed-25" 257 s/a: as int-ptr! 1000h 258 s/c: (fooA s/a) + s/b + size ;-- (reg/reg)/ref 259 --assert s/c = as int-ptr! 1014h 260 261 --test-- "math-mixed-26" 262 s/a: as int-ptr! 1000h 263 s/c: (fooA s/a) + (fooB s/b) + s/b ;-- (reg/reg)/reg 264 --assert s/c = as int-ptr! 1018h 265 266 --test-- "math-mixed-27" 267 s/a: as int-ptr! 1000h 268 s/c: (fooA s/a) + (fooB s/b) + 2 ;-- (reg/reg)/imm 269 --assert s/c = as int-ptr! 1014h 270 271 --test-- "math-mixed-28" 272 s/a: as int-ptr! 1000h 273 s/c: (fooA s/a) + (fooB s/b) + size ;-- (reg/reg)/ref 274 --assert s/c = as int-ptr! 1014h 275 276 --test-- "math-mixed-29" 277 s/a: as int-ptr! 1000h 278 s/c: (fooA s/a) + s/b + (fooB s/b) ;-- (reg/reg)/reg 279 --assert s/c = as int-ptr! 1018h 280 281 --test-- "math-mixed-30" 282 s/a: as int-ptr! 1000h 283 i: s/b + (fooA s/a) + (fooB s/b) ;-- (reg/reg)/reg 284 --assert i = 1006h 285 286 --test-- "math-mixed-31" 287 s/a: as int-ptr! 1000h 288 i: 2 + (fooA s/a) + (fooB s/b) ;-- (imm/reg)/reg 289 --assert i = 1005h 290 291 --test-- "math-mixed-32" 292 s/a: as int-ptr! 1000h 293 i: 2 + (fooA s/a) + size ;-- (imm/reg)/ref 294 --assert i = 1004h 295 296 --test-- "math-mixed-33" 297 s/a: as int-ptr! 1000h 298 i: 2 + (fooA s/a) + 3 ;-- (imm/reg)/imm 299 --assert i = 1005h 300 301 302 --test-- "math-mixed-34" 303 s/a: as int-ptr! 1000h 304 s/a: s/a - 2 ;-- reg/imm 305 --assert s/a = as int-ptr! 0FF8h 306 307 --test-- "math-mixed-35" 308 s/a: as int-ptr! 1000h 309 i: 2000h - as-integer s/a ;-- imm/reg 310 --assert i = 1000h 311 312 --test-- "math-mixed-36" 313 s/a: as int-ptr! 1000h 314 s/a: (as int-ptr! 6000h) - as-integer s/a ;-- imm/reg 315 --assert s/a = as int-ptr! 2000h 316 317 --test-- "math-mixed-37" 318 s/a: as int-ptr! s/b - size ;-- reg/ref 319 --assert s/a = as int-ptr! 01h 320 321 --test-- "math-mixed-38" 322 i: size - s/b ;-- ref/reg 323 --assert i = -1 324 325 --test-- "math-mixed-39" 326 s/a: (as int-ptr! size) - s/b ;-- ref/reg 327 --assert s/a = as int-ptr! -10 328 329 --test-- "math-mixed-40" 330 i: s/b - size - 1 ;-- (reg/ref)/imm 331 --assert i = 0 332 333 --test-- "math-mixed-41" 334 i: s/b - 1 - size ;-- (reg/imm)/ref 335 --assert i = 0 336 337 --test-- "math-mixed-42" 338 s/a: as int-ptr! 1000h 339 s/a: s/a - size - 1 ;-- (reg/ref)/imm 340 --assert s/a = as int-ptr! 0FF4h 341 342 --test-- "math-mixed-43" 343 s/a: as int-ptr! 1000h 344 s/a: s/a - 1 - size ;-- (reg/imm)/ref 345 --assert s/a = as int-ptr! 0FF4h 346 347 --test-- "math-mixed-44" 348 s/a: as int-ptr! 1000h 349 s/c: s/a - s/b - 2 ;-- (reg/reg)/imm 350 --assert s/c = as int-ptr! 0FECh 351 352 --test-- "math-mixed-45" 353 s/a: as int-ptr! 1000h 354 s/c: s/a - s/b - size ;-- (reg/reg)/ref 355 --assert s/c = as int-ptr! 0FECh 356 357 --test-- "math-mixed-46" 358 s/a: as int-ptr! 1000h 359 s/c: s/a - s/b - s/b ;-- (reg/reg)/reg 360 --assert s/c = as int-ptr! 0FE8h 361 362 --test-- "math-mixed-47" 363 s/a: as int-ptr! 1000h 364 s/a: (fooA s/a) - 2 ;-- reg/imm 365 --assert s/a = as int-ptr! 0FF8h 366 367 --test-- "math-mixed-48" 368 s/a: as int-ptr! 1000h 369 i: 2 - as-integer fooA s/a ;-- imm/reg 370 --assert i = FFFFF002h 371 372 --test-- "math-mixed-49" 373 s/a: as int-ptr! 1000h 374 s/a: (as int-ptr! 2) - as-integer fooA s/a ;-- imm/reg 375 --assert s/a = as int-ptr! FFFFC002h 376 377 --test-- "math-mixed-50" 378 s/a: (as int-ptr! fooB s/b) - size ;-- reg/ref 379 --assert s/a = as int-ptr! -5 380 381 --test-- "math-mixed-51" 382 i: size - fooB s/b ;-- ref/reg 383 --assert i = -1 384 385 --test-- "math-mixed-52" 386 s/a: (as int-ptr! size) - fooB s/b ;-- ref/reg 387 --assert s/a = as int-ptr! -10 388 389 --test-- "math-mixed-53" 390 i: (fooB s/b) - size - 1 ;-- (reg/ref)/imm 391 --assert i = 0 392 393 --test-- "math-mixed-54" 394 i: (fooB s/b) - 1 - size ;-- (reg/imm)/ref 395 --assert i = 0 396 397 --test-- "math-mixed-55" 398 s/a: as int-ptr! 1000h 399 s/a: (fooA s/a) - size - 1 ;-- (reg/ref)/imm 400 --assert s/a = as int-ptr! 0FF4h 401 402 --test-- "math-mixed-56" 403 s/a: as int-ptr! 1000h 404 s/a: (fooA s/a) - 1 - size ;-- (reg/imm)/ref 405 --assert s/a = as int-ptr! 0FF4h 406 407 --test-- "math-mixed-57" 408 s/a: as int-ptr! 1000h 409 s/c: (fooA s/a) - s/b - 2 ;-- (reg/reg)/imm 410 --assert s/c = as int-ptr! 0FECh 411 412 --test-- "math-mixed-58" 413 s/a: as int-ptr! 1000h 414 s/c: (fooA s/a) - s/b - size ;-- (reg/reg)/ref 415 --assert s/c = as int-ptr! 0FECh 416 417 --test-- "math-mixed-59" 418 s/a: as int-ptr! 1000h 419 s/c: (fooA s/a) - (fooB s/b) - s/b ;-- (reg/reg)/reg 420 --assert s/c = as int-ptr! 0FE8h 421 422 --test-- "math-mixed-60" 423 s/a: as int-ptr! 1000h 424 s/c: (fooA s/a) - (fooB s/b) - 2 ;-- (reg/reg)/imm 425 --assert s/c = as int-ptr! 0FECh 426 427 --test-- "math-mixed-61" 428 s/a: as int-ptr! 1000h 429 s/c: (fooA s/a) - (fooB s/b) - size ;-- (reg/reg)/ref 430 --assert s/c = as int-ptr! 0FECh 431 432 --test-- "math-mixed-62" 433 s/a: as int-ptr! 1000h 434 s/c: (fooA s/a) - s/b - (fooB s/b) ;-- (reg/reg)/reg 435 --assert s/c = as int-ptr! 0FE8h 436 437 --test-- "math-mixed-63" 438 s/a: as int-ptr! 1000h 439 i: s/b - (fooA s/a) - (fooB s/b) ;-- (reg/reg)/reg 440 --assert i = FFFFF000h 441 442 --test-- "math-mixed-64" 443 s/a: as int-ptr! 1000h 444 i: 2 - (fooA s/a) - (fooB s/b) ;-- (imm/reg)/reg 445 --assert i = FFFFEFFFh 446 447 --test-- "math-mixed-65" 448 s/a: as int-ptr! 1000h 449 i: 2 - (fooA s/a) - size ;-- (imm/reg)/ref 450 --assert i = FFFFF000h 451 452 --test-- "math-mixed-66" 453 s/a: as int-ptr! 1000h 454 i: 2 - (fooA s/a) - 3 ;-- (imm/reg)/imm 455 --assert i = FFFFEFFFh 456 457 --test-- "math-mixed-67" 458 s/a: as int-ptr! 1000h 459 s/c: as int-ptr! 1010h 460 i: as-integer s/c - s/a ;-- reg/reg (pointer - pointer) 461 --assert i = 10h 462 463 --test-- "math-mixed-68" 464 s/a: as int-ptr! 1000h 465 s/c: as int-ptr! 1010h 466 i: as-integer (s/c - s/a) + 1 ;-- reg/reg (pointer - pointer) 467 --assert i = 14h 468 469 --test-- "math-mixed-69" 470 s/a: as int-ptr! 00154E28h 471 s/c: as int-ptr! 00150018h 472 i: (as-integer (s/a - s/c) + 1) / 4 ;-- reg/reg (pointer - pointer) 473 --assert i = 4997 474 475 --test-- "math-mixed-70" 476 p: declare struct! [ 477 a [byte-ptr!] 478 c [int-ptr!] 479 ] 480 p/a: as byte-ptr! 001F0014h 481 p/c: as int-ptr! 001D0004h 482 i: as-integer (p/a - ((as byte-ptr! p/c) + 16)) ;-- reg/(reg/imm) 483 --assert i = 00020000h 484 485 --test-- "math-mixed-71" 486 p/a: as byte-ptr! 001F0014h 487 p/c: as int-ptr! 001D0014h 488 series: p/c 489 sz: 32 490 --assert ((as byte-ptr! series) + sz) < p/a ;-- (ref/ref)/reg 491 492===end-group=== 493 494~~~end-file~~~ 495