1

I'm running into some issues when writing a new unit test. My comments in the code example explain my specific issue:

KeyboardLayout layout
    = Engine.ParseLayout(Dimensions.Create(300, 300), "FooBar", provider);
var dweeb = layout.Layout.SelectMany(t => t);

//Parsed layout is not a mutant
Assert.IsTrue(!layout.IsMutant);

KeyboardLayout newLayout = StandardKeyboardMutations.MutateKeyboard(layout);

var newb = newLayout.Layout.SelectMany(t => t);

//I don't understand why this test fails. They should be stored in seperate memeory locations and therefore not equal by reference comparison.
//Unless of course saying var x = foo doesn't always provide a fresh memory location for the value of foo.
Assert.AreNotEqual(layout, newLayout);

this line Assert.AreNotEqual(layout, newLayout); always fails.

I don't know why I've been making this assumption (stated in comments) up until now: Declaring a variable always executes a store procedure to a fresh memory location. (one not being used by a currently running program).

To be clear, the question is, in c#, does var x = foo; always use a fresh memory cell? If not, what is the criteria for using one? Or maybe it always stores in x the address of the value? Or have I missed something and I am asking the wrong question entirely to the find what is wrong here?

This is my first "official" unit testing experience, Thanks in advance.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
lonious
  • 676
  • 9
  • 25
  • I can't find any documentation on `StandardKeyboardMutations`. Do you have a reference? – lc. Jan 27 '14 at 04:17
  • Custom class. I'd have to come up with a varied example if you want to look at its' implementation. – lonious Jan 27 '14 at 04:29
  • The answer probably lies in its implementation though. Unless you've overridden `KeyboardLayout.Equals` you're getting the `ReferenceEquals` implementation. Which means somehow `StandardKeyboardMutations.MutateKeyboard` is returning a reference to *the same object* as `layout` - probably *not* what you intend. – lc. Jan 27 '14 at 04:35

2 Answers2

3

You are looking for Assert.AreSame and not Assert.AreEqual.

The Assert.AreSame() assertion checks that the arguments reference the same objects (you mentioned "same memory location"), while the Assert.AreEqual() (and respectively AreNotEqual()), assertion checks for the equality of the underlying objects, ie., it checks that a.Equals(b), which can hold true even if a and b reference different objects.

mockinterface
  • 14,452
  • 5
  • 28
  • 49
  • This is good information. However, I used the Assert.AreNotSame(); method and the assertion still failed. These must be referencing the same object even though my example code leads me to believe otherwise. – lonious Jan 27 '14 at 04:36
  • If they are nevertheless the same then it is possible that the `StandardKeyboardMutations` utilises the [Flyweight pattern](http://en.wikipedia.org/wiki/Flyweight_pattern) to share the mutations it hands out. It would be best if you could expand the question to include the said implementation or relevant portions of it. – mockinterface Jan 27 '14 at 04:53
2

does var x = foo; always use a fresh memory cell?

Yes, x is always new peace of memory. However, it doesn't necessary mean what you think it means.

Or maybe it always stores in x the address of the value?

Yes, if value type is a reference type. e.g.

var x = new MyClass();
var y = x;

both x and y contain reference (you can think of it as an address) to the same peace of memory, where MyClass() instance is actually stored.

For value types it's quite different:

var x = new MyStruct();
var y = x;

The entire object is copied when assigned to y. There is no reference, the object itself is stored in y.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • I suspect OP actually needed something else. But good start :) what about optimizations and all sorts of aliasing where 2 variables share memory location, boxing... Simple link to Eric Lipperts blog like [Value types](http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx) may be enough... – Alexei Levenkov Jan 27 '14 at 04:23
  • So would you say that in my example code if both dweeb and newb had reference types on the right hand side of the initialization = that they could be referencing the same thing? – lonious Jan 27 '14 at 04:41
  • I don't think so, they refer to LINQ in-memory query definition. The reason why you're getting then as equal is because you're using wrong `Assert` method (as the other answer states). – MarcinJuraszek Jan 27 '14 at 04:46