Make Float and Integer immutable
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/196237b7 Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/196237b7 Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/196237b7 Branch: refs/heads/master Commit: 196237b7957bfc90bb395c9a54eb0ee673b24acb Parents: f626543 Author: Nick Wellnhofer <[email protected]> Authored: Thu Jul 9 17:40:39 2015 +0200 Committer: Nick Wellnhofer <[email protected]> Committed: Thu Jul 9 17:40:39 2015 +0200 ---------------------------------------------------------------------- runtime/core/Clownfish/Num.c | 26 +----- runtime/core/Clownfish/Num.cfh | 16 +--- runtime/core/Clownfish/Test/TestNum.c | 141 +++++++++++++++-------------- 3 files changed, 77 insertions(+), 106 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/196237b7/runtime/core/Clownfish/Num.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Num.c b/runtime/core/Clownfish/Num.c index 157880d..991883f 100644 --- a/runtime/core/Clownfish/Num.c +++ b/runtime/core/Clownfish/Num.c @@ -103,11 +103,6 @@ Float_Get_Value_IMP(Float *self) { return self->value; } -void -Float_Set_Value_IMP(Float *self, double value) { - self->value = value; -} - int64_t Float_To_I64_IMP(Float *self) { if (self->value < -POW_2_63 || self->value >= POW_2_63) { @@ -128,13 +123,7 @@ Float_To_String_IMP(Float *self) { Float* Float_Clone_IMP(Float *self) { - return Float_new(self->value); -} - -void -Float_Mimic_IMP(Float *self, Obj *other) { - Float *twin = (Float*)CERTIFY(other, FLOAT); - self->value = twin->value; + return (Float*)INCREF(self); } /***************************************************************************/ @@ -187,11 +176,6 @@ Int_Get_Value_IMP(Integer *self) { return self->value; } -void -Int_Set_Value_IMP(Integer *self, int64_t value) { - self->value = value; -} - double Int_To_F64_IMP(Integer *self) { return (double)self->value; @@ -209,13 +193,7 @@ Int_To_String_IMP(Integer *self) { Integer* Int_Clone_IMP(Integer *self) { - return Int_new(self->value); -} - -void -Int_Mimic_IMP(Integer *self, Obj *other) { - Integer *twin = (Integer*)CERTIFY(other, INTEGER); - self->value = twin->value; + return (Integer*)INCREF(self); } static int32_t http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/196237b7/runtime/core/Clownfish/Num.cfh ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Num.cfh b/runtime/core/Clownfish/Num.cfh index 1874963..d4854f2 100644 --- a/runtime/core/Clownfish/Num.cfh +++ b/runtime/core/Clownfish/Num.cfh @@ -16,7 +16,7 @@ parcel Clownfish; -/** Double precision floating point number. +/** Immutable double precision floating point number. */ final class Clownfish::Float { @@ -34,9 +34,6 @@ final class Clownfish::Float { void* To_Host(Float *self); - void - Set_Value(Float *self, double value); - double Get_Value(Float *self); @@ -60,13 +57,10 @@ final class Clownfish::Float { public incremented Float* Clone(Float *self); - - public void - Mimic(Float *self, Obj *other); } /** - * 64-bit signed integer. + * Immutable 64-bit signed integer. */ final class Clownfish::Integer nickname Int { @@ -84,9 +78,6 @@ final class Clownfish::Integer nickname Int { void* To_Host(Integer *self); - void - Set_Value(Integer *self, int64_t value); - int64_t Get_Value(Integer *self); @@ -110,9 +101,6 @@ final class Clownfish::Integer nickname Int { public incremented Integer* Clone(Integer *self); - - public void - Mimic(Integer *self, Obj *other); } http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/196237b7/runtime/core/Clownfish/Test/TestNum.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Test/TestNum.c b/runtime/core/Clownfish/Test/TestNum.c index 3dcd276..0e88455 100644 --- a/runtime/core/Clownfish/Test/TestNum.c +++ b/runtime/core/Clownfish/Test/TestNum.c @@ -55,24 +55,18 @@ test_To_String(TestBatchRunner *runner) { static void test_accessors(TestBatchRunner *runner) { - Float *f64 = Float_new(1.0); - Integer *i64 = Int_new(1); + Float *f64 = Float_new(1.33); + Integer *i64 = Int_new(INT64_MIN); double wanted64 = 1.33; double got64; - Float_Set_Value(f64, 1.33); got64 = Float_Get_Value(f64); TEST_TRUE(runner, *(int64_t*)&got64 == *(int64_t*)&wanted64, - "F64 Set_Value Get_Value"); - - TEST_TRUE(runner, Float_To_I64(f64) == 1, "Float_To_Int"); - - Int_Set_Value(i64, INT64_MIN); - TEST_TRUE(runner, Int_Get_Value(i64) == INT64_MIN, - "I64 Set_Value Get_Value"); + "F64 Get_Value"); + TEST_TRUE(runner, Float_To_I64(f64) == 1, "Float_To_I64"); - Int_Set_Value(i64, -1); - TEST_TRUE(runner, Int_To_F64(i64) == -1, "Int_To_Float"); + TEST_TRUE(runner, Int_Get_Value(i64) == INT64_MIN, "I64 Get_Value"); + TEST_TRUE(runner, Int_To_F64(i64) == -9223372036854775808.0, "Int_To_F64"); DECREF(i64); DECREF(f64); @@ -115,46 +109,76 @@ S_test_compare_float_int(TestBatchRunner *runner, double f64_val, static void test_Equals_and_Compare_To(TestBatchRunner *runner) { - Float *f1 = Float_new(1.0); - Float *f2 = Float_new(1.0); - Integer *i64 = Int_new(INT64_MAX); - - TEST_TRUE(runner, Float_Compare_To(f1, (Obj*)f2) == 0, - "F64_Compare_To equal"); - TEST_TRUE(runner, Float_Equals(f1, (Obj*)f2), - "F64_Equals equal"); - - Float_Set_Value(f2, 2.0); - TEST_TRUE(runner, Float_Compare_To(f1, (Obj*)f2) < 0, - "F64_Compare_To less than"); - TEST_FALSE(runner, Float_Equals(f1, (Obj*)f2), - "F64_Equals less than"); - - Float_Set_Value(f2, 0.0); - TEST_TRUE(runner, Float_Compare_To(f1, (Obj*)f2) > 0, - "F64_Compare_To greater than"); - TEST_FALSE(runner, Float_Equals(f1, (Obj*)f2), - "F64_Equals greater than"); - - Float_Set_Value(f1, INT64_MAX * 2.0); - TEST_TRUE(runner, Float_Compare_To(f1, (Obj*)i64) > 0, - "Float comparison to Integer"); - TEST_TRUE(runner, Int_Compare_To(i64, (Obj*)f1) < 0, - "Integer comparison to Float"); - - Int_Set_Value(i64, INT64_C(0x6666666666666666)); - Integer *i64_copy = Int_new(INT64_C(0x6666666666666666)); - TEST_TRUE(runner, Int_Compare_To(i64, (Obj*)i64_copy) == 0, - "Integer comparison to same number"); - - DECREF(i64_copy); - DECREF(i64); - DECREF(f1); - DECREF(f2); + { + Float *f1 = Float_new(1.0); + Float *f2 = Float_new(1.0); + TEST_TRUE(runner, Float_Compare_To(f1, (Obj*)f2) == 0, + "Float_Compare_To equal"); + TEST_TRUE(runner, Float_Equals(f1, (Obj*)f2), + "Float_Equals equal"); + DECREF(f1); + DECREF(f2); + } + + { + Float *f1 = Float_new(1.0); + Float *f2 = Float_new(2.0); + TEST_TRUE(runner, Float_Compare_To(f1, (Obj*)f2) < 0, + "Float_Compare_To less than"); + TEST_FALSE(runner, Float_Equals(f1, (Obj*)f2), + "Float_Equals less than"); + DECREF(f1); + DECREF(f2); + } + + { + Float *f1 = Float_new(1.0); + Float *f2 = Float_new(0.0); + TEST_TRUE(runner, Float_Compare_To(f1, (Obj*)f2) > 0, + "Float_Compare_To greater than"); + TEST_FALSE(runner, Float_Equals(f1, (Obj*)f2), + "Float_Equals greater than"); + DECREF(f1); + DECREF(f2); + } + + { + Integer *i1 = Int_new(INT64_C(0x6666666666666666)); + Integer *i2 = Int_new(INT64_C(0x6666666666666666)); + TEST_TRUE(runner, Int_Compare_To(i1, (Obj*)i2) == 0, + "Int_Compare_To equal"); + TEST_TRUE(runner, Int_Equals(i1, (Obj*)i2), + "Int_Equals equal"); + DECREF(i1); + DECREF(i2); + } + + { + Integer *i1 = Int_new(INT64_C(0x6666666666666666)); + Integer *i2 = Int_new(INT64_C(0x6666666666666667)); + TEST_TRUE(runner, Int_Compare_To(i1, (Obj*)i2) < 0, + "Int_Compare_To less than"); + TEST_FALSE(runner, Int_Equals(i1, (Obj*)i2), + "Int_Equals less than"); + DECREF(i1); + DECREF(i2); + } + + { + Integer *i1 = Int_new(INT64_C(0x6666666666666666)); + Integer *i2 = Int_new(INT64_C(0x6666666666666665)); + TEST_TRUE(runner, Int_Compare_To(i1, (Obj*)i2) > 0, + "Int_Compare_To greater than"); + TEST_FALSE(runner, Int_Equals(i1, (Obj*)i2), + "Int_Equals greater than"); + DECREF(i1); + DECREF(i2); + } // NOTICE: When running these tests on x86/x64, it's best to compile // with -ffloat-store to avoid excess FPU precision which can hide // implementation bugs. + S_test_compare_float_int(runner, INT64_MAX * 2.0, INT64_MAX, 1); S_test_compare_float_int(runner, pow(2.0, 60.0), INT64_C(1) << 60, 0); S_test_compare_float_int(runner, pow(2.0, 60.0), (INT64_C(1) << 60) - 1, 1); @@ -182,32 +206,13 @@ test_Clone(TestBatchRunner *runner) { DECREF(f64); } -static void -test_Mimic(TestBatchRunner *runner) { - Float *f64 = Float_new(1.33); - Integer *i64 = Int_new(INT64_MAX); - Float *f64_dupe = Float_new(0.0); - Integer *i64_dupe = Int_new(0); - Float_Mimic(f64_dupe, (Obj*)f64); - Int_Mimic(i64_dupe, (Obj*)i64); - TEST_TRUE(runner, Float_Equals(f64, (Obj*)f64_dupe), - "Float Mimic"); - TEST_TRUE(runner, Int_Equals(i64, (Obj*)i64_dupe), - "Integer Mimic"); - DECREF(i64_dupe); - DECREF(f64_dupe); - DECREF(i64); - DECREF(f64); -} - void TestNum_Run_IMP(TestNum *self, TestBatchRunner *runner) { - TestBatchRunner_Plan(runner, (TestBatch*)self, 59); + TestBatchRunner_Plan(runner, (TestBatch*)self, 68); test_To_String(runner); test_accessors(runner); test_Equals_and_Compare_To(runner); test_Clone(runner); - test_Mimic(runner); }
