You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2011/08/04 06:09:45 UTC

[lucy-commits] svn commit: r1153748 - in /incubator/lucy/trunk/core/Lucy: Object/Num.c Object/Num.cfh Test/Object/TestNum.c

Author: marvin
Date: Thu Aug  4 04:09:44 2011
New Revision: 1153748

URL: http://svn.apache.org/viewvc?rev=1153748&view=rev
Log:
Introduce BoolNum, a boolean class, plus singleton objects representing true
and false.  These are needed for clean round-tripping of data through JSON
serialization.

Modified:
    incubator/lucy/trunk/core/Lucy/Object/Num.c
    incubator/lucy/trunk/core/Lucy/Object/Num.cfh
    incubator/lucy/trunk/core/Lucy/Test/Object/TestNum.c

Modified: incubator/lucy/trunk/core/Lucy/Object/Num.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/core/Lucy/Object/Num.c?rev=1153748&r1=1153747&r2=1153748&view=diff
==============================================================================
--- incubator/lucy/trunk/core/Lucy/Object/Num.c (original)
+++ incubator/lucy/trunk/core/Lucy/Object/Num.c Thu Aug  4 04:09:44 2011
@@ -21,6 +21,8 @@
 #define C_LUCY_INTEGER64
 #define C_LUCY_FLOAT32
 #define C_LUCY_FLOAT64
+#define C_LUCY_BOOLNUM
+#define C_LUCY_VIEWCHARBUF
 #define LUCY_USE_SHORT_NAMES
 #define CHY_USE_SHORT_NAMES
 
@@ -354,4 +356,74 @@ Int64_deserialize(Integer64 *self, InStr
     return self ? Int64_init(self, value) : Int64_new(value);
 }
 
+/***************************************************************************/
+
+static ViewCharBuf true_string  = { VIEWCHARBUF, {1}, "true",  4, 0 };
+static ViewCharBuf false_string = { VIEWCHARBUF, {1}, "false", 5, 0 };
+static BoolNum true_obj  = { BOOLNUM, {1}, true, &true_string };
+static BoolNum false_obj = { BOOLNUM, {1}, false, &false_string };
+BoolNum *Bool_true_singleton  = &true_obj;
+BoolNum *Bool_false_singleton = &false_obj;
+
+void
+Bool_destroy(BoolNum *self) {
+    UNUSED_VAR(self);
+}
+
+bool_t
+Bool_get_value(BoolNum *self) {
+    return self->value;
+}
+
+double
+Bool_to_f64(BoolNum *self) {
+    return (double)self->value;
+}
+
+int64_t
+Bool_to_i64(BoolNum *self) {
+    return self->value;
+}
+
+BoolNum*
+Bool_clone(BoolNum *self) {
+    return self;
+}
+
+int32_t
+Bool_hash_sum(BoolNum *self) {
+    int64_t hash_sum = PTR_TO_I64(self) + self->value;
+    return (int32_t)hash_sum;
+}
+
+CharBuf*
+Bool_to_string(BoolNum *self) {
+    return (CharBuf*)ViewCB_Inc_RefCount(self->string);
+}
+
+bool_t
+Bool_equals(BoolNum *self, Obj *other) {
+    return self == (BoolNum*)other;
+}
+
+void
+Bool_serialize(BoolNum *self, OutStream *outstream) {
+    OutStream_Write_U8(outstream, (uint8_t)self->value);
+}
+
+BoolNum*
+Bool_deserialize(BoolNum *self, InStream *instream) {
+    bool_t value = (bool_t)InStream_Read_U8(instream);
+    return value ? CFISH_TRUE : CFISH_FALSE;
+}
+
+BoolNum*
+Bool_inc_refcount(BoolNum *self) {
+    return self;
+}
+
+uint32_t
+Bool_dec_refcount(BoolNum *self) {
+    return 1;
+}
 

