From 3ece28810afd74d33af07225519c71433f4713f1 Mon Sep 17 00:00:00 2001 From: "John Chen (CLR)" Date: Tue, 17 May 2016 23:58:14 -0700 Subject: [PATCH] Initial Ready-to-Run tests for generics * Added some generics tests intended for testing Ready-to-Run feature. * Added capability to set ZapRequire=1, to ensure methods are pre-compiled. * Fix spelling of environment variables for case-sensitive platforms. --- tests/src/CLRTest.CrossGen.targets | 10 +- tests/src/dir.props | 5 + tests/src/readytorun/generics.cs | 214 +++++++++++++++++++++++++++ tests/src/readytorun/generics.csproj | 40 +++++ 4 files changed, 263 insertions(+), 6 deletions(-) create mode 100644 tests/src/readytorun/generics.cs create mode 100644 tests/src/readytorun/generics.csproj diff --git a/tests/src/CLRTest.CrossGen.targets b/tests/src/CLRTest.CrossGen.targets index c71df333ae..9d0f28c69d 100644 --- a/tests/src/CLRTest.CrossGen.targets +++ b/tests/src/CLRTest.CrossGen.targets @@ -38,9 +38,8 @@ WARNING: When setting properties based on their current state (for example: true + + + 2 + + diff --git a/tests/src/readytorun/generics.cs b/tests/src/readytorun/generics.cs new file mode 100644 index 0000000000..069235c64b --- /dev/null +++ b/tests/src/readytorun/generics.cs @@ -0,0 +1,214 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. +// + +using System; +using System.Collections.Generic; +using System.Text; + +class Program +{ + static int Main() + { + // Run all tests 3x times to exercise both slow and fast paths work + for (int i = 0; i < 3; i++) + RunAllTests(); + + Console.WriteLine(Assert.HasAssertFired ? "FAILED" : "PASSED"); + return Assert.HasAssertFired ? 1 : 100; + } + + static void RunAllTests() + { + DateTime dt = new DateTime(1776, 7, 4); + string dtString = dt.ToString(); + Assert.AreEqual(new GenClass1c(dt).ToStringEx(7), dtString + " 7"); + Assert.AreEqual(new GenClass1c(1).ToStringEx(7), "1 7"); + Assert.AreEqual(new GenClass1c(2).ToStringEx(7), "2 7"); + Assert.AreEqual(new GenClass1c(3.14f).ToStringEx(7), "3.14 7"); + Assert.AreEqual(new GenClass1c(4.13).ToStringEx(7), "4.13 7"); + Assert.AreEqual(new GenClass1c(9).ToString(), "9"); + + Assert.AreEqual(new GenClass2(dt, 3.1416).ToString(), dtString + " 3.1416"); + Assert.AreEqual(new GenClass2(dt, 3.1416).ToStringEx(7, 8), dtString + " 3.1416 7 8"); + Assert.AreEqual(new GenClass2(new object(), "3.1416").ToString(), "System.Object 3.1416"); + Assert.AreEqual(new GenClass2(new object(), "3.1416").ToStringEx(7L, 8L), "System.Object 3.1416 7 8"); + Assert.AreEqual(GetString(7.0, 8.0), "7 8"); + + var gen1a = new GenClass1a(); + Assert.AreEqual(gen1a.CreateGenClass1b(), "GenClass1b`1[System.Object]"); + Assert.AreEqual(gen1a.CreateGenClass1bArray(), "GenClass1b`1[System.Object][]"); + + var gen1aInt = new GenClass1a(); + var gen1bInt = new GenClass1b(); + var gen1bLong = new GenClass1b(); + Assert.AreEqual(gen1bInt.IsGenClass1a(gen1aInt).ToString(), "True"); + Assert.AreEqual(gen1bLong.IsGenClass1a(gen1aInt).ToString(), "False"); + Assert.AreEqual(gen1bInt.AsGenClass1a(gen1aInt)?.ToString() ?? "null", gen1aInt.ToString()); + Assert.AreEqual(gen1bLong.AsGenClass1a(gen1aInt)?.ToString() ?? "null", "null"); + + var gen1aString = new GenClass1a(); + var gen1b = new GenClass1b(); + Assert.AreEqual(gen1b.IsGenClass1a(gen1aString).ToString(), "True"); + Assert.AreEqual(gen1b.AsGenClass1a(gen1aString)?.ToString() ?? "null", gen1aString.ToString()); +#if false // not yet supported + Assert.AreEqual(GenClass1a.CallVirtual(gen1b), "GenClass1b`1[System.String].VirtualMethod"); + Assert.AreEqual(GenClass1a.CallInterface(gen1b), "GenClass1b`1[System.String].InterfaceMethod1"); + Assert.AreEqual(GenClass1a.CallInterface(gen1b, "Test").ToString(), "GenClass1b`1[System.String]"); +#endif + + NormalClass n = new NormalClass(); + Assert.AreEqual(CallGenVirtMethod(n).ToString(), "GenClass1a`1[System.Int32]"); + Assert.AreEqual(CallGenVirtMethod(n, 42).ToString(), "System.Int32[]"); +#if false // not yet supported + Assert.AreEqual(CallGenVirtMethod(n).ToString(), "GenClass1a`1[System.String]"); + Assert.AreEqual(CallGenVirtMethod(n, "forty-two").ToString(), "System.String[]"); +#endif + } + + static string GetString(X x, Y y) + { + return string.Join(" ", x, y); + } + + static GenClass1a CallGenVirtMethod(NormalClass n) + { + return n.GetGenClass1a(); + } + + static IEnumerable CallGenVirtMethod(NormalClass n, object o) + { + return n.GetEnumerable(o); + } +} + +interface IGenInterface +{ + string InterfaceMethod1(); + IGenInterface InterfaceMethod2(U u); +} + +class GenClass1a +{ + public string CreateGenClass1b() + { + var x = new GenClass1b(); + return x.ToString(); + } + public string CreateGenClass1bArray() + { + var x = new GenClass1b[3]; + return x.ToString(); + } + public static string CallVirtual(GenClass1b x) + { + return x.VirtualMethod(); + } + public static string CallInterface(IGenInterface x) + { + return x.InterfaceMethod1(); + } + public static IGenInterface CallInterface(IGenInterface x, V v) + { + return x.InterfaceMethod2(v); + } +} + +class GenClass1b : IGenInterface +{ + public virtual string VirtualMethod() + { + return ToString() + ".VirtualMethod"; + } + public virtual string InterfaceMethod1() + { + return ToString() + ".InterfaceMethod1"; + } + public virtual IGenInterface InterfaceMethod2(U u) + { + return this; + } + public bool IsGenClass1a(object o) + { + return o is GenClass1a; + } + public GenClass1a AsGenClass1a(object o) + { + return o as GenClass1a; + } +} + +class GenClass1c where T : new() +{ + public T t; + public GenClass1c() + { + t = new T(); + } + public GenClass1c(T _t) + { + t = _t; + } + public void SetT(object x) + { + t = (T)x; + } + public override string ToString() + { + return t.ToString(); + } + public string ToStringEx(X x) + { + return string.Join(" ", t, x); + } +} + +class GenClass2 +{ + public GenClass2(T t, U u) + { + this.t = t; + this.u = u; + } + public override string ToString() + { + return t.ToString() + " " + u.ToString(); + } + public string ToStringEx(X x, Y y) + { + return string.Join(" ", t, u, x, y); + } + T t; + U u; +} + +class NormalClass +{ + public virtual GenClass1a GetGenClass1a() + { + return new GenClass1a(); + } + public virtual IEnumerable GetEnumerable(object o) + { + T[] array = new T[1]; + array[0] = (T)o; + return array; + } +} + +public static class Assert +{ + public static bool HasAssertFired; + + public static void AreEqual(Object actual, Object expected) + { + if (!(actual == null && expected == null) && !actual.Equals(expected)) + { + Console.WriteLine("Not equal!"); + Console.WriteLine("actual = " + actual.ToString()); + Console.WriteLine("expected = " + expected.ToString()); + HasAssertFired = true; + } + } +} diff --git a/tests/src/readytorun/generics.csproj b/tests/src/readytorun/generics.csproj new file mode 100644 index 0000000000..18a21f9f33 --- /dev/null +++ b/tests/src/readytorun/generics.csproj @@ -0,0 +1,40 @@ + + + + + generics + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 7a9bfb7d + true + false + Exe + BuildAndRun + 0 + 1 + + + + + False + + + + + + + + + + + + + + + + + -- 2.34.1