Skip to content
This repository
branch: nqp-p6
Fetching contributors…

Cannot retrieve contributors at this time

file 682 lines (553 sloc) 12.859 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
#!./parrot
# Copyright (C) 2008-2010, Parrot Foundation.

=head1 NAME

t/dynpmc/rational.t - Rational PMC

=head1 SYNOPSIS

% prove t/dynpmc/rational.t

=head1 DESCRIPTION

Tests the Rational PMC.

=cut

.sub main :main
    .include 'test_more.pir'
    .include 'iglobals.pasm'
    .local pmc config_hash, interp

    interp = getinterp
    config_hash = interp[.IGLOBALS_CONFIG_HASH]
    $S0 = config_hash['gmp']

    unless $S0 goto no_gmp
    plan(79)
    loadlib $P1, 'rational'
    test_init()
    test_destroy()
    test_version()
    test_set_get_native_int()
    test_set_get_native_float()
    test_set_get_native_string()

    test_set_get_int()
    test_set_get_float()
    test_set_get_string()
    test_set_get_string_keyed()

    test_get_bool()

    test_inc_dec()

    test_add_int_inplace()
    test_add_float_inplace()

    test_add_int_pmc_inplace()
    test_add_float_pmc_inplace()
    test_add_rats_inplace()

    test_subtract_int()
    test_subtract_float()
    test_subtract_int_pmc()
    test_subtract_float_pmc()
    test_subtract_rats()

    test_multiply_int()
    test_multiply_float()
    test_multiply_int_pmc()
    test_multiply_float_pmc()
    test_multiply_rats()

    test_divide_int()
    test_divide_float()
    test_divide_int_pmc()
    test_divide_float_pmc()
    test_divide_rats()

    test_defaults()

    test_neg()
    test_abs()
    test_cmp()
    test_equal()
    test_equal_int()
    .return()

 no_gmp:
    skip_all('GMP not found, skipping Rational tests')
    .return()
.end

.sub test_neg
    new $P2, 'Rational'
    new $P3, 'Rational'

    $P2 = "-3/2"
    $P3 = -$P2
    neg $P2

    is($P2,'3/2','neg')
    is($P3,'3/2','neg')
.end

.sub test_abs
    new $P2, 'Rational'
    new $P3, 'Rational'

    $P2 = "-3/2"
    $P3 = abs $P2
    abs $P2
    is($P2,'3/2','abs')
    is($P3,'3/2','abs')
.end

.sub test_equal
    new $P2, 'Rational'
    new $P3, 'Integer'
    $P2 = "2/1"
    $P3 = 2
    is($P2, $P3, '== on Rational and Integer PMC')

    throws_substring(<<"CODE", "epsilon", "== on Rational and Float PMC throws exception")
    .sub main
        $P0 = new ['Rational']
        $P1 = new ['Float']
        $P0 = "2/1"
        $P1 = 2.
        if $P0 == $P1 goto fail
    fail:
        ok(0, '== on Rational and Float PMC')
    .end
CODE

    throws_substring(<<"CODE", "is_equal not implemented (yet).", "== on Rational and String PMC throws exception")
    .sub main
        $P0 = new ['Rational']
        $P1 = new ['String']
        $P0 = "2/1"
        $P1 = "foo"
        if $P0 == $P1 goto fail
    fail:
        ok(0, '== on Rational and String PMC')
    .end
CODE
.end

.sub test_equal_int
    $P0 = new ['Rational']
    $P0 = "2/1"

    eq $P0, 2, success
  success:
    ok(1, 'is_equal on Rational and integer')
.end

.sub test_cmp
    new $P2, 'Rational'
    new $P3, 'Rational'

    $P2 = "3/2"
    $P3 = "6/4"

    is($P2, $P3,'== on Rational PMC')

    $P3 = "7/4"
    cmp $I1, $P2, $P3
    cmp $I2, $P3, $P2
    is($I1,-1,'cmp on Rational PMC')
    is($I2,1,'cmp on Rational PMC')

    $P4 = new ['Integer']
    $P4 = -1
    cmp $I1, $P2, $P4
    is($I1, 1, 'cmp with Integer')

    $P4 = 100
    cmp $I1, $P2, $P4
    is($I1, -1, 'cmp with Integer')

    $P4 = new ['Float']
    $P4 = -1.0
    push_eh eh1
    cmp $I1, $P2, $P4
    is($I1, -1, 'cmp with Float')
    goto finally1
