Author: martin
Date: 2005-03-07 01:01:26 -0500 (Mon, 07 Mar 2005)
New Revision: 41501

Added:
   trunk/mcs/tests/gen-130.cs
   trunk/mcs/tests/gen-131.cs
   trunk/mcs/tests/gen-132.cs
   trunk/mcs/tests/gen-133.cs
   trunk/mcs/tests/gen-134.cs
Modified:
   trunk/mcs/tests/ChangeLog
   trunk/mcs/tests/Makefile
Log:
2005-03-07  Martin Baulig  <[EMAIL PROTECTED]>

        * gen-130.cs, gen-131.cs, gen-132.cs, gen-133.cs, gen-134.cs: New
        test cases for Nullable Types.  Thanks a lot to Peter Sestoft :-)



Modified: trunk/mcs/tests/ChangeLog
===================================================================
--- trunk/mcs/tests/ChangeLog   2005-03-07 05:55:39 UTC (rev 41500)
+++ trunk/mcs/tests/ChangeLog   2005-03-07 06:01:26 UTC (rev 41501)
@@ -1,3 +1,8 @@
+2005-03-07  Martin Baulig  <[EMAIL PROTECTED]>
+
+       * gen-130.cs, gen-131.cs, gen-132.cs, gen-133.cs, gen-134.cs: New
+       test cases for Nullable Types.  Thanks a lot to Peter Sestoft :-)
+
 2005-03-04  Raja R Harinath  <[EMAIL PROTECTED]>
 
        * Makefile (TEST_HARNESS_EXTRAS): Add test-353-2.cs.

Modified: trunk/mcs/tests/Makefile
===================================================================
--- trunk/mcs/tests/Makefile    2005-03-07 05:55:39 UTC (rev 41500)
+++ trunk/mcs/tests/Makefile    2005-03-07 06:01:26 UTC (rev 41501)
@@ -117,7 +117,8 @@
        gen-91  gen-92  gen-93  gen-94  gen-95  gen-96  gen-97                  
gen-100 \
        gen-101 gen-102 gen-103 gen-104 gen-105 gen-106 gen-107 gen-108 gen-109 
gen-110 \
        gen-111 gen-112 gen-113 gen-114 gen-115 gen-116 gen-117 gen-118 gen-119 
gen-120 \
-       gen-121 gen-122 gen-123 gen-124 gen-125         gen-127 gen-128 gen-129
+       gen-121 gen-122 gen-123 gen-124 gen-125         gen-127 gen-128 gen-129 
gen-130 \
+               gen-132 gen-133 gen-134
 
 # gen-72 fails after the runtime changes in r40305
 TEST_EXCLUDES_net_2_0 = $(NEW_TEST_SOURCES_common) gen-72

Added: trunk/mcs/tests/gen-130.cs
===================================================================
--- trunk/mcs/tests/gen-130.cs  2005-03-07 05:55:39 UTC (rev 41500)
+++ trunk/mcs/tests/gen-130.cs  2005-03-07 06:01:26 UTC (rev 41501)
@@ -0,0 +1,29 @@
+//-- ex-nullable-bool
+
+using System;
+
+class MyTest {
+  public static void Main(String[] args) {
+    Console.WriteLine("Note that null prints as blank or []\n");
+    bool? b1 = null, b2 = false, b3 = true;
+    bool? b4 = b1^b2, b5 = b1&b2, b6 = b1|b2;                     // null 
false null
+    Console.WriteLine("[{0}] [{1}] [{2}]", b4, b5, b6);
+    bool? b7 = b1^b3, b8 = b1&b3, b9 = b1|b3;                     // null null 
true
+    Console.WriteLine("[{0}] [{1}] [{2}]", b7, b8, b9);
+    Console.WriteLine(b1 ? "null is true" : "null is false");     // null is 
false
+    Console.WriteLine(!b1 ? "!null is true" : "!null is false");  // !null is 
false
+
+    Console.WriteLine();
+    bool?[] bvals = new bool?[] { null, false, true };
+    Console.WriteLine("{0,-6} {1,-6} {2,-6} {3,-6} {4,-6}", 
+                      "x", "y", "x&y", "x|y", "x^y");
+    foreach (bool? x in bvals) 
+      foreach (bool? y in bvals) 
+        Console.WriteLine("{0,-6} {1,-6} {2,-6} {3,-6} {4,-6}", 
+                          x, y, x&y, x|y, x^y);
+    Console.WriteLine();
+    Console.WriteLine("{0,-6} {1,-6}", "x", "!x");
+    foreach (bool? x in bvals) 
+      Console.WriteLine("{0,-6} {1,-6}", x, !x);
+  }
+}

