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]
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 ()
{
Assert.IsFalse (g1 != g2, "A2");
Assert.IsTrue (g1 != g3, "A3");
}
+#pragma warning restore 1718
[Test]
public void EqualsObject ()
Assert.IsFalse (g1.Equals ((object)"This is not a Guid!"), "A5");
}
-#if NET_2_0
+
[Test]
public void EqualsGuid ()
{
Assert.IsFalse (g1.Equals (null), "A4");
Assert.IsFalse (g1.Equals ("This is not a Guid!"), "A5");
}
-#endif
[Test]
public void CompareToObject ()
Guid.Empty.CompareTo ("Say what?");
}
-#if NET_2_0
[Test]
public void CompareToGuid ()
{
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 ()
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))]
{
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
}
}