@@ -59,6 +59,41 @@ describe "BigInt" do
5959 BigInt .new(12.3 ).to_s.should eq(" 12" )
6060 end
6161
62+ describe " .from_digits" do
63+ it " creates from digits" do
64+ BigInt .from_digits([] of Int32 ).should eq(0 .to_big_i)
65+ BigInt .from_digits([3 , 2 , 1 ]).should eq(123 .to_big_i)
66+ BigInt .from_digits({3 , 2 , 1 }, base: 16 ).should eq(0x123 .to_big_i)
67+ BigInt .from_digits((0xfa ..0xff ), base: 256 ).should eq(0xfffefdfcfbfa .to_big_i)
68+ BigInt .from_digits([UInt128 ::MAX , UInt128 ::MAX , UInt128 ::MAX , UInt128 ::MAX ], base: 2 .to_big_i ** 128 ).should eq(2 .to_big_i ** 512 - 1 )
69+ BigInt .from_digits(Deque {1 , 0 .to_big_i, 0 , 1 .to_big_i}, base: 2 ).should eq(9 .to_big_i)
70+ end
71+
72+ it " raises for base less than 2" do
73+ [-1 , 0 , 1 ].each do |base |
74+ expect_raises(ArgumentError , " Invalid base #{ base } " ) do
75+ BigInt .from_digits([1 , 2 , 3 ], base)
76+ end
77+ end
78+ end
79+
80+ it " raises for digits greater than base" do
81+ expect_raises(ArgumentError , " Invalid digit 2 for base 2" ) do
82+ BigInt .from_digits([1 , 0 , 2 ], 2 )
83+ end
84+
85+ expect_raises(ArgumentError , " Invalid digit 10 for base 2" ) do
86+ BigInt .from_digits([1 , 0 , 10 ], 2 )
87+ end
88+ end
89+
90+ it " raises for negative digits" do
91+ expect_raises(ArgumentError , " Invalid digit -1" ) do
92+ BigInt .from_digits([1 , 2 , -1 ])
93+ end
94+ end
95+ end
96+
6297 it " compares" do
6398 1 .to_big_i.should eq(1 .to_big_i)
6499 1 .to_big_i.should eq(1 )
0 commit comments