Why is my C# VS 2008 Unit Test failing?

Assert.AreEqual failed. Expected:<System.Int32[,]>. Actual:<S

Page 1 of 1

3 Replies - 4815 Views - Last Post: 19 June 2009 - 08:16 AM Rate Topic: -----

#1 isaacde   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-June 09

Why is my C# VS 2008 Unit Test failing?

Posted 19 June 2009 - 07:42 AM

Hi Everyone,

I've been having a frustrating time this morning trying to use the Visual Studio 2008 Professional Unit Testing framework. For some reason an Assert is failing that shouldn't be as far as I can tell.

Here's what I'm working with, in simplified version:

int[,] expected = { { 10, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 10 } };
int[,] wtfexpected = { { 10, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 10 } };
Assert.AreEqual(expected, wtfexpected);



This returns:
Assert.AreEqual failed. Expected:<System.Int32[,]>. Actual:<System.Int32[,]>.

I can replace the last line with:
Assert.AreEqual<int[,]>(expected, wtfexpected);


And it does not change the result.

However if I use my target object's property 'stones' of type int[,] instead:
BoardState target = new BoardState();
int[,] expected = { { 10, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 10 } };
int[,] actual;
target.Stones = expected;
actual = target.Stones;
Assert.AreEqual(expected, actual);


The assert works.

Here's some code from in my BoardState class:
	public class BoardState
	{
		private int[,] stonesDistribution;

		public BoardState()
		{
			//initialize the board
			//stones array shows where the stones are
			stonesDistribution = new int[4,4]{ { 10, 0, 0, 0 } , { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 10 } };
		}
		public int[,] Stones
		{
			get
			{
				return stonesDistribution;
			}
			set
			{
				stonesDistribution = value;
			}
		}
	 }



Can anybody help me out?

Is This A Good Question/Topic? 0
  • +

Replies To: Why is my C# VS 2008 Unit Test failing?

#2 egof   User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 132
  • Joined: 27-March 09

Re: Why is my C# VS 2008 Unit Test failing?

Posted 19 June 2009 - 08:07 AM

When your doing the comparison its doing the memory address and not the actual values.
Was This Post Helpful? 0
  • +
  • -

#3 SwiftStriker00   User is offline

  • No idea why my code works
  • member icon

Reputation: 439
  • View blog
  • Posts: 1,617
  • Joined: 25-December 08

Re: Why is my C# VS 2008 Unit Test failing?

Posted 19 June 2009 - 08:14 AM

Ok, that is weird. I tried this
[TestMethod]
		public void dichelp() {
			int[,] expected = { { 10, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 10 } };
			int[,] wtfexpected = { { 10, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 10 } };

			for( int i = 0; i < 4; i++ ) {
				for( int j = 0; j < 4; j++ ) {
					Assert.AreEqual(expected[i,j], wtfexpected[i,j]);
				} 
			}
		}


And that passed.


What I think is happening is there no compareTo<T>() method for int[,], and even if there is its not going to matter. When you create an array you set X amount of memory where X = sizeOftype*sizeOfarray. Any the computer is most likely placing expected at [MEM:0000] and wtfexpected at [MEM: 1001] for example, and when you compare them, its just returning the start index of the int[,].

The reason it works when you pass it into your stones, is that your actually making a reference and not a new one. Essentially pointers to the same orignial object at [MEM:0000].
Was This Post Helpful? 1
  • +
  • -

#4 isaacde   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-June 09

Re: Why is my C# VS 2008 Unit Test failing?

Posted 19 June 2009 - 08:16 AM

Awesome, thank you that makes sense. I'll just compare item by item :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1