Skip to content

Commit

Permalink
additions to allow NuBSONObjectIDs to be used as dictionary keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Burks committed Dec 22, 2010
1 parent 1119e5d commit fa78cee
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
9 changes: 9 additions & 0 deletions objc/NuBSON.m
Expand Up @@ -162,6 +162,15 @@ - (id) initWithData:(NSData *) data
return self;
}

- (id) copyWithZone:(NSZone *) zone
{
return [[[self class] allocWithZone:zone] initWithObjectIDPointer:&oid];
}

- (NSInteger) hash {
oid.ints[0] + oid.ints[1] + oid.ints[2];
}

- (NSData *) dataRepresentation
{
return [[[NSData alloc] initWithBytes:oid.bytes length:12] autorelease];
Expand Down
15 changes: 12 additions & 3 deletions test/test_bson.nu
Expand Up @@ -47,7 +47,16 @@
(- testOIDs is
(10 times:
(do (i)
(set id ((NuBSONObjectID objectID)))
(set id1 ((NuBSONObjectID objectID)))
(set id2 ((NuBSONObjectID alloc)
initWithData:(id dataRepresentation)))
(assert_equal id id2)))))
initWithData:(id1 dataRepresentation)))
(assert_equal id1 id2)

(set id3 (NuBSONObjectID new))
(set id4 (NuBSONObjectID new))

(set d (dict id1 123 id3 456))
(assert_equal 123 (d id1))
(assert_equal 123 (d id2))
(assert_equal 456 (d id3))
(assert_equal 456 (d id4))))))

0 comments on commit fa78cee

Please sign in to comment.