eh1:
    todo(0, "cmp with Float not implemented")
finally1:
    pop_eh

    $P4 = 100.0
    push_eh eh2
    cmp $I1, $P2, $P4
    is($I1, 1, 'cmp with Float')
    goto finally2
eh2:
    todo(0, "cmp with Float not implemented")
finally2:
    pop_eh

    throws_substring(<<"CODE", "cmp not implemented (yet).", "cmp with default")
    .sub main
        $P0 = new ['Rational']
        $P0 = "1"

        $P1 = new ['String']
        $P1 = 'foo'
        cmp $I1, $P0, $P1
    .end
CODE
.end

.sub test_divide_int
    new $P1, 'Rational'
    new $P2, 'Rational'
    $I1 = 7

    $P1 = "3/2"
    $P2 = $P1 / $I1
    div $P1, $I1
    is($P1,'3/14','divide int inplace')
    is($P2,'3/14','divide int')
.end

.sub test_divide_float
    new $P1, 'Rational'
    new $P2, 'Rational'
    $N1 = 7.

    $P1 = "3/2"
    $P2 = $P1 / $N1
    div $P1, $N1
    is($P1,'3/14','divide float inplace')
    is($P2,'3/14','divide float')

.end

.sub test_divide_int_pmc
    new $P2, 'Rational'
    new $P3, 'Rational'
    new $P4, 'Integer'

    $P4 = 7

    $P2 = "3/2"
    $P3 = $P2 / $P4
    div $P2, $P4
    is($P2,'3/14','divide Integer PMC inplace')
    is($P3,'3/14','divide Integer PMC')
.end

.sub test_divide_float_pmc
    new $P2, 'Rational'
    new $P3, 'Rational'
    new $P4, 'Float'

    $P4 = 7.

    $P2 = "3/2"
    $P3 = $P2 / $P4
    div $P2, $P4
    is($P2,'3/14','divide Float PMC inplace')
    is($P3,'3/14','divide Float PMC')
.end

.sub test_divide_rats
    new $P1, 'Rational'
    new $P2, 'Rational'
    new $P3, 'Rational'

    $P2 = "3/2"
    $P3 = "5/2"

    $P1 = $P2 / $P3
    div $P2, $P3
    is($P1,'3/5','divide Rational PMC')
    is($P2,'3/5','divide Rational PMC inplace')
.end

.sub test_multiply_int
    new $P1, 'Rational'
    new $P2, 'Rational'
    $I1 = 7

    $P1 = "3/2"
    $P2 = $P1 * $I1
    mul $P1, $I1
    is($P1,'21/2','multiply int inplace')
    is($P2,'21/2','multiply int')
.end

.sub test_multiply_float
    new $P1, 'Rational'
    new $P2, 'Rational'
    $N1 = 7.

    $P1 = "3/2"
    $P2 = $P1 * $N1
    mul $P1, $N1
    is($P1,'21/2','multiply float inplace')
    is($P2,'21/2','multiply float')
.end

.sub test_multiply_int_pmc
    new $P2, 'Rational'
    new $P3, 'Rational'
    new $P4, 'Integer'

    $P4 = 7

    $P2 = "3/2"
    $P3 = $P2 * $P4
    mul $P2, $P4
    is($P2,'21/2','multiply Integer PMC inplace')
    is($P3,'21/2','multiply Integer PMC')
.end

.sub test_multiply_float_pmc
    new $P2, 'Rational'
    new $P3, 'Rational'
    new $P4, 'Float'

    $P4 = 7.

    $P2 = "3/2"
    $P3 = $P2 * $P4
    mul $P2, $P4
    is($P2,'21/2','multiply Float PMC inplace')
    is($P3,'21/2','multiply Float PMC')

.end

.sub test_multiply_rats
    new $P1, 'Rational'
    new $P2, 'Rational'
    new $P3, 'Rational'

    $P2 = "3/2"
    $P3 = "5/2"

    $P1 = $P2 * $P3
    mul $P2, $P3
    is($P1,'15/4','multiply Rational PMC')
    is($P2,'15/4','multiply Rational PMC inplace')
.end

