Test DateTimeOffset.Parse with format yyyy-dd-MMzzz. Covers #22558.
[mono.git] / mcs / class / corlib / Test / System / GuidTest.cs
index bba0de94d42b785dec275d75d3191d6767a0acb3..463d54a9b5e65b398b76a83dba112c9a830d7f7c 100644 (file)
@@ -53,13 +53,16 @@ namespace MonoTests.System {
                        Guid g2 = new Guid ("{00010203-0405-0607-0809-0A0B0C0D0E0F}"); 
                        Guid g3 = new Guid ("{0x00010203,0x0405,0x0607,{0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}}");
                        Guid g4 = new Guid ("(00010203-0405-0607-0809-0A0B0C0D0E0F)");
-
+                       Guid g5 = new Guid ("\n  \r  \n 00010203-0405-0607-0809-0a0b0c0d0e0f \r\n");
+                       Guid g6 = new Guid ("\t {\t 0x00000001 , 0x0002 , \t 0x0003 \t , { 0x04 , 0x05 , \t 0x06\t,\t0x07 , 0x08 , 0x09 , 0x0a , 0x0b } \t }\t ");
                        string expected = "00010203-0405-0607-0809-0a0b0c0d0e0f";
                        Assert.AreEqual (expected, g0.ToString (), "A0");
                        Assert.AreEqual (expected, g1.ToString (), "A1");
                        Assert.AreEqual (expected, g2.ToString (), "A2");
                        Assert.AreEqual (expected, g3.ToString (), "A3");
                        Assert.AreEqual (expected, g4.ToString (), "A4");
+                       Assert.AreEqual (expected, g5.ToString (), "A5");
+                       Assert.AreEqual ("00000001-0002-0003-0405-060708090a0b", g6.ToString (), "A6");
                }
 
                [Test]
@@ -139,6 +142,16 @@ namespace MonoTests.System {
                        Assert.IsFalse (g1 == g2);
                }
 
