[Groonga-commit] ranguba/rroonga at cafac23 [master] Accept string key for integer/unsigned integer/float types

Back to archive index

Kouhei Sutou null+****@clear*****
Fri Dec 12 17:48:56 JST 2014


Kouhei Sutou	2014-12-12 17:48:56 +0900 (Fri, 12 Dec 2014)

  New Revision: cafac2355de3dc85bc8b8357a449209a6971d676
  https://github.com/ranguba/rroonga/commit/cafac2355de3dc85bc8b8357a449209a6971d676

  Message:
    Accept string key for integer/unsigned integer/float types

  Modified files:
    ext/groonga/rb-grn-utils.c
    test/test-table-key-support.rb

  Modified: ext/groonga/rb-grn-utils.c (+27 -0)
===================================================================
--- ext/groonga/rb-grn-utils.c    2014-12-12 17:24:33 +0900 (1c3a706)
+++ ext/groonga/rb-grn-utils.c    2014-12-12 17:48:56 +0900 (6238d20)
@@ -481,46 +481,73 @@ rb_grn_bulk_from_ruby_object_with_type (VALUE object, grn_ctx *context,
 
     switch (type_id) {
     case GRN_DB_INT8:
+        if (string_p) {
+            object = rb_Integer(object);
+        }
         value.int8_value = NUM2SHORT(object);
         string = (const char *)&(value.int8_value);
         size = sizeof(value.int8_value);
         break;
     case GRN_DB_UINT8:
+        if (string_p) {
+            object = rb_Integer(object);
+        }
         value.uint8_value = NUM2USHORT(object);
         string = (const char *)&(value.uint8_value);
         size = sizeof(value.uint8_value);
         break;
     case GRN_DB_INT16:
+        if (string_p) {
+            object = rb_Integer(object);
+        }
         value.int16_value = NUM2SHORT(object);
         string = (const char *)&(value.int16_value);
         size = sizeof(value.int16_value);
         break;
     case GRN_DB_UINT16:
+        if (string_p) {
+            object = rb_Integer(object);
+        }
         value.uint16_value = NUM2USHORT(object);
         string = (const char *)&(value.uint16_value);
         size = sizeof(value.uint16_value);
         break;
     case GRN_DB_INT32:
+        if (string_p) {
+            object = rb_Integer(object);
+        }
         value.int32_value = NUM2INT(object);
         string = (const char *)&(value.int32_value);
         size = sizeof(value.int32_value);
         break;
     case GRN_DB_UINT32:
+        if (string_p) {
+            object = rb_Integer(object);
+        }
         value.uint32_value = NUM2UINT(object);
         string = (const char *)&(value.uint32_value);
         size = sizeof(value.uint32_value);
         break;
     case GRN_DB_INT64:
+        if (string_p) {
+            object = rb_Integer(object);
+        }
         value.int64_value = NUM2LL(object);
         string = (const char *)&(value.int64_value);
         size = sizeof(value.int64_value);
         break;
     case GRN_DB_UINT64:
+        if (string_p) {
+            object = rb_Integer(object);
+        }
         value.uint64_value = NUM2ULL(object);
         string = (const char *)&(value.uint64_value);
         size = sizeof(value.uint64_value);
         break;
     case GRN_DB_FLOAT:
+        if (string_p) {
+            object = rb_Float(object);
+        }
         value.double_value = NUM2DBL(object);
         string = (const char *)&(value.double_value);
         size = sizeof(value.double_value);

  Modified: test/test-table-key-support.rb (+59 -1)
===================================================================
--- test/test-table-key-support.rb    2014-12-12 17:24:33 +0900 (9e17b28)
+++ test/test-table-key-support.rb    2014-12-12 17:48:56 +0900 (7e8bdd4)
@@ -1,4 +1,4 @@
-# Copyright (C) 2013  Kouhei Sutou <kou �� clear-code.com>
+# Copyright (C) 2013-2014  Kouhei Sutou <kou �� clear-code.com>
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -76,6 +76,64 @@ class TableKeySupportTest < Test::Unit::TestCase
     end
   end
 
+  class CastTest < self
+    def test_int8
+      key = "-1"
+      ids = Groonga::Hash.create(:name => "IDs", :key_type => "Int8")
+      id = ids.add(key)
+      assert_equal(key.to_i, id.key)
+    end
+
+    def test_uint8
+      key = "1"
+      ids = Groonga::Hash.create(:name => "IDs", :key_type => "UInt8")
+      id = ids.add(key)
+      assert_equal(key.to_i, id.key)
+    end
+
+    def test_int16
+      key = (-(2 ** 8)).to_s
+      ids = Groonga::Hash.create(:name => "IDs", :key_type => "Int16")
+      id = ids.add(key)
+      assert_equal(key.to_i, id.key)
+    end
+
+    def test_uint16
+      key = (2 ** 8).to_s
+      ids = Groonga::Hash.create(:name => "IDs", :key_type => "UInt16")
+      id = ids.add(key)
+      assert_equal(key.to_i, id.key)
+    end
+
+    def test_int32
+      key = (-(2 ** 16)).to_s
+      ids = Groonga::Hash.create(:name => "IDs", :key_type => "Int32")
+      id = ids.add(key)
+      assert_equal(key.to_i, id.key)
+    end
+
+    def test_uint32
+      key = (2 ** 16).to_s
+      ids = Groonga::Hash.create(:name => "IDs", :key_type => "UInt32")
+      id = ids.add(key)
+      assert_equal(key.to_i, id.key)
+    end
+
+    def test_int64
+      key = (-(2 ** 32)).to_s
+      ids = Groonga::Hash.create(:name => "IDs", :key_type => "Int64")
+      id = ids.add(key)
+      assert_equal(key.to_i, id.key)
+    end
+
+    def test_uint64
+      key = (2 ** 32).to_s
+      ids = Groonga::Hash.create(:name => "IDs", :key_type => "UInt64")
+      id = ids.add(key)
+      assert_equal(key.to_i, id.key)
+    end
+  end
+
   class TokenizeTest < self
     class TableTypeTest < self
       def test_hash
-------------- next part --------------
HTML����������������������������...
Télécharger 



More information about the Groonga-commit mailing list
Back to archive index