number 0.9.11 → 0.9.12

Sign up to get free protection for your applications and to get access to all the features.
File without changes
@@ -0,0 +1,10 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright © 2010-2011 Jesse Sielaff
4
+ #
5
+
6
+ require 'rake/testtask'
7
+
8
+ Rake::TestTask.new do |t|
9
+ t.pattern = 'test/test_*.rb'
10
+ end
@@ -194,8 +194,8 @@ void complex_print (char* buf, Complex* complex)
194
194
  }
195
195
 
196
196
  /*
197
- * Puts a string representation of real as a truncated integer into the string
198
- * pointed to by buf.
197
+ * Puts a string representation of the real part of complex as a truncated
198
+ * integer into the string pointed to by buf.
199
199
  */
200
200
  void complex_print_int (char* buf, Complex* complex)
201
201
  {
@@ -220,8 +220,8 @@ void complex_print_int (char* buf, Complex* complex)
220
220
  }
221
221
 
222
222
  /*
223
- * Puts a string representation of real as a fractional decimal into the string
224
- * pointed to by buf.
223
+ * Puts a string representation of the real part of complex as a fractional
224
+ * decimal into the string pointed to by buf.
225
225
  */
226
226
  void complex_print_decimal (char* buf, Complex* complex)
227
227
  {
@@ -292,7 +292,37 @@ VALUE rb_Number_initialize_copy (VALUE number, VALUE copy)
292
292
 
293
293
  VALUE rb_Number_to_c (VALUE number)
294
294
  {
295
- /* TODO */
295
+ VALUE real;
296
+ VALUE imag;
297
+
298
+ if (complex_re_normal_p(DATA_PTR(number)))
299
+ {
300
+ char re_buf[10000];
301
+
302
+ complex_print_decimal(re_buf, DATA_PTR(number));
303
+
304
+ real = DBL2NUM(rb_cstr_to_dbl(re_buf, 0));
305
+ }
306
+ else
307
+ {
308
+ real = complex_re_nan_p(DATA_PTR(number)) ? DBL2NUM(NAN) : complex_re_pos_inf_p(DATA_PTR(number)) ? DBL2NUM(INFINITY) : rb_funcall(DBL2NUM(INFINITY), rb_intern("-@"), 0);
309
+ }
310
+
311
+ if (complex_im_normal_p(DATA_PTR(number)))
312
+ {
313
+ char im_buf[10000];
314
+ VALUE im = rb_funcall(number, rb_intern("imag"), 0);
315
+
316
+ complex_print_decimal(im_buf, DATA_PTR(im));
317
+
318
+ imag = DBL2NUM(rb_cstr_to_dbl(im_buf, 0));
319
+ }
320
+ else
321
+ {
322
+ imag = complex_im_nan_p(DATA_PTR(number)) ? DBL2NUM(NAN) : complex_im_pos_inf_p(DATA_PTR(number)) ? DBL2NUM(INFINITY) : rb_funcall(DBL2NUM(INFINITY), rb_intern("-@"), 0);
323
+ }
324
+
325
+ return rb_funcall(rb_cObject, rb_intern("Complex"), 2, real, imag);
296
326
  }
297
327
 
298
328
  VALUE rb_Number_to_f (VALUE number)
@@ -1474,6 +1504,7 @@ void Init_number ()
1474
1504
  rb_define_method(rb_cNumber, "singleton_method_added", rb_Number_singleton_method_added, 1);
1475
1505
  rb_define_method(rb_cNumber, "initialize_copy", rb_Number_initialize_copy, 1);
1476
1506
 
1507
+ rb_define_method(rb_cNumber, "to_c", rb_Number_to_c, 0);
1477
1508
  rb_define_method(rb_cNumber, "to_f", rb_Number_to_f, 0);
1478
1509
  rb_define_method(rb_cNumber, "to_i", rb_Number_to_i, 0);
1479
1510
  rb_define_method(rb_cNumber, "to_int", rb_Number_to_int, 0);
@@ -0,0 +1,37 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright © 2011 Jesse Sielaff
4
+ #
5
+
6
+ require 'minitest/autorun'
7
+ require 'number'
8
+
9
+ class TestComplex < MiniTest::Unit::TestCase
10
+ def test_to_number_returns_number_with_same_value
11
+ assert_equal(Number(1,2), (1+2.i).to_number)
12
+ end
13
+
14
+ def test_to_number_works_for_nan_real_part
15
+ assert_equal(Number(Number.nan,2), (Float::NAN+2.i).to_number)
16
+ end
17
+
18
+ def test_to_number_works_for_nan_imag_part
19
+ assert_equal(Number(1,Number.nan), (1+Float::NAN.i).to_number)
20
+ end
21
+
22
+ def test_to_number_works_for_pos_inf_real_part
23
+ assert_equal(Number(Number.pos_inf,2), (Float::INFINITY+2.i).to_number)
24
+ end
25
+
26
+ def test_to_number_works_for_pos_inf_imag_part
27
+ assert_equal(Number(1,Number.pos_inf), (1+Float::INFINITY.i).to_number)
28
+ end
29
+
30
+ def test_to_number_works_for_neg_inf_real_part
31
+ assert_equal(Number(Number.neg_inf,2), (-Float::INFINITY+2.i).to_number)
32
+ end
33
+
34
+ def test_to_number_works_for_neg_inf_imag_part
35
+ assert_equal(Number(1,Number.neg_inf), (1-Float::INFINITY.i).to_number)
36
+ end
37
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright © 2011 Jesse Sielaff
4
+ #
5
+
6
+ require 'minitest/autorun'
7
+ require 'number'
8
+
9
+ class TestFloat < MiniTest::Unit::TestCase
10
+ def test_to_number_returns_number_with_same_value
11
+ assert_equal(Number(1.5), (1.5).to_number)
12
+ end
13
+
14
+ def test_to_number_works_for_positive_infinity
15
+ assert_equal(Number.pos_inf, (Float::INFINITY).to_number)
16
+ end
17
+
18
+ def test_to_number_works_for_negative_infinity
19
+ assert_equal(Number.neg_inf, (-Float::INFINITY).to_number)
20
+ end
21
+
22
+ def test_to_number_works_for_nan
23
+ assert_equal(Number.nan, (Float::NAN).to_number)
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright © 2011 Jesse Sielaff
4
+ #
5
+
6
+ require 'minitest/autorun'
7
+ require 'number'
8
+
9
+ class TestInteger < MiniTest::Unit::TestCase
10
+ def test_to_number_returns_number_with_same_value
11
+ assert_equal(1, 1.to_number)
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright © 2011 Jesse Sielaff
4
+ #
5
+
6
+ require 'minitest/autorun'
7
+ require 'number'
8
+
9
+ class TestNil < MiniTest::Unit::TestCase
10
+ def test_to_number_returns_nan
11
+ assert_equal(Number.nan, nil.to_number)
12
+ end
13
+ end
@@ -0,0 +1,87 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright © 2011 Jesse Sielaff
4
+ #
5
+
6
+ require 'minitest/autorun'
7
+ require 'number'
8
+
9
+ class TestNumber < MiniTest::Unit::TestCase
10
+ def test_new_raises_error
11
+ assert_raises(NoMethodError) { Number.new }
12
+ end
13
+
14
+ def test_Number_returns_new_number
15
+ assert_equal(1, Number(1).to_i)
16
+ end
17
+
18
+ def test_Number_with_two_arguments_returns_new_complex_number
19
+ # Need to write Number#to_c
20
+ # assert_equal(Complex(1,2), Number(1,2).to_c)
21
+ end
22
+
23
+ def test_Number_with_two_arguments_returns_new_interval_number
24
+
25
+ end
26
+
27
+ def test_digits
28
+ end
29
+
30
+ def test_nan_returns_new_nan_number
31
+ assert_equal(Float::NAN, Number.nan)
32
+ end
33
+
34
+ def test_pos_inf_returns_new_pos_inf_number
35
+ assert_equal(Float::INFINITY, Number.pos_inf)
36
+ end
37
+
38
+ def test_neg_inf_returns_new_neg_inf_number
39
+ assert_equal(-Float::INFINITY, Number.neg_inf)
40
+ end
41
+
42
+ def test_zero_returns_new_zero_number
43
+ assert_equal(0, Number.zero)
44
+ end
45
+
46
+ def test_one_returns_new_one_number
47
+ assert_equal(1, Number.one)
48
+ end
49
+
50
+ def test_pi_returns_new_pi_number
51
+ # Need to figure out what kind of assertion to use (in_delta?)
52
+ # assert_equal(Math::PI, Number.pi)
53
+ end
54
+
55
+ def test_e_returns_new_e_number
56
+ # See above
57
+ # assert_equal(Math::E, Number.e)
58
+ end
59
+
60
+ def test_i_returns_new_i_number
61
+ assert_equal(1.i, Number.i)
62
+ end
63
+
64
+ def test_unary_plus_returns_same_number
65
+ assert_equal(1, Number(1).+@)
66
+ end
67
+
68
+ def test_unary_minus_returns_negative_of_number
69
+ assert_equal(-1, Number(1).-@)
70
+ end
71
+
72
+ def test_add_returns_sum_of_numbers
73
+ assert_equal(2, Number(1) + Number(1))
74
+ end
75
+
76
+ def test_subtract_returns_difference_of_numbers
77
+ assert_equal(0, Number(1) - Number(1))
78
+ end
79
+
80
+ def test_multiply_returns_product_of_numbers
81
+ assert_equal(4, Number(2) * Number(2))
82
+ end
83
+
84
+ def test_divide_returns_quotient_of_numbers
85
+ assert_equal(1, Number(2) / Number(2))
86
+ end
87
+ end
File without changes
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: number
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.9.11
5
+ version: 0.9.12
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jesse Sielaff
@@ -35,6 +35,14 @@ files:
35
35
  - ext/number/real.c
36
36
  - ext/number/real_bounds.c
37
37
  - ext/number/round.c
38
+ - test/test_complex.rb
39
+ - test/test_float.rb
40
+ - test/test_integer.rb
41
+ - test/test_nil.rb
42
+ - test/test_number.rb
43
+ - test/test_string.rb
44
+ - Rakefile
45
+ - .gemtest
38
46
  homepage:
39
47
  licenses: []
40
48