.sub test_subtract_rats
    new $P1, 'Rational'
    new $P2, 'Rational'
    new $P3, 'Rational'

    $P2 = "3/2"
    $P3 = "5/2"

    $P1 = $P2 - $P3
    sub $P2, $P3
    is($P1,-1,'subtract Rational')
    is($P2,-1,'subtract Rational inplace')

.end

.sub test_subtract_int
    new $P1, 'Rational'
    new $P2, 'Rational'
    $I1 = 7

    $P1 = "3/2"
    $P2 = $P1 - $I1
    sub $P1, $I1
    sub $P1, $I1
    is($P1,'-25/2','subtract int inplace')
    is($P2,'-11/2','subtract int')
.end

.sub test_subtract_float
    new $P1, 'Rational'
    new $P2, 'Rational'
    $N1 = 7.

    $P1 = "3/2"
    $P2 = $P1 - $N1
    sub $P1, $N1
    sub $P1, $N1
    is($P1,'-25/2','subtract float inplace')
    is($P2,'-11/2','subtract float')
.end

.sub test_subtract_float_pmc
    new $P2, 'Rational'
    new $P3, 'Rational'
    new $P4, 'Float'

    $P4 = 7.

    $P2 = "3/2"
    $P3 = $P2 - $P4
    sub $P2, $P4
    is($P2,'-11/2','subtract Float PMC inplace')
    is($P3,'-11/2','subtract Float PMC')
.end

.sub test_subtract_int_pmc
    new $P2, 'Rational'
    new $P3, 'Rational'
    new $P4, 'Integer'

    $P4 = 7

    $P2 = "3/2"
    $P3 = $P2 - $P4
    sub $P2, $P4
    is($P2,'-11/2','subtract Integer PMC inplace')
    is($P3,'-11/2','subtract Integer PMC')
.end

.sub test_add_rats_inplace
    new $P1, 'Rational'
    new $P2, 'Rational'
    new $P3, 'Rational'

    $P2 = "3/2"
    $P3 = "5/2"

    $P1 = $P2 + $P3
    add $P2, $P3
    is($P1,4,'adding rationals')
    is($P2,4,'adding rationals inplace')
.end

.sub test_add_int_pmc_inplace
    new $P2, 'Rational'
    new $P3, 'Rational'
    new $P4, 'Integer'

    $P4 = 7

    $P2 = "3/2"
    $P3 = $P2 + $P4
    add $P2, $P4
    is($P2,'17/2','add Integer PMCs inplace')
    is($P3,'17/2','add Integer PMCs')
.end

.sub test_add_float_pmc_inplace
    new $P2, 'Rational'
    new $P3, 'Rational'
    new $P4, 'Float'

    $P4 = 7.

    $P2 = "3/2"
    $P3 = $P2 + $P4
    add $P2, $P4
    is($P2,'17/2','add Float PMCs inplace')
    is($P3,'17/2','add Float PMCs')
.end

.sub test_add_int_inplace
    new $P1, 'Rational'
    new $P2, 'Rational'
    $I1 = 7

    $P1 = "3/2"
    $P2 = $P1 + $I1
    add $P1, $I1
    add $P1, $I1
    is($P1,'31/2','add integers inplace')
    is($P2,'17/2','add integers')
.end

.sub test_add_float_inplace
    new $P1, 'Rational'
    new $P2, 'Rational'
    $N1 = 7.

    $P1 = "3/2"
    $P2 = $P1 + $N1
    add $P1, $N1
    add $P1, $N1
    is($P1,'31/2','add floats inplace')
    is($P2,'17/2','add floats')
.end

.sub test_defaults
    throws_substring(<<"CODE", "Rational, add: Not implemented (yet).", "add string")
    .sub main
        $P0 = new ['Rational']
        $P0 = "1/2"
        $P1 = new ['String']
        $P1 = "foo"

        $P2 = $P0 + $P1
    .end
CODE
    throws_substring(<<"CODE", "Rational, subtract: Not implemented (yet).", "sub string")
    .sub main
        $P0 = new ['Rational']
        $P0 = "1/2"
        $P1 = new ['String']
        $P1 = "foo"

        $P2 = $P0 - $P1
    .end
CODE
    throws_substring(<<"CODE", "Rational, multiply: Not implemented (yet).", "mul string")
    .sub main
        $P0 = new ['Rational']
        $P0 = "1/2"
        $P1 = new ['String']
        $P1 = "foo"

        $P2 = $P0 * $P1
    .end
