iphone - Comparing two arrays for equivilant objects with Kiwi (Sentesting) Kit -


i want compare 2 arrays equivalent objects, 1 property inside class , other in test method.

i cannot compare directly since objects allocated seperately , have different memory locations.

to around implemented description on object list out properties in string: (vel cgpoint)

- (nsstring *)description { return [nsstring stringwithformat:@"vel:%.5f%.5f",vel.x,vel.y]; } 

i test with:

nslog(@"movearray description: %@",[movearray description]); nslog(@"currentmoves description: %@", [p.currentmoves description]);  [[thevalue([movearray description]) should] equal:thevalue([p.currentmoves description])]; 

my nslog's yield:

project[13083:207] movearray description: ( "vel:0.38723-0.92198" )  project[13083:207] currentmoves description: ( "vel:0.38723-0.92198" ) 

but test fails:

/projectpath/objecttest.m:37: error: -[objecttest example] : 'object should pass test' [failed], expected subject equal <9086b104>, got <7099e004> 

thevalue initializes kwvalue bytes , objective-c type , sets value with

- (id)initwithbytes:(const void *)bytes objctype:(const char *)anobjctype { if ((self = [super init])) {     objctype = anobjctype;     value = [[nsvalue alloc] initwithbytes:bytes objctype:anobjctype]; }  return self; } 

how can compare these 2 arrays have objects of equivalent values?

your test fails because comparing pointer addresses, not values.

you can iterate through 1 array , compare each object against equivalently positioned object in second array. make sure comparison done correctly type of value comparing. if every element has different type gets trickier.

// in class - (bool)comparevelocitiesinarray:(nsarray *)array1 witharray:(nsarray *)array2 {     bool result = yes;      (uint = 0; < [array1 count]; i++) {         customobject *testobj1 = [array1 objectatindex:i]         customobject *testobj2 = [array2 objectatindex:i]          // perform test here ...         if ([testobj1 velocityasfloat] != [testobj2 velocityasfloat]) {             result = no;         }     }      return result; }  // in class nsarray *myarray = [nsarray arraywithobjects:obj1, obj2, nil]; nsarray *myotherarray = [nsarray arraywithobjects:obj3, obj4, nil]; bool result;  result = [self comparevelocitiesinarray:myarray witharray:myotherarray]; nslog(@"do arrays pass test? %@", result ? @"yes" : @"no"); 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -