@@ -633,60 +633,109 @@ class TestBignum : public CxxTest::TestSuite, public VMTest {
633
633
check_float (f, Float::create (state, -880872.999999925 ));
634
634
}
635
635
636
- void test_left_shift () {
636
+ void test_positive_left_shift_positive () {
637
637
Bignum* one = Bignum::from (state, 1 );
638
+
639
+ Fixnum* max_width = Fixnum::from (FIXNUM_MAX_WIDTH);
640
+ Integer* fix = one->left_shift (state, max_width);
641
+
642
+ TS_ASSERT (kind_of<Fixnum>(fix));
643
+ TS_ASSERT_EQUALS (1L << (FIXNUM_MAX_WIDTH), fix->to_native ());
644
+
645
+ Integer* max_plus1 = one->left_shift (state, max_width+1 );
646
+
647
+ TS_ASSERT (kind_of<Bignum>(max_plus1));
648
+ TS_ASSERT_EQUALS (FIXNUM_MAX + 1 , max_plus1->to_native ());
649
+ }
650
+
651
+ void test_positive_left_shift_negative () {
652
+ Bignum* max_plus1 = Bignum::from (state, FIXNUM_MAX+1 );
653
+ Fixnum* neg_one = Fixnum::from (-1 );
654
+
655
+ Integer* val = max_plus1->left_shift (state, neg_one);
656
+
657
+ TS_ASSERT (kind_of<Fixnum>(val));
658
+ TS_ASSERT_EQUALS (1L << (FIXNUM_MAX_WIDTH), val->to_native ());
659
+ }
660
+
661
+ void test_negative_left_shift_positive () {
638
662
Bignum* neg_one = Bignum::from (state, -1 );
663
+ Fixnum* min_width = Fixnum::from (FIXNUM_MIN_WIDTH);
639
664
640
- Fixnum* width_minus1 = Fixnum::from (FIXNUM_WIDTH-1 );
641
- Fixnum* width = Fixnum::from (FIXNUM_WIDTH);
665
+ Integer* val = neg_one->left_shift (state, min_width);
642
666
643
- Integer* fix = one->left_shift (state, width_minus1);
667
+ TS_ASSERT (kind_of<Fixnum>(val));
668
+ TS_ASSERT_EQUALS ((0UL - 1L ) << (FIXNUM_MIN_WIDTH), val->to_native ());
644
669
645
- TS_ASSERT (kind_of<Fixnum>(fix));
646
- TS_ASSERT_EQUALS (1L << (FIXNUM_WIDTH-1 ), fix->to_native ());
670
+ val = neg_one->left_shift (state, min_width+1 );
647
671
648
- fix = neg_one->left_shift (state, width_minus1);
672
+ TS_ASSERT (kind_of<Bignum>(val));
673
+ TS_ASSERT_EQUALS (FIXNUM_MIN-1 , val->to_native ());
674
+ }
649
675
650
- TS_ASSERT (kind_of<Fixnum>(fix));
651
- TS_ASSERT_EQUALS ((0UL - 1L ) << (FIXNUM_WIDTH-1 ), fix->to_native ());
676
+ void test_negative_left_shift_negative () {
677
+ Fixnum* neg_one = Fixnum::from (-1 );
678
+ Bignum* min_minus1 = Bignum::from (state, FIXNUM_MIN-1 );
652
679
653
- Integer* max_plus1 = one ->left_shift (state, width );
680
+ Integer* val = min_minus1 ->left_shift (state, neg_one );
654
681
655
- TS_ASSERT (kind_of<Bignum>(max_plus1));
656
- TS_ASSERT_EQUALS (FIXNUM_MAX + 1 , max_plus1->to_native ());
682
+ TS_ASSERT (kind_of<Fixnum>(val));
683
+ TS_ASSERT_EQUALS ((0UL - 1L ) << (FIXNUM_MIN_WIDTH), val->to_native ());
684
+ }
685
+
686
+ void test_positive_right_shift_positive () {
687
+ Fixnum* one = Fixnum::from (1 );
657
688
658
- Integer* min_minus1 = neg_one->left_shift (state, width);
689
+ Bignum* max_plus1 = Bignum::from (state, FIXNUM_MAX+1 );
690
+ Integer* val = max_plus1->right_shift (state, one);
659
691
660
- TS_ASSERT (kind_of<Bignum>(min_minus1 ));
661
- TS_ASSERT_EQUALS (FIXNUM_MIN - 1 , min_minus1 ->to_native ());
692
+ TS_ASSERT (kind_of<Fixnum>(val ));
693
+ TS_ASSERT_EQUALS (1L << (FIXNUM_MAX_WIDTH), val ->to_native ());
662
694
}
663
695
664
- void test_right_shift () {
696
+ void test_positive_right_shift_negative () {
697
+ Fixnum* neg_one = Fixnum::from (-1 );
698
+
699
+ Bignum* big = Bignum::from (state, 1L << FIXNUM_MAX_WIDTH);
700
+ Integer* val = big->right_shift (state, neg_one);
701
+
702
+ TS_ASSERT (kind_of<Bignum>(val));
703
+ TS_ASSERT_EQUALS (FIXNUM_MAX+1 , val->to_native ());
704
+
665
705
Bignum* one = Bignum::from (state, 1 );
666
- Bignum* neg_one = Bignum ::from (state, - 1 );
706
+ Fixnum* neg_max_width = Fixnum ::from (FIXNUM_MAX_WIDTH );
667
707
668
- Fixnum* neg_width_minus1 = Fixnum::from (-(FIXNUM_WIDTH-1 ));
669
- Fixnum* neg_width = Fixnum::from (-FIXNUM_WIDTH);
708
+ val = one->right_shift (state, neg_max_width);
670
709
671
- Integer* fix = one->right_shift (state, neg_width_minus1);
710
+ TS_ASSERT (kind_of<Fixnum>(val));
711
+ TS_ASSERT_EQUALS (1L << FIXNUM_MAX_WIDTH, val->to_native ());
712
+ }
672
713
673
- TS_ASSERT (kind_of<Fixnum>(fix));
674
- TS_ASSERT_EQUALS (1L << (FIXNUM_WIDTH-1 ), fix->to_native ());
714
+ void test_negative_right_shift_positive () {
715
+ Bignum* min_minus1 = Bignum::from (state, FIXNUM_MIN-1 );
716
+ Fixnum* one = Fixnum::from (1 );
675
717
676
- fix = neg_one ->right_shift (state, neg_width_minus1 );
718
+ Integer* val = min_minus1 ->right_shift (state, one );
677
719
678
- TS_ASSERT (kind_of<Fixnum>(fix));
679
- TS_ASSERT_EQUALS ((0UL - 1L ) << (FIXNUM_WIDTH-1 ), fix->to_native ());
720
+ TS_ASSERT (kind_of<Fixnum>(val));
721
+ TS_ASSERT_EQUALS ((0UL - 1L ) << FIXNUM_MIN_WIDTH, val->to_native ());
722
+ }
680
723
681
- Integer* max_plus1 = one->right_shift (state, neg_width);
724
+ void test_negative_right_shift_negative () {
725
+ Bignum* neg_one = Bignum::from (state, -1 );
726
+ Fixnum* neg_min_width = Fixnum::from (-FIXNUM_MIN_WIDTH);
682
727
683
- TS_ASSERT (kind_of<Bignum>(max_plus1));
684
- TS_ASSERT_EQUALS (FIXNUM_MAX + 1 , max_plus1->to_native ());
728
+ Integer* val = neg_one->right_shift (state, neg_min_width);
685
729
686
- Integer* min_minus1 = neg_one->right_shift (state, neg_width);
730
+ TS_ASSERT (kind_of<Fixnum>(val));
731
+ TS_ASSERT_EQUALS ((0UL - 1L ) << FIXNUM_MIN_WIDTH, val->to_native ());
687
732
688
- TS_ASSERT (kind_of<Bignum>(min_minus1));
689
- TS_ASSERT_EQUALS (FIXNUM_MIN - 1 , min_minus1->to_native ());
733
+ Fixnum* neg_min_width_plus1 = Fixnum::from (-(FIXNUM_MIN_WIDTH+1 ));
734
+
735
+ val = neg_one->right_shift (state, neg_min_width_plus1);
736
+
737
+ TS_ASSERT (kind_of<Bignum>(val));
738
+ TS_ASSERT_EQUALS (FIXNUM_MIN-1 , val->to_native ());
690
739
}
691
740
692
741
void test_pow () {
0 commit comments