CODE
    throws_substring(<<"CODE", "Rational, divide: Not implemented (yet).", "div string")
    .sub main
        $P0 = new ['Rational']
        $P0 = "1/2"
        $P1 = new ['String']
        $P1 = "foo"

        $P2 = $P0 / $P1
    .end
CODE
    throws_substring(<<"CODE", "Rational, i_add: Not implemented (yet).", "i_add string")
    .sub main
        $P0 = new ['Rational']
        $P0 = "1/2"
        $P1 = new ['String']
        $P1 = "foo"

        add $P0, $P1
    .end
CODE
    throws_substring(<<"CODE", "Rational, i_subtract: Not implemented (yet).", "i_sub string")
    .sub main
        $P0 = new ['Rational']
        $P0 = "1/2"
        $P1 = new ['String']
        $P1 = "foo"

        sub $P0, $P1
    .end
CODE
    throws_substring(<<"CODE", "Rational, i_multiply: Not implemented (yet).", "i_mul string")
    .sub main
        $P0 = new ['Rational']
        $P0 = "1/2"
        $P1 = new ['String']
        $P1 = "foo"

        mul $P0, $P1
    .end
CODE
    throws_substring(<<"CODE", "Rational, i_divide: Not implemented (yet).", "i_div string")
    .sub main
        $P0 = new ['Rational']
        $P0 = "1/2"
        $P1 = new ['String']
        $P1 = "foo"

        div $P0, $P1
    .end
CODE
.end

.sub test_init
    new $P1, 'Rational'
    is($P1, 0, 'initialization')
.end

.sub test_destroy
    new $P1, 'Rational'
    null $P1
    sweep 1
    ok(1, 'destroy')
.end

.sub test_version
    new $P1, 'Rational'
    $S1 = $P1.'version'()
    ok($S1,'can get version number')
.end

.sub test_set_get_native_int
    new $P1, 'Rational'

    $I1 = 42
    $P1 = $I1
    $I2 = $P1
    is($I2,42,'set and get native int')
.end

.sub test_set_get_int
    new $P1, 'Rational'
    new $P2, 'Integer'
    new $P3, 'Integer'

    $P2 = 7
    $P1 = $P2
    $P3 = $P1
    is($P3,7,'set and get int')

    throws_substring(<<"CODE", "Number is too big", "int too long overflows")
    .sub main
        $P0 = new ['Rational']
        $P0 = "10000000000000000000"
        $I0 = $P0
    .end
CODE
.end

.sub test_set_get_float
    new $P1, 'Rational'

    new $P2, 'Float'
    new $P3, 'Float'

    $P2 = 7.110000
    $P1 = $P2
    $P3 = $P1
    is($P3,7.11,'set and set float',0.0001)
.end

.sub test_inc_dec
    new $P1, 'Rational'

    $P1 = "7/4"
    inc $P1
    is($P1,'11/4','increment a rational')
    dec $P1
    dec $P1
    is($P1,'3/4','decrement a rational')
.end

.sub test_set_get_string
    new $P1, 'Rational'
    new $P2, 'String'
    new $P3, 'String'

    $P2 = "7/4"
    $P1 = $P2
    $P3 = $P1
    is($P3,"7/4",'set and get string')
.end

.sub test_set_get_native_float
    new $P0, 'Rational'

    $N0 = 11.1
    $P0 = $N0
    $N1 = $P0
    is($N1,11.1,'set and get a native float')
.end

.sub test_set_get_native_string
    new $P1, 'Rational'

    $S1 = "7/4"
    $P1 = $S1
    $S2 = $P1
    is($S2,'7/4','set and get native string')
.end

.sub test_set_get_string_keyed
    new $P1, 'Rational'

    $S1 = "a/b"
    $P1[16] = $S1
    $S2 = $P1[8]

    is($S2, '12/13', 'set and get string keyed')
.end

.sub test_get_bool
    new $P0, 'Rational'
    $P0 = "0"
    $I0 = isfalse $P0
    ok($I0, '0 should be false')
    $P0 = "3/4"
    $I0 = istrue $P0
    ok($I0, '3/4 should be true')
.end

# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir:
Something went wrong with that request. Please try again.