Added: trunk/mcs/tests/gen-131.cs
===================================================================
--- trunk/mcs/tests/gen-131.cs  2005-03-07 05:55:39 UTC (rev 41500)
+++ trunk/mcs/tests/gen-131.cs  2005-03-07 06:01:26 UTC (rev 41501)
@@ -0,0 +1,45 @@
+//-- ex-nullable
+
+using System;
+
+class MyTest {
+  public static void Main(String[] args) {
+    Console.WriteLine("Note that null prints as blank or []\n");
+    int? i1 = 11, i2 = 22, i3 = null, i4 = i1+i2, i5 = i1+i3;
+    // Values: 11 22 null 33 null
+    Console.WriteLine("[{0}] [{1}] [{2}] [{3}] [{4}]", i1, i2, i3, i4, i5);
+    int i6 = (int)i1;                           // Legal
+    // int i7 = (int)i5;                        // Legal but fails at run-time
+    // int i8 = i1;                             // Illegal
+
+    int?[] iarr = { i1, i2, i3, i4, i5 };
+    i2 += i1;
+    i2 += i4;
+    Console.WriteLine("i2 = {0}", i2);          // 66 = 11+22+33
+
+    int sum = 0;
+    for (int i=0; i<iarr.Length; i++)
+      sum += iarr[i] != null ? iarr[i].Value : 0;
+      //      sum += iarr[i] ?? 0;
+    Console.WriteLine("sum = {0}", sum);        // 66 = 11+22+33
+
+    for (int i=0; i<iarr.Length; i++)
+      if (iarr[i] > 11)
+        Console.Write("[{0}] ", iarr[i]);       // 22 33
+    Console.WriteLine();
+
+    for (int i=0; i<iarr.Length; i++)
+      if (iarr[i] != i1)
+        Console.Write("[{0}] ", iarr[i]);       // 22 null 33 null
+    Console.WriteLine();
+    Console.WriteLine();
+    int?[] ivals = { null, 2, 5 };
+    Console.WriteLine("{0,6} {1,6} {2,6} {3,6} {4,-6} {5,-6} {6,-6} {7,-6}", 
+                      "x", "y", "x+y", "x-y", "x<y", "x>=y", "x==y", "x!=y");
+    Console.WriteLine();
+    foreach (int? x in ivals) 
+      foreach (int? y in ivals) 
+        Console.WriteLine("{0,6} {1,6} {2,6} {3,6} {4,-6} {5,-6} {6,-6} 
{7,-6}", 
+                          x, y, x+y, x-y, x<y, x>=y, x==y, x!=y);
+  }
+}

Added: trunk/mcs/tests/gen-132.cs
===================================================================
--- trunk/mcs/tests/gen-132.cs  2005-03-07 05:55:39 UTC (rev 41500)
+++ trunk/mcs/tests/gen-132.cs  2005-03-07 06:01:26 UTC (rev 41501)
@@ -0,0 +1,17 @@
+//-- ex-nullable-sqrt
+
+using System;
+
+class MyTest {
+  public static int? Sqrt(int? x) {
+    if (x.HasValue && x.Value >= 0)
+      return (int)(Math.Sqrt(x.Value));
+    else
+      return null;
+  }
+
+  public static void Main(String[] args) {
+    // Prints :2:::
+    Console.WriteLine(":{0}:{1}:{2}:", Sqrt(5), Sqrt(null), Sqrt(-5));
+  }
+}

Added: trunk/mcs/tests/gen-133.cs
===================================================================
--- trunk/mcs/tests/gen-133.cs  2005-03-07 05:55:39 UTC (rev 41500)
+++ trunk/mcs/tests/gen-133.cs  2005-03-07 06:01:26 UTC (rev 41501)
@@ -0,0 +1,31 @@
+// Not used -- ex-nullable-struct
+
+// Converting a struct from S to S? creates a copy of the struct.
+// Getting the struct out of the non-null value creates another copy.
+
+using System;
+
+struct S {
+  private int x;
+  public int X {
+    get { return x; }
+    set { this.x = value; }    // Cannot be used on non-variable ns.Value
+  }
+  public void Set(int x) {
+    this.x = x;
+  }
+}
+
+class MyTest {
+  public static void Main(String[] args) {
+    S s = new S();
+    s.Set(11);
+    Console.WriteLine("s.X = {0}", s.X);
+    S? ns = s;
+    Console.WriteLine("s.X = {0} ns.Value.X = {1}", s.X, ns.Value.X);
+    ns.Value.Set(22);
+    Console.WriteLine("s.X = {0} ns.Value.X = {1}", s.X, ns.Value.X);
+    s.Set(33);
+    Console.WriteLine("s.X = {0} ns.Value.X = {1}", s.X, ns.Value.X);
+  }
+}

Added: trunk/mcs/tests/gen-134.cs
===================================================================
--- trunk/mcs/tests/gen-134.cs  2005-03-07 05:55:39 UTC (rev 41500)
+++ trunk/mcs/tests/gen-134.cs  2005-03-07 06:01:26 UTC (rev 41501)
@@ -0,0 +1,41 @@
+// [EMAIL PROTECTED] * 2004-08
+
+using System;
+
+class MyTest {
+  public static void Main(String[] args) {
+    Foo<int?> fni1 = new Foo<int?>(null);
+    Console.WriteLine(fni1.Fmt());
+    Foo<int?> fni2 = new Foo<int?>(17);
+    Console.WriteLine(fni2.Fmt());
+    Foo<int> fi = new Foo<int>(7);
+    Console.WriteLine(fi.Fmt());
+    Foo<String> fs1 = new Foo<String>(null);
+    Console.WriteLine(fs1.Fmt());
+    Foo<String> fs2 = new Foo<String>("haha");
+    Console.WriteLine(fs2.Fmt());
+  }
+}
+
+class Foo<T> {
+  T x;
+  public Foo(T x) { 
+    this.x = x;
+  }
+  
+  // This shows how to deal with tests for null in a generic setting
+  // where null may mean both `null reference' and `null value of a
+  // nullable type'.  Namely, the test (x == null) will always be
+  // false if the generic type parameter t is instantiated with a
+  // nullable type.  Reason: the null literal will be considered a
+  // null reference and x will be boxed if a value type, and hence the
+  // comparison will be false...
+
+  public String Fmt() {
+    if (x is INullableValue && ((INullableValue)x).HasValue
+       || !(x is INullableValue) && x != null)
+      return x.ToString();
+    else
+      return "null";
+  }  
+}

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to