test an ordered hash and encode them specially in c

This commit is contained in:
Mike Dirolf 2009-03-05 14:58:34 -05:00
parent 1d0fe595c8
commit e93b02137c
2 changed files with 23 additions and 1 deletions

View File

@ -289,7 +289,18 @@ static void write_doc(bson_buffer* buffer, VALUE hash) {
int start_position = buffer->position; int start_position = buffer->position;
int length_location = buffer_save_bytes(buffer, 4); int length_location = buffer_save_bytes(buffer, 4);
// we have to check for an OrderedHash and handle that specially
if (strcmp(rb_class2name(RBASIC(hash)->klass), "OrderedHash") == 0) {
VALUE keys = rb_funcall(hash, rb_intern("keys"), 0);
int i;
for(i = 0; i < RARRAY(keys)->len; i++) {
VALUE key = RARRAY(keys)->ptr[i];
VALUE value = rb_hash_aref(hash, key);
write_element(key, value, (VALUE)buffer);
}
} else {
rb_hash_foreach(hash, write_element, (VALUE)buffer); rb_hash_foreach(hash, write_element, (VALUE)buffer);
}
// write null byte and fill in length // write null byte and fill in length
buffer_write_bytes(buffer, &zero, 1); buffer_write_bytes(buffer, &zero, 1);

View File

@ -1,5 +1,6 @@
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib') $LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
require 'mongo' require 'mongo'
require 'mongo/util/ordered_hash'
require 'test/unit' require 'test/unit'
class BSONTest < Test::Unit::TestCase class BSONTest < Test::Unit::TestCase
@ -37,6 +38,16 @@ class BSONTest < Test::Unit::TestCase
assert_equal doc, @b.deserialize assert_equal doc, @b.deserialize
end end
def test_ordered_hash
doc = OrderedHash.new
doc["b"] = 1
doc["a"] = 2
doc["c"] = 3
doc["d"] = 4
@b.serialize(doc)
assert_equal doc, @b.deserialize
end
def test_object def test_object
doc = {'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}} doc = {'doc' => {'age' => 42, 'name' => 'Spongebob', 'shoe_size' => 9.5}}
@b.serialize(doc) @b.serialize(doc)