Browse files

additions to allow NuBSONObjectIDs to be used as dictionary keys

  • Loading branch information...
1 parent 1119e5d commit fa78cee2915f775fd08ab503ba9422df77885266 Tim Burks committed Dec 22, 2010
Showing with 21 additions and 3 deletions.
  1. +9 −0 objc/NuBSON.m
  2. +12 −3 test/test_bson.nu
View
9 objc/NuBSON.m
@@ -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];
View
15 test/test_bson.nu
@@ -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.