[ruby-oci8-commit] [388] trunk/ruby-oci8: * ext/oci8/oranumber_util.c, test/test_oranumber.rb:

nobody at rubyforge.org nobody at rubyforge.org
Fri Apr 23 09:32:46 EDT 2010


Revision: 388
Author:   kubo
Date:     2010-04-23 09:32:46 -0400 (Fri, 23 Apr 2010)

Log Message:
-----------
* ext/oci8/oranumber_util.c, test/test_oranumber.rb:
    add limitted support for OraNumber's positive and negative
    infinity. They are converted to '~' and '-~' respectively
    as described in <URL:http://www.ixora.com.au/notes/infinity.htm>.

Modified Paths:
--------------
    trunk/ruby-oci8/ChangeLog
    trunk/ruby-oci8/ext/oci8/oranumber_util.c
    trunk/ruby-oci8/test/test_oranumber.rb

Modified: trunk/ruby-oci8/ChangeLog
===================================================================
--- trunk/ruby-oci8/ChangeLog	2010-04-23 12:09:01 UTC (rev 387)
+++ trunk/ruby-oci8/ChangeLog	2010-04-23 13:32:46 UTC (rev 388)
@@ -1,4 +1,10 @@
 2010-04-23  KUBO Takehiro  <kubo at jiubao.org>
+	* ext/oci8/oranumber_util.c, test/test_oranumber.rb:
+	    add limitted support for OraNumber's positive and negative
+	    infinity. They are converted to '~' and '-~' respectively
+	    as described in <URL:http://www.ixora.com.au/notes/infinity.htm>.
+
+2010-04-23  KUBO Takehiro  <kubo at jiubao.org>
 	* Makefile: fix wrong dependencies in Makefile when running
 	    'make -jNNN (where NNN >= 2)'
 	    (contributed by Alyano Alyanos. See bug #28129 on rubyforge.)

Modified: trunk/ruby-oci8/ext/oci8/oranumber_util.c
===================================================================
--- trunk/ruby-oci8/ext/oci8/oranumber_util.c	2010-04-23 12:09:01 UTC (rev 387)
+++ trunk/ruby-oci8/ext/oci8/oranumber_util.c	2010-04-23 13:32:46 UTC (rev 388)
@@ -37,9 +37,24 @@
             PUTEND();
             return 1;
         }
+        if (on->OCINumberPart[1] == 0) {
+            /* negative infinity */
+            PUTC('-');
+            PUTC('~');
+            PUTEND();
+            return 2;
+        }
         /* unexpected format */
         return -1;
     }
+    if (datalen == 2) {
+        if (on->OCINumberPart[1] == 255 && on->OCINumberPart[2] == 101) {
+            /* positive infinity */
+            PUTC('~');
+            PUTEND();
+            return 1;
+        }
+    }
     if (datalen > 21) {
         /* too long */
         return -1;
@@ -137,6 +152,28 @@
         buf++;
         is_positive = 0;
     }
+    if (*buf == '~') {
+        buf ++;
+        /* skip trailing spaces */
+        while (buf < end && *buf == ' ') {
+            buf++;
+        }
+        if (buf != end) {
+            return ORANUMBER_INVALID_NUMBER;
+        }
+        if (is_positive) {
+            /* positive infinity */
+            on->OCINumberPart[0] = 2;
+            on->OCINumberPart[1] = 255;
+            on->OCINumberPart[2] = 101;
+        } else {
+            /* negative infinity */
+            on->OCINumberPart[0] = 1;
+            on->OCINumberPart[1] = 0;
+        }
+        return ORANUMBER_SUCCESS;
+    }
+
     /* next should be number or a dot */
     if ((*buf < '0' || '9' < *buf) && *buf != '.') {
         return ORANUMBER_INVALID_NUMBER;

Modified: trunk/ruby-oci8/test/test_oranumber.rb
===================================================================
--- trunk/ruby-oci8/test/test_oranumber.rb	2010-04-23 12:09:01 UTC (rev 387)
+++ trunk/ruby-oci8/test/test_oranumber.rb	2010-04-23 13:32:46 UTC (rev 388)
@@ -125,6 +125,33 @@
         val = $1+'.'+$2 if /(-?)0\.(.*)/ =~ val
         assert_equal(val, cursor[:out])
       end
+      # Infinity and -Infinity
+      ['~', '-~'].each do |val|
+        cursor[:in] = OraNumber.new(val)
+        cursor.exec
+        assert_equal(val, cursor[:out])
+      end
+      if OCI8::oracle_client_version >= OCI8::ORAVER_10_1
+        cursor = conn.parse(<<EOS)
+BEGIN
+  IF (:in_oranum = CAST(:in_binary_double AS NUMBER)) THEN
+    :result := 'match';
+  ELSE
+    :result := 'unmatch';
+  END IF;
+END;
+EOS
+        cursor.bind_param(:in_oranum, nil, OraNumber)
+        cursor.bind_param(:in_binary_double, nil, :binary_double)
+        cursor.bind_param(:result, nil, String, 7)
+        [['~', 1.0/0.0], ['-~', -1.0/0.0]].each do |val|
+          cursor[:in_oranum] = OraNumber.new(val[0])
+          cursor[:in_binary_double] = val[1]
+          cursor.exec
+          assert_equal(cursor[:result], 'match')
+        end
+      end
+
     ensure
       conn.logoff
     end
@@ -142,13 +169,24 @@
         cursor.exec
         assert_equal(OraNumber.new(val), cursor[:out])
       end
+      # Infinity and -Infinity
+      if OCI8::oracle_client_version >= OCI8::ORAVER_10_1
+        cursor = conn.parse("BEGIN :out := CAST(:in AS NUMBER); END;")
+        cursor.bind_param(:out, OraNumber)
+        cursor.bind_param(:in, nil, :binary_double)
+        [['~', 1.0/0.0], ['-~', -1.0/0.0]].each do |val|
+          cursor[:in] = val[1]
+          cursor.exec
+          assert_equal(OraNumber.new(val[0]), cursor[:out])
+        end
+      end
     ensure
       conn.logoff
     end
   end
 
   def test_dup
-    LARGE_RANGE_VALUES.each do |x|
+    (LARGE_RANGE_VALUES + ['~', '-~']).each do |x|
       n = OraNumber.new(x)
       assert_equal(n, n.dup)
       assert_equal(n, n.clone)
@@ -156,14 +194,14 @@
   end
 
   def test_marshal
-    LARGE_RANGE_VALUES.each do |x|
+    (LARGE_RANGE_VALUES + ['~', '-~']).each do |x|
       n = OraNumber.new(x)
       assert_equal(n, Marshal.load(Marshal.dump(n)))
     end
   end
 
   def test_yaml
-    LARGE_RANGE_VALUES.each do |x|
+    (LARGE_RANGE_VALUES + ['~', '-~']).each do |x|
       n = OraNumber.new(x)
       assert_equal(n, YAML.load(YAML.dump(n)))
     end




More information about the ruby-oci8-commit mailing list