[Mono-bugs] [Bug 59694][Nor] New - Test code gives different results when compiled with mcs and csc

bugzilla-daemon@bugzilla.ximian.com bugzilla-daemon@bugzilla.ximian.com
Mon, 7 Jun 2004 07:06:15 -0400 (EDT)


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by anders.gustafsson@cureos.com.

http://bugzilla.ximian.com/show_bug.cgi?id=59694

--- shadow/59694	2004-06-07 07:06:15.000000000 -0400
+++ shadow/59694.tmp.11288	2004-06-07 07:06:15.000000000 -0400
@@ -0,0 +1,291 @@
+Bug#: 59694
+Product: Mono: Compilers
+Version: unspecified
+OS: All
+OS Details: Windows XP Professional, Fedora Core 2 (Linux 2.6.5-1.358)
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Normal
+Component: C#
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: anders.gustafsson@cureos.com               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: Test code gives different results when compiled with mcs and csc
+
+Description of Problem:
+When running a test with NUnit (2.1.91), test results are different for mcs
+and csc compiled code. The example in question concerns a small class, Unit
+(see below), containing static operators * and /, where the class member
+variable is a System.Collections.SortedList. When running nunit-console
+under Mono, the division test fails (both under Windows XP and Fedora Core
+2), whereas when running NUnit-Gui for the corresponding test fixture build
+with C# Builder (csc compiler), both the multiplication and division test
+fails.
+
+Steps to reproduce the problem:
+
+1. Create a Mono DLL using the two source code files Unit.cs and
+UnitTest.cs below (Windows or Linux):
+mcs -target:library -out:unittest.dll -r:nunit.framework.dll Unit.cs
+UnitTest.cs
+
+2. Run nunit-console and examine the results:
+(in Windows) nunit-console.bat unittest.dll
+(in Linux) mono path-to-nunit-bin-directory/nunit-console.exe unittest.dll
+
+3. In Windows, build a corresponding csc compiled library using e.g. C#
+Builder, start NUnit-Gui, load and run unittest.dll and examine the results.
+
+Actual Results:
+
+The mcs compiled library fails the division test, whereas the csc compiled
+library fails both tests.
+
+**** Mono *******
+<?xml version="1.0" encoding="utf-8" standalone="no"?>
+<!--This file represents the results of running a test suite-->
+<test-results name="C:\Documents and Settings\Anders\My Documents\Borland
+Studio Projects\unittest\unittest-win.dll" total="2" failures="1"
+not-run="0" date="6/7/2004" time="12:30 PM">
+  <test-suite name="C:\Documents and Settings\Anders\My Documents\Borland
+Studio Projects\unittest\unittest-win.dll" success="False" time="0.08"
+asserts="0">
+    <results>
+      <test-suite name="UnitTest" success="False" time="0.05" asserts="0">
+        <results>
+          <test-case name="UnitTest.MultiplySameAtomicUnit" executed="True"
+success="True" time="0.010" asserts="1" />
+          <test-case name="UnitTest.DivideSameAtomicUnit" executed="True"
+success="False" time="0.010" asserts="0">
+            <failure>
+              <message><![CDATA[System.InvalidOperationException :
+SortedList.Enumerator: snapshot out of sync.]]></message>
+              <stack-trace><![CDATA[in <0x00055> Enumerator:MoveNext ()
+in <0x00045> Cureos.Core.Unit:op_Division (Cureos.Core.Unit,Cureos.Core.Unit)
+in <0x00037> UnitTest:DivideSameAtomicUnit ()
+in (unmanaged) (wrapper managed-to-native)
+System.Reflection.MonoMethod:InternalInvoke (object,object[])
+in <0x00004> (wrapper managed-to-native)
+System.Reflection.MonoMethod:InternalInvoke (object,object[])
+in <0x0008e> System.Reflection.MonoMethod:Invoke
+(object,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo)
+]]></stack-trace>
+            </failure>
+          </test-case>
+        </results>
+      </test-suite>
+    </results>
+  </test-suite>
+</test-results>
+
+***** csc compiled library ********
+<?xml version="1.0" encoding="utf-8" standalone="no"?>
+<!--This file represents the results of running a test suite-->
+<test-results name="C:\Documents and Settings\Anders\My Documents\Borland
+Studio Projects\unittest\bin\Debug\unittest.dll" total="2" failures="2"
+not-run="0" date="6/7/2004" time="12:32 PM">
+  <test-suite name="C:\Documents and Settings\Anders\My Documents\Borland
+Studio Projects\unittest\bin\Debug\unittest.dll" success="False"
+time="0.0600864" asserts="0">
+    <results>
+      <test-suite name="UnitTest" success="False" time="0.0600864" asserts="0">
+        <results>
+          <test-case name="UnitTest.DivideSameAtomicUnit" executed="True"
+success="False" time="0.010" asserts="0">
+            <failure>
+              <message><![CDATA[System.InvalidOperationException :
+Collection was modified; enumeration operation may not execute.]]></message>
+              <stack-trace><![CDATA[   at
+System.Collections.SortedListEnumerator.MoveNext()
+   at Cureos.Core.Unit.op_Division(Unit iLhs, Unit iRhs) in c:\Documents
+and Settings\Anders\My Documents\Borland Studio
+Projects\unittest\Unit.cs:line 85
+   at UnitTest.DivideSameAtomicUnit() in c:\Documents and
+Settings\Anders\My Documents\Borland Studio
+Projects\unittest\UnitTest.cs:line 27
+]]></stack-trace>
+            </failure>
+          </test-case>
+          <test-case name="UnitTest.MultiplySameAtomicUnit" executed="True"
+success="False" time="0.000" asserts="0">
+            <failure>
+              <message><![CDATA[System.InvalidOperationException :
+Collection was modified; enumeration operation may not execute.]]></message>
+              <stack-trace><![CDATA[   at
+System.Collections.SortedListEnumerator.MoveNext()
+   at Cureos.Core.Unit.op_Multiply(Unit iLhs, Unit iRhs) in c:\Documents
+and Settings\Anders\My Documents\Borland Studio
+Projects\unittest\Unit.cs:line 61
+   at UnitTest.MultiplySameAtomicUnit() in c:\Documents and
+Settings\Anders\My Documents\Borland Studio
+Projects\unittest\UnitTest.cs:line 17
+]]></stack-trace>
+            </failure>
+          </test-case>
+        </results>
+      </test-suite>
+    </results>
+  </test-suite>
+</test-results>
+
+Expected Results:
+The mcs library should fail both test cases (I think, I have not been able
+to completely resolve the bug in my code yet, at this moment I cannot
+realize why either test should fail, but that issue is primarily related to
+me learning C# :-)
+
+How often does this happen? 
+Always.
+
+Additional Information:
+
+Source code
+
+***** Unit.cs
+// project created on 6/4/2004 at 2:09 PM
+using System;
+using System.Collections;
+
+
+namespace Cureos {
+
+	namespace Core {
+	
+		/// <summary>
+		/// General unit representation.
+		/// Any type of atomic or compound unit can be represented, e.g. physical
+		/// and dosimetric units.
+		/// </summary>
+		public class Unit
+		{
+			private SortedList mAtomicUnitList;
+			
+			/// <summary>Empty constructor</summary>
+			public Unit()
+			{
+				mAtomicUnitList = new SortedList();
+			}
+			
+			/// <summary>Atomic unit constructor</summary>
+			/// <param name="iLabel">Atomic unit label</param>
+			public Unit(string iLabel)
+			{
+				mAtomicUnitList = new SortedList();
+				mAtomicUnitList.Add(iLabel, 1);
+			}
+			
+			/// <summary>Compound unit constructor</summary>
+			/// <param name="iAtomicUnitList">List of atomic units and powers to
+make up the compound unit</param>
+			public Unit(SortedList iAtomicUnitList)
+			{
+				mAtomicUnitList = iAtomicUnitList;
+			}
+			
+			/// <summary>Unit label property</summary>
+			public string Label
+			{
+				get {
+					string s = "";
+					foreach (DictionaryEntry entry in mAtomicUnitList) {
+						s += " " + (string) entry.Key;
+						if ((int) entry.Value != 1) {
+							s += "^" + Convert.ToString(entry.Value);
+						}
+					}
+					return s;
+				}
+			}
+			
+			/// <summary>Multiply two units</summary>
+			/// <param name="iLhs">Left-hand side Unit subject to multiplication</param>
+			/// <param name="iRhs">Right-hand side Unit subject to
+multiplication</param>
+			public static Unit operator*(Unit iLhs, Unit iRhs)
+			{
+				Unit ret = iLhs;
+				foreach (DictionaryEntry entry in iRhs.mAtomicUnitList) {
+					if (ret.mAtomicUnitList.Contains(entry.Key)) {
+						int exponent = (int) ret.mAtomicUnitList[entry.Key] + (int) entry.Value;
+						if (exponent != 0) {
+							ret.mAtomicUnitList[entry.Key] = exponent;
+						}
+						else {
+							ret.mAtomicUnitList.Remove(entry.Key);
+						}
+					}
+					else {
+						ret.mAtomicUnitList.Add(entry.Key, entry.Value);
+					}
+				}
+				
+				return ret;
+			}
+
+			/// <summary>Divide two units</summary>
+			/// <param name="iLhs">Dividend Unit</param>
+			/// <param name="iRhs">Divisor Unit</param>
+			public static Unit operator/(Unit iLhs, Unit iRhs)
+			{
+				Unit ret = iLhs;
+				foreach (DictionaryEntry entry in iRhs.mAtomicUnitList) {
+					if (ret.mAtomicUnitList.Contains(entry.Key)) {
+						int exponent = (int) ret.mAtomicUnitList[entry.Key] - (int) entry.Value;
+						if (exponent != 0) {
+							ret.mAtomicUnitList[entry.Key] = exponent;
+						}
+						else {
+							ret.mAtomicUnitList.Remove(entry.Key);
+						}
+					}
+					else {
+						ret.mAtomicUnitList.Add(entry.Key, -(int) entry.Value);
+					}
+				}
+				
+				return ret;
+			}
+			
+		}
+		
+	}
+
+}
+
+
+**** UnitTest.cs
+// project created on 6/4/2004 at 3:05 PM
+using Cureos.Core;
+
+using System;
+using System.Collections;
+using NUnit.Framework;
+
+
+[TestFixture]
+public class UnitTest
+{
+	[Test]
+	public void MultiplySameAtomicUnit()
+	{
+		Unit u1 = new Unit("m");
+		
+		Unit u2 = u1 * u1;
+		
+		Assert.AreEqual(" m^2", u2.Label);
+	}
+
+	[Test]
+	public void DivideSameAtomicUnit()
+	{
+		Unit u1 = new Unit("m");
+		
+		Unit u2 = u1 / u1;
+		
+		Assert.AreEqual("", u2.Label);
+	}
+}