Modified: incubator/lucy/trunk/core/Lucy/Object/Num.cfh
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/core/Lucy/Object/Num.cfh?rev=1153748&r1=1153747&r2=1153748&view=diff
==============================================================================
--- incubator/lucy/trunk/core/Lucy/Object/Num.cfh (original)
+++ incubator/lucy/trunk/core/Lucy/Object/Num.cfh Thu Aug  4 04:09:44 2011
@@ -53,7 +53,6 @@ abstract class Lucy::Object::IntNum inhe
     To_String(IntNum *self);
 }
 
-
 /** Single precision floating point number.
  */
 class Lucy::Object::Float32 inherits Lucy::Object::FloatNum {
@@ -233,3 +232,61 @@ class Lucy::Object::Integer64 cnick Int6
 }
 
 
+/**
+ * Boolean type.
+ * 
+ * There are only two singleton instances of this class: CFISH_TRUE and
+ * CFISH_FALSE.
+ */
+class Lucy::Object::BoolNum cnick Bool inherits Lucy::Object::IntNum {
+    bool_t value;
+    ViewCharBuf *string;
+
+    inert BoolNum *true_singleton;
+    inert BoolNum *false_singleton;
+
+    public void
+    Destroy(BoolNum *self);
+
+    bool_t
+    Get_Value(BoolNum *self);
+
+    public int64_t
+    To_I64(BoolNum *self);
+
+    public double
+    To_F64(BoolNum *self);
+
+    public int32_t
+    Hash_Sum(BoolNum *self);
+
+    public void
+    Serialize(BoolNum *self, OutStream *outstream);
+
+    public incremented BoolNum*
+    Deserialize(BoolNum *self, InStream *instream);
+
+    /* Returns self. */
+    public incremented BoolNum*
+    Clone(BoolNum *self);
+
+    public bool_t
+    Equals(BoolNum *self, Obj *other);
+
+    public incremented CharBuf*
+    To_String(BoolNum *self);
+
+    incremented BoolNum*
+    Inc_RefCount(BoolNum *self);
+
+    uint32_t
+    Dec_RefCount(BoolNum *self);
+}
+
+__C__
+
+#define CFISH_TRUE  lucy_Bool_true_singleton
+#define CFISH_FALSE lucy_Bool_false_singleton
+
+__END_C__
+

