Re: Having difficulties using Is.EquivelentTo() toocompare two collections

"Charlie Poole" <[email protected]> Thu, 7 Aug 2008 12:16:28 -0700
Newsgroups gmane.comp.windows.dotnet.nunit.user
Message-ID <003a01c8f8c2$190ee6d0$6401a8c0@ferrari>
I think you need to do more in overriding GetHashCode()
by incorporating the hashes for the members you use to 
differentiate instances. Using object's implementation
is the same as making object identity the criterion
for equality.

However, although you should really do that, I can't
guarantee that it will solve the problem because I forgot
to ask what version of NUnit you are using - the implementation
I described is in relatively recent versions only.

Charlie 

> -----Original Message-----
> From: Ryan Andrus [mailto:[email protected]] 
> Sent: Thursday, August 07, 2008 12:02 PM
> To: 'Charlie Poole'; [email protected]
> Subject: RE: [Nunit-users] Having difficulties using 
> Is.EquivelentTo() toocompare two collections
> 
> Equals has been correctly overridden but I'm not sure if 
> GetHashCode is done correctly. GetHashCode simply calls the 
> base get hash code which calls the base, etc... and I'd 
> assume it's using object.GetHashCode(). Code is below:
> 
> 		public override bool Equals(object obj)
> 		{
> 			VibratorIdPeltonMessage other =
> ((VibratorIdPeltonMessage)(obj));
> 			if ((other != null))
> 			{
> 				return ((((base.Equals(obj) 
> 					 &&
> CrewNumber.Equals(other.CrewNumber)) 
> 					 && Radio.Equals(other.Radio)) 
> 					 && 
> MsgCount.Equals(other.MsgCount))
> 
> 					 &&
> Vibrators.Equals(other.Vibrators));
> 			}
> 			return base.Equals(obj);
> 		}
> 
> 		public override int GetHashCode()
> 		{
> 			return base.GetHashCode();
> 		}
> 
> -----Original Message-----
> From: Charlie Poole [mailto:[email protected]]
> Sent: Thursday, August 07, 2008 1:48 PM
> To: 'Ryan Andrus'; [email protected]
> Subject: RE: [Nunit-users] Having difficulties using 
> Is.EquivelentTo() toocompare two collections
> 
> The way CollectionEquivalentConstraint works is as follows:
> 1) The expected objects are put into a hashtabe, using the 
> objects themseves as keys and with the values being the 
> number of occurences of each object.
> 2) The actual collection objects are applied to the same 
> hashtable, decrementing the counts.
> 3) The assertion succeeds if all the objects in the hash 
> table reach zero.
> 
> For this to work, your objects must override Equals() and 
> GetHashCode(). Have you done that?
> 
> Charlie
> 
> > -----Original Message-----
> > From: [email protected]
> > [mailto:[email protected]] On 
> Behalf Of Ryan 
> > Andrus
> > Sent: Thursday, August 07, 2008 10:14 AM
> > To: [email protected]
> > Subject: [Nunit-users] Having difficulties using
> > Is.EquivelentTo() toocompare two collections
> > 
> > Hi,
> > 
> > I'm having difficulties using Is.EquivelentTo() too compare two 
> > collections.
> > My collections are the same which I've manually verified from the 
> > output of the Assert but the Assert is throwing an 
> exception stating 
> > that they are not the same. I'm comparing two list of 
> PeltonMessages 
> > and I've set a break point in the Equals method for the 
> various types 
> > of pelton messages in my test and the break point is never 
> hit so I'm 
> > a bit confused about how this comparison is done. Can 
> someone please 
> > explain how the comparison is done so that I can determine 
> if we need 
> > to update our business objects? Or please review my code and let me 
> > know if I'm just using this constraint wrong?
> > 
> > Code
> > ---------------------------------------------------------------
> >       [Test]
> >       //[Ignore("Under developement")]
> >       public void MergeSendParamsRequests_NoMatches()
> >       {
> >          // Old singler request
> >          Ion.LisdCore.SourceControllers.PeltonRequestType
> > oldRequestType 
> >             =
> > Ion.LisdCore.SourceControllers.PeltonRequestType.SendParams;
> >          PeltonRequestPriority oldPriority = 
> > PeltonRequestPriority.High;
> >          List<PeltonMessage> oldMessages = new 
> List<PeltonMessage>();
> >          SourceControllerNumber oldSrcId = 
> (SourceControllerNumber)2;
> >          CommandSentNotification oldCallback = new 
> > CommandSentNotification(On_CommandSentNotification);
> >          oldMessages.Add(new VibratorIdPeltonMessage());
> >          oldMessages.Add(new SweepStartPeltonMessage());
> >          oldMessages.Add(new SweepAbortPeltonMessage());
> >          oldMessages.Add(new EndPeltonMessage());
> > 
> >          // New single request
> >          Ion.LisdCore.SourceControllers.PeltonRequestType
> > newRequestType 
> >             =
> > Ion.LisdCore.SourceControllers.PeltonRequestType.SendParams;
> >          PeltonRequestPriority newPriority = 
> > PeltonRequestPriority.High;
> >          List<PeltonMessage> newMessages = new 
> List<PeltonMessage>();
> >          SourceControllerNumber newSrcId = 
> (SourceControllerNumber)2;
> >          CommandSentNotification newCallback = new 
> > CommandSentNotification(On_CommandSentNotification);
> >          newMessages.Add(new VibratorIdPeltonMessage());
> >          newMessages.Add(new HelloPeltonMessage());
> >          newMessages.Add(new StartCodePeltonMessage());
> >          newMessages.Add(new EndPeltonMessage());
> > 
> >          // Expected SingleRequest.Messages
> >          List<PeltonMessage> expectedMessages = new 
> > List<PeltonMessage>();
> >          expectedMessages.Add(new VibratorIdPeltonMessage());
> >          expectedMessages.Add(new SweepStartPeltonMessage());
> >          expectedMessages.Add(new SweepAbortPeltonMessage());
> >          expectedMessages.Add(new HelloPeltonMessage());
> >          expectedMessages.Add(new StartCodePeltonMessage());
> >          expectedMessages.Add(new EndPeltonMessage());
> >          
> >          SingleRequest oldSingleRequest = new 
> > SingleRequest(oldRequestType, oldPriority,
> >                                                    
> > oldMessages, oldSrcId, null, null, null, oldCallback);
> >          SingleRequest newSingleRequest = new 
> > SingleRequest(newRequestType, newPriority,
> >                                                    
> > newMessages, newSrcId, null, null, null, newCallback);
> > 
> >          SingleRequest.MergeSendParamsRequests(oldSingleRequest,
> > newSingleRequest);
> > 
> >          Assert.That(oldSingleRequest.messages,
> > Is.EquivalentTo(expectedMessages), 
> >                         "oldSingleRequest.messages didn't match the 
> > expected values.");
> >       }
> > 
> > Error Messsage
> > ---------------------------------------------------------------
> > ------ Test started: Assembly: SourceControllerTests.dll ------
> > 
> > TestCase
> > 'SourceControllerTests.CollisionManagementBaseTests.MergeSendP
> > aramsRequests_
> > NoMatches'
> > failed: 
> >   oldSingleRequest.messages didn't match the expected values.
> >   Expected: equivalent to < <   MsgId =32
> >    Name =VibratorIdPeltonMessage
> >    Port =0
> >    Startchar =163
> >    MsgId =32
> >    AddrOffset =0
> >    SendOverSecondaryChannel =False
> >    CrewNumber =0
> >    Radio =False
> >    MsgCount =2
> >    Vibrators =4294967295
> > >, <   MsgId =11
> >    Name =SweepStartPeltonMessage
> >    Port =0
> >    Startchar =163
> >    MsgId =11
> >    AddrOffset =0
> >    SendOverSecondaryChannel =False
> >    filler0 =S
> >    filler1 =W
> >    filler2 =E
> >    filler3 =E
> >    filler4 =P
> >    filler5 =S
> >    filler6 =T
> >    filler7 =A
> >    filler8 =R
> >    filler9 =T
> > >, <   MsgId =12
> >    Name =SweepAbortPeltonMessage
> >    Port =0
> >    Startchar =163
> >    MsgId =12
> >    AddrOffset =0
> >    SendOverSecondaryChannel =False
> >    filler0 =A
> >    filler1 =B
> >    filler2 =O
> >    filler3 =R
> >    filler4 =T
> >    filler5 =S
> >    filler6 =W
> >    filler7 =E
> >    filler8 =E
> >    filler9 =P
> > >, <   MsgId =8
> >    Name =HelloPeltonMessage
> >    Port =0
> >    Startchar =163
> >    MsgId =8
> >    AddrOffset =0
> >    SendOverSecondaryChannel =False
> >    EncoderNumber =0
> >    UnitSerial =0
> >    DSP =
> >    MCU =
> >    OperatingMode =0
> >    ModemInfo =
> >    ModemSerial =0
> >    ModemSetup =0
> >    ModemPSS =0
> > >, <   MsgId =10
> >    Name =StartCodePeltonMessage
> >    Port =0
> >    Startchar =163
> >    MsgId =10
> >    AddrOffset =0
> >    SendOverSecondaryChannel =False
> >    FleetNumber =0
> >    CrewNumber =0
> >    EncoderMode =Normal
> >    DelayedPSS =False
> >    DecoderSweepLen =0
> >    SweepID =0
> >    SweepNumber =0
> >    StoredSweep =False
> >    LowForce =False
> >    StartCodeNumber =0
> >    DecodersEnabled =0
> >    VibSimilarity =0
> >    EP =0
> >    ShotID =0
> >    Line =0
> >    Station =0
> >    LastSweepIndex =0
> >    LastEnabledDecoders =0
> >    NextSweepNumber =0
> >    RetransmitMissedPss =False
> >    RetransmitSweepIndex =0
> >    RetransmitDecoders =0
> >    SweepProfileNumber =1
> >    ZeroWaitPSS =True
> >    ExtraByte =False
> > >, <   MsgId =1
> >    Name =EndPeltonMessage
> >    Port =0
> >    Startchar =163
> >    MsgId =1
> >    AddrOffset =0
> >    SendOverSecondaryChannel =False
> > > >
> >   But was:  < <   MsgId =32
> >    Name =VibratorIdPeltonMessage
> >    Port =0
> >    Startchar =163
> >    MsgId =32
> >    AddrOffset =0
> >    SendOverSecondaryChannel =False
> >    CrewNumber =0
> >    Radio =False
> >    MsgCount =2
> >    Vibrators =4294967295
> > >, <   MsgId =11
> >    Name =SweepStartPeltonMessage
> >    Port =0
> >    Startchar =163
> >    MsgId =11
> >    AddrOffset =0
> >    SendOverSecondaryChannel =False
> >    filler0 =S
> >    filler1 =W
> >    filler2 =E
> >    filler3 =E
> >    filler4 =P
> >    filler5 =S
> >    filler6 =T
> >    filler7 =A
> >    filler8 =R
> >    filler9 =T
> > >, <   MsgId =12
> >    Name =SweepAbortPeltonMessage
> >    Port =0
> >    Startchar =163
> >    MsgId =12
> >    AddrOffset =0
> >    SendOverSecondaryChannel =False
> >    filler0 =A
> >    filler1 =B
> >    filler2 =O
> >    filler3 =R
> >    filler4 =T
> >    filler5 =S
> >    filler6 =W
> >    filler7 =E
> >    filler8 =E
> >    filler9 =P
> > >, <   MsgId =8
> >    Name =HelloPeltonMessage
> >    Port =0
> >    Startchar =163
> >    MsgId =8
> >    AddrOffset =0
> >    SendOverSecondaryChannel =False
> >    EncoderNumber =0
> >    UnitSerial =0
> >    DSP =
> >    MCU =
> >    OperatingMode =0
> >    ModemInfo =
> >    ModemSerial =0
> >    ModemSetup =0
> >    ModemPSS =0
> > >, <   MsgId =10
> >    Name =StartCodePeltonMessage
> >    Port =0
> >    Startchar =163
> >    MsgId =10
> >    AddrOffset =0
> >    SendOverSecondaryChannel =False
> >    FleetNumber =0
> >    CrewNumber =0
> >    EncoderMode =Normal
> >    DelayedPSS =False
> >    DecoderSweepLen =0
> >    SweepID =0
> >    SweepNumber =0
> >    StoredSweep =False
> >    LowForce =False
> >    StartCodeNumber =0
> >    DecodersEnabled =0
> >    VibSimilarity =0
> >    EP =0
> >    ShotID =0
> >    Line =0
> >    Station =0
> >    LastSweepIndex =0
> >    LastEnabledDecoders =0
> >    NextSweepNumber =0
> >    RetransmitMissedPss =False
> >    RetransmitSweepIndex =0
> >    RetransmitDecoders =0
> >    SweepProfileNumber =1
> >    ZeroWaitPSS =True
> >    ExtraByte =False
> > >, <   MsgId =1
> >    Name =EndPeltonMessage
> >    Port =0
> >    Startchar =163
> >    MsgId =1
> >    AddrOffset =0
> >    SendOverSecondaryChannel =False
> > > >
> > 	
> > C:\code\Firefly\Trunk_CollisionManagementTesting\CSC\Common.NE
> > T\SourceContro
> > llerTests\CollisionManagementBaseTests.cs(78,0): at 
> > SourceControllerTests.CollisionManagementBaseTests.MergeSendPa
> > ramsRequests_N
> > oMatches()
> > 
> > 
> > 0 passed, 1 failed, 0 skipped, took 6.08 seconds.
> > 
> > 
> > 
> > 
> > --------------------------------------------------------------
> > -----------
> > This SF.Net email is sponsored by the Moblin Your Move Developer's 
> > challenge Build the coolest Linux based applications with 
> Moblin SDK & 
> > win great prizes Grand prize is a trip for two to an Open 
> Source event 
> > anywhere in the world 
> > http://moblin-contest.org/redirect.php?banner_id=100&url=/
> > _______________________________________________
> > Nunit-users mailing list
> > [email protected]
> > https://lists.sourceforge.net/lists/listinfo/nunit-users
> > 
> 
> 
> 



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/