ninjudd-tuple 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/VERSION.yml +1 -1
  2. data/ext/tuple.c +37 -7
  3. data/test/tuple_test.rb +16 -1
  4. metadata +2 -2
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 0
3
- :patch: 6
3
+ :patch: 7
4
4
  :major: 0
data/ext/tuple.c CHANGED
@@ -2,7 +2,8 @@
2
2
 
3
3
  VALUE mTuple;
4
4
 
5
- #define TRUE_SORT 128 // TrueClass
5
+ #define TRUE_SORT 255 // TrueClass
6
+ #define TIME_SORT 128 // Time
6
7
  #define SYM_SORT 64 // Symbol
7
8
  #define STR_SORT 32 // String
8
9
  #define INTP_SORT 16 // Integer (Positive)
@@ -14,6 +15,16 @@ VALUE mTuple;
14
15
 
15
16
  #define BDIGITS(x) ((BDIGIT*)RBIGNUM(x)->digits)
16
17
 
18
+ static void null_pad(VALUE data, int len) {
19
+ static u_int8_t null = 0;
20
+
21
+ // Pad with null bytes so subsequent fields will be aligned.
22
+ while (len % 4 != 0) {
23
+ rb_str_cat(data, (char*)&null, 1);
24
+ len++;
25
+ }
26
+ }
27
+
17
28
  /*
18
29
  * call-seq:
19
30
  * Tuple.dump(tuple) -> string
@@ -64,11 +75,17 @@ static VALUE tuple_dump(VALUE self, VALUE tuple) {
64
75
  len = RSTRING_LEN(item);
65
76
  rb_str_cat(data, RSTRING_PTR(item), len);
66
77
 
67
- // Pad with null bytes so subsequent fields will be aligned.
68
- while (len % 4 != 0) {
69
- rb_str_cat(data, (char*)&header, 1);
70
- len++;
71
- }
78
+ null_pad(data, len);
79
+ } else if (rb_obj_class(item) == rb_cTime) {
80
+ header[2] = TIME_SORT;
81
+ rb_str_cat(data, (char*)&header, sizeof(header));
82
+
83
+ item = rb_funcall(item, rb_intern("getgm"), 0);
84
+ item = rb_funcall(item, rb_intern("strftime"), 1, rb_str_new2("%Y/%m/%d %H:%M:%S +0000"));
85
+ len = RSTRING_LEN(item);
86
+ rb_str_cat(data, RSTRING_PTR(item), len);
87
+
88
+ null_pad(data, len);
72
89
  } else {
73
90
  if (item == Qnil) header[2] = NIL_SORT;
74
91
  else if (item == Qtrue) header[2] = TRUE_SORT;
@@ -134,12 +151,23 @@ static VALUE tuple_load(VALUE self, VALUE data) {
134
151
  case STR_SORT:
135
152
  case SYM_SORT:
136
153
  item = rb_str_new2(ptr);
137
- len = RSTRING_LEN(item);
154
+ len = RSTRING_LEN(item);
138
155
  while (len % 4 != 0) len++;
139
156
  ptr += len;
140
157
  if (header[2] == SYM_SORT) item = rb_funcall(item, rb_intern("to_sym"), 0);
141
158
  rb_ary_push(tuple, item);
142
159
  break;
160
+ case TIME_SORT:
161
+ item = rb_str_new2(ptr);
162
+ len = RSTRING_LEN(item);
163
+ while (len % 4 != 0) len++;
164
+ ptr += len;
165
+ item = rb_funcall(rb_cTime, rb_intern("parse"), 1, item);
166
+ rb_ary_push(tuple, item);
167
+ break;
168
+ default:
169
+ rb_raise(rb_eTypeError, "invalid type code %d in tuple", header[2]);
170
+ break;
143
171
  }
144
172
  }
145
173
  return tuple;
@@ -147,6 +175,8 @@ static VALUE tuple_load(VALUE self, VALUE data) {
147
175
 
148
176
  VALUE mTuple;
149
177
  void Init_tuple() {
178
+ rb_require("time");
179
+
150
180
  mTuple = rb_define_module("Tuple");
151
181
  rb_define_module_function(mTuple, "dump", tuple_dump, 1);
152
182
  rb_define_module_function(mTuple, "load", tuple_load, 1);
data/test/tuple_test.rb CHANGED
@@ -1,8 +1,15 @@
1
1
  require File.dirname(__FILE__) + '/test_helper'
2
2
 
3
+ class Time
4
+ def ==(other)
5
+ # Ignore microseconds for testing.
6
+ to_i == other.to_i
7
+ end
8
+ end
9
+
3
10
  class TupleTest < Test::Unit::TestCase
4
11
  should "dump and load arrays of simple types" do
5
- t = [1, true, :foo, "foo", -1001, false, nil]
12
+ t = [1, true, :foo, "foo", -1001, false, nil, Time.now]
6
13
  assert_equal t, Tuple.load(Tuple.dump(t))
7
14
  end
8
15
 
@@ -17,6 +24,8 @@ class TupleTest < Test::Unit::TestCase
17
24
  end
18
25
 
19
26
  should "sort tuples using binary" do
27
+ now = Time.now
28
+
20
29
  tuples = [
21
30
  [1, "foo"],
22
31
  [1, true],
@@ -35,6 +44,9 @@ class TupleTest < Test::Unit::TestCase
35
44
  ["charles", "atlas jr."],
36
45
  ["charles", "atlas", "world's", "strongest", "man"],
37
46
  ["charles", "atlas", 5],
47
+ [now, "foo"],
48
+ [now, "bar"],
49
+ [now - 24 * 60 * 60],
38
50
  ]
39
51
 
40
52
  expected = [
@@ -54,6 +66,9 @@ class TupleTest < Test::Unit::TestCase
54
66
  [:foo, -18446744073709551616],
55
67
  [:foo, -1],
56
68
  [:foo, 18446744073709551616],
69
+ [now - 24 * 60 * 60],
70
+ [now, "bar"],
71
+ [now, "foo"],
57
72
  [true]
58
73
  ]
59
74
  assert_equal expected, tuples.sort_by {|t| Tuple.dump(t)}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ninjudd-tuple
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Balthrop
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-18 00:00:00 -07:00
12
+ date: 2009-08-22 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15