public
Description: Rubinius, the Ruby VM
Homepage: http://rubini.us
Clone URL: git://github.com/evanphx/rubinius.git
Implement Fixnum::neg, to_f and to_s, might be complete now
dbussink (author)
Fri May 30 19:10:42 -0700 2008
commit  a95eeb16bca84628f55e2d1f43a39b9d49be723b
tree    f4f675b5406d3d3a23c04d0e6c973c9b304f26cb
parent  d14b845521533877b4f81a25a9e513fa3039e070
...
148
149
150
 
 
 
 
 
151
152
153
...
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
...
148
149
150
151
152
153
154
155
156
157
158
...
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
0
@@ -148,6 +148,11 @@ namespace rubinius {
0
       return Float::create(state, n2i())->divmod(state, other);
0
     }
0
 
0
+ // Ruby.primitive :fixnum_neg
0
+ INTEGER neg(STATE) {
0
+ return Object::i2n(-n2i());
0
+ }
0
+
0
     // Ruby.primitive! :fixnum_equal
0
     OBJECT equal(STATE, FIXNUM other) {
0
       return n2i() == other->n2i() ? Qtrue : Qfalse;
0
@@ -299,46 +304,67 @@ namespace rubinius {
0
       return Object::i2n(sizeof(native_int));
0
     }
0
 
0
+ // Ruby.primitive! :fixnum_and
0
     INTEGER bit_and(STATE, FIXNUM other) {
0
       return Object::i2n(n2i() & other->n2i());
0
     }
0
 
0
+ // Ruby.primitive! :fixnum_and
0
     INTEGER bit_and(STATE, Bignum* other) {
0
       return other->bit_and(state, this);
0
     }
0
 
0
+ // Ruby.primitive! :fixnum_and
0
     INTEGER bit_and(STATE, Float* other) {
0
       return Object::i2n(n2i() & (native_int)other->val);
0
     }
0
 
0
+ // Ruby.primitive! :fixnum_or
0
     INTEGER bit_or(STATE, FIXNUM other) {
0
       return Object::i2n(n2i() | other->n2i());
0
     }
0
 
0
+ // Ruby.primitive! :fixnum_or
0
     INTEGER bit_or(STATE, Bignum* other) {
0
       return other->bit_or(state, this);
0
     }
0
 
0
+ // Ruby.primitive! :fixnum_or
0
     INTEGER bit_or(STATE, Float* other) {
0
       return Object::i2n(n2i() | (native_int)other->val);
0
     }
0
 
0
+ // Ruby.primitive! :fixnum_xor
0
     INTEGER bit_xor(STATE, FIXNUM other) {
0
       return Object::i2n(n2i() ^ other->n2i());
0
     }
0
 
0
+ // Ruby.primitive! :fixnum_xor
0
     INTEGER bit_xor(STATE, Bignum* other) {
0
       return other->bit_xor(state, this);
0
     }
0
 
0
+ // Ruby.primitive! :fixnum_xor
0
     INTEGER bit_xor(STATE, Float* other) {
0
       return Object::i2n(n2i() ^ (native_int)other->val);
0
     }
0
 
0
+ // Ruby.primitive :fixnum_invert
0
     INTEGER invert(STATE) {
0
       return Object::i2n(~n2i());
0
     }
0
 
0
+ // Ruby.primitive :fixnum_to_f
0
+ Float* to_f(STATE) {
0
+ return Float::create(state, (double)n2i());
0
+ }
0
+
0
+ // Ruby.primitive :fixnum_to_s
0
+ String* to_s(STATE) {
0
+ std::stringstream sout;
0
+ sout << n2i();
0
+ return String::create(state, sout.str().c_str());
0
+ }
0
   };
0
 
0
   typedef Fixnum* FIXNUM;
...
3
4
5
 
6
7
8
...
3
4
5
6
7
8
9
0
@@ -3,6 +3,7 @@
0
 
0
 #include <stdint.h>
0
 #include <sys/types.h>
0
+#include <sstream>
0
 #include <cstring>
0
 #include <cassert>
0
 #include <vector>
...
41
42
43
 
 
44
45
46
...
80
81
82
83
84
85
86
87
...
41
42
43
44
45
46
47
48
...
82
83
84
 
 
85
86
87
0
@@ -41,6 +41,8 @@ namespace rubinius {
0
 
0
 #include "builtin_tuple.hpp"
0
 #include "builtin_array.hpp"
0
+#include "builtin_bytearray.hpp"
0
+#include "builtin_string.hpp"
0
 #include "builtin_float.hpp"
0
 
0
 #include "builtin_bignum.hpp"
0
@@ -80,8 +82,6 @@ namespace rubinius {
0
 #include "builtin_io.hpp"
0
 
0
 #include "builtin_regexp.hpp"
0
-#include "builtin_bytearray.hpp"
0
-#include "builtin_string.hpp"
0
 
0
 #include "builtin_lookuptable.hpp"
0
 #include "builtin_methodtable.hpp"
...
258
259
260
 
 
 
 
 
261
262
263
...
454
455
456
 
 
 
 
 
 
 
 
 
 
 
 
457
458
459
...
258
259
260
261
262
263
264
265
266
267
268
...
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
0
@@ -258,6 +258,11 @@ class TestFixnum : public CxxTest::TestSuite {
0
     check_float(as<Float>(o2), Float::create(state, -1.5));
0
   }
0
 
0
+ void test_neg() {
0
+ TS_ASSERT_EQUALS(Object::i2n(3)->neg(state), Object::i2n(-3));
0
+ TS_ASSERT_EQUALS(Object::i2n(-3)->neg(state), Object::i2n(3));
0
+ }
0
+
0
   void test_equal() {
0
     TS_ASSERT_EQUALS(Object::i2n(3)->equal(state, Object::i2n(3)), Qtrue);
0
     TS_ASSERT_EQUALS(Object::i2n(4)->equal(state, Object::i2n(3)), Qfalse);
0
@@ -454,6 +459,18 @@ class TestFixnum : public CxxTest::TestSuite {
0
     TS_ASSERT_EQUALS(Object::i2n(-5)->invert(state), Object::i2n(4));
0
   }
0
 
0
+ void test_to_f() {
0
+ Float* f = Object::i2n(5)->to_f(state);
0
+ check_float(f, Float::create(state, 5.0));
0
+ f = Object::i2n(-2)->to_f(state);
0
+ check_float(f, Float::create(state, -2.0));
0
+ }
0
+
0
+ void test_to_s() {
0
+ String* n = Object::i2n(86545)->to_s(state);
0
+ TS_ASSERT_EQUALS(std::string("86545"), (char*)*n);
0
+ }
0
+
0
   void test_uncastable_object_throws_exception() {
0
     TS_ASSERT_THROWS( as<Integer>(String::create(state, "blah")), const TypeError &);
0
   }

Comments

    No one has commented yet.