Modified: incubator/lucy/trunk/core/Lucy/Test/Object/TestNum.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/core/Lucy/Test/Object/TestNum.c?rev=1153748&r1=1153747&r2=1153748&view=diff
==============================================================================
--- incubator/lucy/trunk/core/Lucy/Test/Object/TestNum.c (original)
+++ incubator/lucy/trunk/core/Lucy/Test/Object/TestNum.c Thu Aug  4 04:09:44 2011
@@ -31,6 +31,8 @@ test_To_String(TestBatch *batch) {
     CharBuf *f64_string = Float64_To_String(f64);
     CharBuf *i32_string = Int32_To_String(i32);
     CharBuf *i64_string = Int64_To_String(i64);
+    CharBuf *true_string  = Bool_To_String(CFISH_TRUE);
+    CharBuf *false_string = Bool_To_String(CFISH_FALSE);
 
     TEST_TRUE(batch, CB_Starts_With_Str(f32_string, "1.3", 3),
               "Float32_To_String");
@@ -40,7 +42,13 @@ test_To_String(TestBatch *batch) {
               "Int32_To_String");
     TEST_TRUE(batch, CB_Equals_Str(i64_string, "9223372036854775807", 19),
               "Int64_To_String");
+    TEST_TRUE(batch, CB_Equals_Str(true_string, "true", 4),
+              "Bool_To_String [true]");
+    TEST_TRUE(batch, CB_Equals_Str(false_string, "false", 5),
+              "Bool_To_String [false]");
 
+    DECREF(false_string);
+    DECREF(true_string);
     DECREF(i64_string);
     DECREF(i32_string);
     DECREF(f64_string);
@@ -95,6 +103,19 @@ test_accessors(TestBatch *batch) {
     TEST_TRUE(batch, Int32_To_F64(i32) == -1, "Int32_To_F64");
     TEST_TRUE(batch, Int64_To_F64(i64) == -1, "Int64_To_F64");
 
+    TEST_INT_EQ(batch, Bool_Get_Value(CFISH_TRUE), true,
+                "Bool_Get_Value [true]");
+    TEST_INT_EQ(batch, Bool_Get_Value(CFISH_FALSE), false,
+                "Bool_Get_Value [false]");
+    TEST_TRUE(batch, Bool_To_I64(CFISH_TRUE) == true,
+              "Bool_To_I64 [true]");
+    TEST_TRUE(batch, Bool_To_I64(CFISH_FALSE) == false,
+              "Bool_To_I64 [false]");
+    TEST_TRUE(batch, Bool_To_F64(CFISH_TRUE) == 1.0,
+              "Bool_To_F64 [true]");
+    TEST_TRUE(batch, Bool_To_F64(CFISH_FALSE) == 0.0,
+              "Bool_To_F64 [false]");
+
     DECREF(i64);
     DECREF(i32);
     DECREF(f64);
@@ -156,6 +177,17 @@ test_Equals_and_Compare_To(TestBatch *ba
     TEST_TRUE(batch, Int32_Compare_To(i32, (Obj*)f32) < 0,
               "Integer32 comparison to Float32");
 
+    TEST_TRUE(batch, Bool_Equals(CFISH_TRUE, (Obj*)CFISH_TRUE), 
+              "CFISH_TRUE Equals itself");
+    TEST_TRUE(batch, Bool_Equals(CFISH_FALSE, (Obj*)CFISH_FALSE), 
+              "CFISH_FALSE Equals itself");
+    TEST_FALSE(batch, Bool_Equals(CFISH_FALSE, (Obj*)CFISH_TRUE), 
+               "CFISH_FALSE not Equals CFISH_TRUE ");
+    TEST_FALSE(batch, Bool_Equals(CFISH_TRUE, (Obj*)CFISH_FALSE), 
+               "CFISH_TRUE not Equals CFISH_FALSE ");
+    TEST_FALSE(batch, Bool_Equals(CFISH_TRUE, (Obj*)CHARBUF), 
+               "CFISH_TRUE not Equals random other object ");
+
     DECREF(i64);
     DECREF(i32);
     DECREF(f64);
@@ -180,6 +212,8 @@ test_Clone(TestBatch *batch) {
               "Integer32 Clone");
     TEST_TRUE(batch, Int64_Equals(i64, (Obj*)i64_dupe),
               "Integer64 Clone");
+    TEST_TRUE(batch, Bool_Equals(CFISH_TRUE, (Obj*)Bool_Clone(CFISH_TRUE)),
+              "BoolNum Clone");
     DECREF(i64_dupe);
     DECREF(i32_dupe);
     DECREF(f64_dupe);
@@ -232,6 +266,7 @@ test_serialization(TestBatch *batch) {
     Float64   *f64_thaw = (Float64*)TestUtils_freeze_thaw((Obj*)f64);
     Integer32 *i32_thaw = (Integer32*)TestUtils_freeze_thaw((Obj*)i32);
     Integer64 *i64_thaw = (Integer64*)TestUtils_freeze_thaw((Obj*)i64);
+    BoolNum   *true_thaw = (BoolNum*)TestUtils_freeze_thaw((Obj*)CFISH_TRUE);
 
     TEST_TRUE(batch, Float32_Equals(f32, (Obj*)f32_thaw),
               "Float32 freeze/thaw");
@@ -241,6 +276,8 @@ test_serialization(TestBatch *batch) {
               "Integer32 freeze/thaw");
     TEST_TRUE(batch, Int64_Equals(i64, (Obj*)i64_thaw),
               "Integer64 freeze/thaw");
+    TEST_TRUE(batch, Bool_Equals(CFISH_TRUE, (Obj*)true_thaw),
+              "BoolNum freeze/thaw");
 
     DECREF(i64_thaw);
     DECREF(i32_thaw);
@@ -254,7 +291,7 @@ test_serialization(TestBatch *batch) {
 
 void
 TestNum_run_tests() {
-    TestBatch *batch = TestBatch_new(42);
+    TestBatch *batch = TestBatch_new(57);
     TestBatch_Plan(batch);
 
     test_To_String(batch);