+               [Test]
+               public void NewGuid_Stressed ()
+               {
+                       for (int i = 0; i < 256; i++) {
+                               Guid g = Guid.NewGuid ();
+                               Assert.AreNotEqual (0, g.CompareTo (Guid.Empty), i.ToString ());
+                       }
+               }
+
+#pragma warning disable 1718
                [Test]
                public void EqualityOp ()
                {
@@ -162,6 +175,7 @@ namespace MonoTests.System {
                        Assert.IsFalse (g1 != g2, "A2");
                        Assert.IsTrue (g1 != g3, "A3");
                }
+#pragma warning restore 1718
 
                [Test]
                public void EqualsObject ()
@@ -177,7 +191,7 @@ namespace MonoTests.System {
                        Assert.IsFalse (g1.Equals ((object)"This is not a Guid!"), "A5");
                }
 
-#if NET_2_0
+
                [Test]
                public void EqualsGuid ()
                {
@@ -191,7 +205,6 @@ namespace MonoTests.System {
                        Assert.IsFalse (g1.Equals (null), "A4");
                        Assert.IsFalse (g1.Equals ("This is not a Guid!"), "A5");
                }
-#endif
 
                [Test]
                public void CompareToObject ()
@@ -218,7 +231,6 @@ namespace MonoTests.System {
                        Guid.Empty.CompareTo ("Say what?");
                }
 
-#if NET_2_0
                [Test]
                public void CompareToGuid ()
                {
@@ -235,7 +247,18 @@ namespace MonoTests.System {
                        Assert.IsTrue (g4.CompareTo (g1) > 0, "A6");
                        Assert.IsTrue (g1.CompareTo (g1) == 0, "A7");
                }
-#endif
+
+               [Test]
+               public void CompareToGuid_2 ()
+               {
+                       var g1 = new Guid ("d1c5088bc188464fb77b0fd2be2d005e");
+                       var g2 = new Guid ("d2c5088bc188464fb77b0fd2be2d005e");
+                       var g3 = new Guid ("00c5088bc188464fb77b0fd2be2d005e");
+
+                       Assert.AreEqual (-1, g1.CompareTo (g2), "#1");
+                       Assert.AreEqual (1, g1.CompareTo (g3), "#2");
+                       Assert.AreEqual (1, g1.CompareTo (Guid.Empty), "#3");
+               }
 
                [Test]
                public void GetHashCode_Same ()
@@ -271,14 +294,20 @@ namespace MonoTests.System {
                        Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g.ToString (""), "A6");
                        Assert.AreEqual ("00010203-0405-0607-0809-0a0b0c0d0e0f", g.ToString ((string)null), "A7");
                        Assert.AreEqual ("{00010203-0405-0607-0809-0a0b0c0d0e0f}", g.ToString ("B", null), "A10");
+#if NET_4_0
+                       Assert.AreEqual ("{0x00010203,0x0405,0x0607,{0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}}", g.ToString ("x"), "A11");
+                       Assert.AreEqual ("{0x00010203,0x0405,0x0607,{0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}}", g.ToString ("X"), "A11");
+#endif
                }
 
+#if !NET_4_0
                [Test]
                [ExpectedException (typeof (FormatException))]
                public void ToString_UnsupportedFormat ()
                {
                        new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f).ToString ("X");
                }
+#endif
 
                [Test]
                [ExpectedException (typeof (FormatException))]
@@ -286,5 +315,136 @@ namespace MonoTests.System {
                {
                        new Guid (0x00010203, 0x0405, 0x0607, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f).ToString ("This is invalid");
                }
+
+               [Test]
+               [ExpectedException (typeof (FormatException))]
+               public void ParseExtraJunkN ()
+               {
+                       new Guid ("000102030405060708090a0b0c0d0e0faaaaaa");
+               }
+
+               [Test]
+               [ExpectedException (typeof (FormatException))]
+               public void ParseExtraJunkD ()
+               {
+                       new Guid ("00010203-0405-0607-0809-0a0b0c0d0e0faaaaaa");
+               }
+
+               [Test]
+               [ExpectedException (typeof (FormatException))]
+               public void ParseExtraJunkB ()
+               {
+                       new Guid ("{00010203-0405-0607-0809-0A0B0C0D0E0F}aaaa");
+               }
+
+               [Test]
+               [ExpectedException (typeof (FormatException))]
+               public void ParseExtraJunkP ()
+               {
+                       new Guid ("(00010203-0405-0607-0809-0A0B0C0D0E0F)aaaa");
+               }
+
+               [Test]
+               [ExpectedException (typeof (FormatException))]
+               public void ParseExtraJunkX ()
+               {
+                       new Guid ("{0x00010203,0x0405,0x0607,{0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}}aaaa");
+               }
+
+#if NET_4_0
+
+               /*
+                       N = new Guid ("000102030405060708090a0b0c0d0e0f"); 
+                       D = new Guid ("00010203-0405-0607-0809-0a0b0c0d0e0f"); 
+                       B = new Guid ("{00010203-0405-0607-0809-0A0B0C0D0E0F}"); 
+                       P = new Guid ("(00010203-0405-0607-0809-0A0B0C0D0E0F)");
+                       X = new Guid ("{0x00010203,0x0405,0x0607,{0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}}");
+
+                       string expected = "00010203-0405-0607-0809-0a0b0c0d0e0f";
+               */
+
+               [Test]
+               public void ParseExact ()
+               {
+                       const string expected = "00010203-0405-0607-0809-0a0b0c0d0e0f";
+
+                       var guid = Guid.ParseExact ("000102030405060708090a0b0c0d0e0f", "N");
+                       Assert.AreEqual (expected, guid.ToString ());
+
+                       guid = Guid.ParseExact ("00010203-0405-0607-0809-0a0b0c0d0e0f", "D");
+                       Assert.AreEqual (expected, guid.ToString ());
+
+                       guid = Guid.ParseExact ("00010203-0405-0607-0809-0a0b0c0d0e0f", "d");
+                       Assert.AreEqual (expected, guid.ToString ());
+
+                       guid = Guid.ParseExact ("{00010203-0405-0607-0809-0A0B0C0D0E0F}", "B");
+                       Assert.AreEqual (expected, guid.ToString ());
+
+                       guid = Guid.ParseExact ("{00010203-0405-0607-0809-0A0B0C0D0E0F}", "b");
+                       Assert.AreEqual (expected, guid.ToString ());
+
+                       guid = Guid.ParseExact ("(00010203-0405-0607-0809-0A0B0C0D0E0F)", "P");
+                       Assert.AreEqual (expected, guid.ToString ());
+
+                       guid = Guid.ParseExact ("{0x00010203,0x0405,0x0607,{0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}}", "X");
+                       Assert.AreEqual (expected, guid.ToString ());
+
+                       guid = Guid.ParseExact ("\t {\t 0x00000001 , 0x0002 , \t 0x0003 \t , { 0x04 , 0x05 , \t 0x06\t,\t0x07 , 0x08 , 0x09 , 0x0a , 0x0b } \t }\t ", "X");
+                       Assert.AreEqual ("00000001-0002-0003-0405-060708090a0b", guid.ToString (), "#8");
+               }
+
+               [Test]
+               public void Parse ()
+               {
+                       var guid = Guid.Parse ("\t {\t 0x00000001 , 0x0002 , \t 0x0003 \t , { 0x04 , 0x05 , \t 0x06\t,\t0x07 , 0x08 , 0x09 , 0x0a , 0x0b } \t }\t ");
+                       Assert.AreEqual ("00000001-0002-0003-0405-060708090a0b", guid.ToString (), "#1");
+               }
+
+               [Test]
+               [ExpectedException (typeof (FormatException))]
+               public void ParseError_1 ()
+               {
+                       Guid.Parse("08888888-0444-444-0444-012121212121");
+               }
+
+               [Test]
+               [ExpectedException (typeof (FormatException))]
+               public void ParseExactN ()
+               {
+                       Guid.ParseExact ("00010203-0405-0607-0809-0a0b0c0d0e0f", "N");
+               }
+
+               [Test]
+               [ExpectedException (typeof (FormatException))]
+               public void ParseExactD ()
+               {
+                       Guid.ParseExact ("{0x00010203,0x0405,0x0607,{0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}}", "D");
+               }
+
+               [Test]
+               public void TryParse()
+               {
+                       Guid guid;
+                       Assert.IsFalse (Guid.TryParse(null, out guid), "A1");
+                       Assert.AreEqual (Guid.Empty, guid, "A2");
+                       Assert.IsFalse (Guid.TryParse("", out guid), "A3");
+                       Assert.AreEqual (Guid.Empty, guid, "A4");
+                       Assert.IsFalse (Guid.TryParse("foobar", out guid), "A5");
+                       Assert.AreEqual (Guid.Empty, guid, "A6");
+                       Assert.IsFalse (Guid.TryParse ("08888888-0444-444-0444-012121212121", out guid), "A7");
+               }
+
+               [Test]
+               public void TryParseExact()
+               {
+                       Guid guid;
+                       Assert.IsFalse (Guid.TryParseExact(null, null, out guid), "A1");
+                       Assert.AreEqual (Guid.Empty, guid, "A2");
+                       Assert.IsFalse (Guid.TryParseExact("", null, out guid), "A3");
+                       Assert.AreEqual (Guid.Empty, guid, "A4");
+                       Assert.IsFalse (Guid.TryParseExact("foobar", null, out guid), "A5");
+                       Assert.AreEqual (Guid.Empty, guid, "A6");
+               }
+#endif
        }
 }