@@ -146,6 +146,102 @@ describe "Int" do
146146 end
147147 end
148148
149+ describe " #to_signed" do
150+ {% for n in [8 , 16 , 32 , 64 , 128 ] % }
151+ it " does for Int{{n}}" do
152+ x = Int {{n}}.new(123 ).to_signed
153+ x.should be_a(Int {{n}})
154+ x.should eq(123 )
155+
156+ Int {{n}}.new(-123 ).to_signed.should eq(-123 )
157+ Int {{n}}::MIN .to_signed.should eq(Int {{n}}::MIN )
158+ Int {{n}}::MAX .to_signed.should eq(Int {{n}}::MAX )
159+ end
160+
161+ it " does for UInt{{n}}" do
162+ x = UInt {{n}}.new(123 ).to_signed
163+ x.should be_a(Int {{n}})
164+ x.should eq(123 )
165+
166+ UInt {{n}}::MIN .to_signed.should eq(0 )
167+ expect_raises(OverflowError ) { UInt {{n}}::MAX .to_signed }
168+ expect_raises(OverflowError ) { (UInt {{n}}.new(Int {{n}}::MAX ) + 1 ).to_signed }
169+ end
170+ {% end % }
171+ end
172+
173+ describe " #to_signed!" do
174+ {% for n in [8 , 16 , 32 , 64 , 128 ] % }
175+ it " does for Int{{n}}" do
176+ x = Int {{n}}.new(123 ).to_signed!
177+ x.should be_a(Int {{n}})
178+ x.should eq(123 )
179+
180+ Int {{n}}.new(-123 ).to_signed!.should eq(-123 )
181+ Int {{n}}::MIN .to_signed!.should eq(Int {{n}}::MIN )
182+ Int {{n}}::MAX .to_signed!.should eq(Int {{n}}::MAX )
183+ end
184+
185+ it " does for UInt{{n}}" do
186+ x = UInt {{n}}.new(123 ).to_signed!
187+ x.should be_a(Int {{n}})
188+ x.should eq(123 )
189+
190+ UInt {{n}}::MIN .to_signed!.should eq(0 )
191+ UInt {{n}}::MAX .to_signed!.should eq(-1 )
192+ (UInt {{n}}::MAX - 122 ).to_signed!.should eq(-123 )
193+ (UInt {{n}}.new(Int {{n}}::MAX ) + 1 ).to_signed!.should eq(Int {{n}}::MIN )
194+ end
195+ {% end % }
196+ end
197+
198+ describe " #to_unsigned" do
199+ {% for n in [8 , 16 , 32 , 64 , 128 ] % }
200+ it " does for Int{{n}}" do
201+ x = Int {{n}}.new(123 ).to_unsigned
202+ x.should be_a(UInt {{n}})
203+ x.should eq(123 )
204+
205+ Int {{n}}.zero.to_unsigned.should eq(UInt {{n}}::MIN )
206+ Int {{n}}::MAX .to_unsigned.should eq(UInt {{n}}.new(Int {{n}}::MAX ))
207+ expect_raises(OverflowError ) { Int {{n}}::MIN .to_unsigned }
208+ end
209+
210+ it " does for UInt{{n}}" do
211+ x = UInt {{n}}.new(123 ).to_unsigned
212+ x.should be_a(UInt {{n}})
213+ x.should eq(123 )
214+
215+ UInt {{n}}::MIN .to_unsigned.should eq(UInt {{n}}::MIN )
216+ UInt {{n}}::MAX .to_unsigned.should eq(UInt {{n}}::MAX )
217+ end
218+ {% end % }
219+ end
220+
221+ describe " #to_unsigned!" do
222+ {% for n in [8 , 16 , 32 , 64 , 128 ] % }
223+ it " does for Int{{n}}" do
224+ x = Int {{n}}.new(123 ).to_unsigned!
225+ x.should be_a(UInt {{n}})
226+ x.should eq(123 )
227+
228+ Int {{n}}.new(-123 ).to_unsigned!.should eq(UInt {{n}}::MAX - 122 )
229+ Int {{n}}::MIN .to_unsigned!.should eq(UInt {{n}}::MAX // 2 + 1 )
230+ Int {{n}}::MAX .to_unsigned!.should eq(UInt {{n}}::MAX // 2 )
231+ Int {{n}}.new(-1 ).to_unsigned!.should eq(UInt {{n}}::MAX )
232+ end
233+
234+ it " does for UInt{{n}}" do
235+ x = UInt {{n}}.new(123 ).to_unsigned!
236+ x.should be_a(UInt {{n}})
237+ x.should eq(123 )
238+
239+ UInt {{n}}::MIN .to_unsigned!.should eq(UInt {{n}}::MIN )
240+ UInt {{n}}::MAX .to_unsigned!.should eq(UInt {{n}}::MAX )
241+ end
242+ {% end % }
243+ end
244+
149245 describe " #abs_unsigned" do
150246 {% for int in Int ::Signed .union_types % }
151247 it " does for {{ int }}" do
0 commit comments