1
0
mirror of https://github.com/twiglet/cs2j.git synced 2025-01-18 13:15:17 +01:00

Add Test Projects

This commit is contained in:
Kevin Glynn 2011-06-10 14:05:32 +02:00
parent d855f757ae
commit 2d24b572eb
14 changed files with 732 additions and 0 deletions

View File

@ -0,0 +1,27 @@
using System.Reflection;
using System.Runtime.CompilerServices;
// Information about this assembly is defined by the following attributes.
// Change them to the values specific to your project.
[assembly: AssemblyTitle("Tester")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
[assembly: AssemblyVersion("1.0.*")]
// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.
//[assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyFile("")]

View File

@ -0,0 +1,189 @@
using System;
using System.Collections.Generic;
namespace Tester.DelegateUser
{
public delegate void CompleteCallback (String name, bool result);
public delegate bool Predicate<T> (T value);
public delegate void Del<T> (T value);
[Serializable]
public class DelegateTest<U>
{
public delegate void NodeSysLogHandler(string fname);
public static event NodeSysLogHandler OnNodeSysLogHandler; // Event handler is fired on change event
public void EventLogger() {
OnNodeSysLogHandler += fname => Console.WriteLine("Log:" + fname);
if (OnNodeSysLogHandler != null)
{
OnNodeSysLogHandler("log");
return;
}
}
private Predicate<String> predDel = value => value == "Hello";
//private event CompleteCallback cbackMember2 = null;
private event CompleteCallback cbackMember = null;
public CompleteCallback cbackProperty { get; set; }
public Del<String> printProp { get; set; }
delegate void TestDelegate (string s);
delegate void TestRefDelegate (string s, ref int cumul);
static void M (string s)
{
Console.WriteLine (s);
}
public void Notify (string i)
{
Console.Out.WriteLine ("Notify: {0}", i);
}
public void UserResponse (String nameParam, bool resultParam)
{
Console.Out.WriteLine ("{0} was {1}", nameParam, resultParam);
}
public void Init (string[] args)
{
cbackMember = UserResponse;
cbackProperty = new CompleteCallback (UserResponse);
}
public void SwingIt ()
{
CompleteCallback cback = new CompleteCallback (UserResponse);
CompleteCallback cback1 = null;
cback1 = UserResponse;
CompleteCallback cback2 = new CompleteCallback (this.cbackProperty);
cback ("kevin", true);
cback1 ("lizzie", true);
cback2 ("jessie", false);
Del<string> d1 = new Del<string> (Notify);
d1 ("456");
cbackMember ("fred", false);
this.cbackProperty ("thomas", true);
// Original delegate syntax required
// initialization with a named method.
TestDelegate testDelA = new TestDelegate (M);
// C# 2.0: A delegate can be initialized with
// inline code, called an "anonymous method." This
// method takes a string as an input parameter.
TestDelegate testDelB = delegate(string s) { Console.WriteLine (s); };
// C# 3.0. A delegate can be initialized with
// a lambda expression. The lambda also takes a string
// as an input parameter (x).
TestDelegate testDelC = (string x) => { Console.WriteLine (x); };
// C# 3.0. A delegate can be initialized with
// a lambda expression. The lambda also takes a string
// as an input parameter (x). The type of x is inferred by the compiler.
TestDelegate testDelD = x => { Console.WriteLine (x); };
// Invoke the delegates.
testDelA ("Hello. My name is M and I write lines.");
testDelB ("That's nothing. I'm anonymous and ");
testDelC ("I'm a famous author.");
TestRefDelegate testDela = delegate (string x, ref int cumul) { cumul += 1; Console.WriteLine (x); Console.WriteLine ("cumul is {0}", cumul); };
TestRefDelegate testDelb = (string x, ref int cumul) => { cumul += 3; Console.WriteLine (x); Console.WriteLine ("cumul is {0}", cumul); };
TestRefDelegate testDelc = (string x, ref int cumul) => { cumul += 2; Console.WriteLine (x); Console.WriteLine ("cumul is {0}", cumul); };
TestRefDelegate testDeld = testDela + testDelb + testDelc;
int mycumulator = 0;
testDeld("RefAccumulator", ref mycumulator);
}
delegate void DelC (string s);
static void Hello (string s)
{
System.Console.WriteLine (" Hello, {0}!", s);
}
static void Goodbye (string s)
{
System.Console.WriteLine (" Goodbye, {0}!", s);
}
void HelloGoodbye ()
{
DelC a, b, c, d;
// Create the delegate object a that references
// the method Hello:
a = Hello;
// Create the delegate object b that references
// the method Goodbye:
b = Goodbye;
// The two delegates, a and b, are composed to form c:
c = a + b;
// Remove a from the composed delegate, leaving d,
// which calls only the method Goodbye:
d = c - a;
System.Console.WriteLine ("Invoking delegate a:");
a ("A");
System.Console.WriteLine ("Invoking delegate b:");
b ("B");
System.Console.WriteLine ("Invoking delegate c:");
c ("C");
System.Console.WriteLine ("Invoking delegate d:");
d ("D");
d += c;
d += c;
d -= c;
System.Console.WriteLine ("Invoking composed delegate d:");
d ("DA");
Del<String> myt = x => Console.WriteLine("And so goodbye from " + x);
this.printProp = x => Console.WriteLine("Hello from " + x);
this.printProp += myt;
printProp += x => Console.WriteLine("And goodbye from " + x);
printProp("kevin");
this.printProp -= myt;
printProp("des");
}
public static void DelMain (string[] args)
{
DelegateTest<string> myDel = new DelegateTest<string> ();
myDel.Init (args);
myDel.SwingIt ();
myDel.HelloGoodbye ();
myDel.EventLogger();
}
}
}

View File

@ -0,0 +1,50 @@
using System;
namespace Tester
{
// ordinary outer class
public class FullOuterPartialInner
{
public FullOuterPartialInner ()
{
}
public partial class PartialMiddle {
// First part of inner
public partial class PartialInner {
// interface
public partial interface PartialIF {
int metha(int fred);
}
// member a
public int a = 0;
}
// middlea member
public int middlea = 0;
}
// a outer member
public int outera = 0;
public partial class PartialMiddle {
// middle b member
public int middleb = 0;
// Second part of inner
public partial class PartialInner {
// member b
public int b = 0;
public partial interface PartialIF {
int methb(char fred);
}
}
}
}
}

View File

@ -0,0 +1,130 @@
using System;
namespace Tester.Generics
{
class Factory6<U> where U : new() {
public static U GetNew() { return new U(); }
}
class Program6 {
static void Main6(){
int i = Factory6<int>.GetNew();
object obj = Factory6<object>.GetNew();
// Here, 'i' is equal to 0 and 'obj' references
// an instance of the class 'object'.
}
}
// Derivation constraint
// If you wish to use certain members of the instances of a parameter type in a generic, you must apply a derivation constraint. Here is an example which illustrates the syntax:
//
//
// Example 7
interface ICustomInterface7 { int Fct(); }
class C7<U> where U : ICustomInterface7 {
public int AnotherFct(U u) { return u.Fct(); }
}
// You can apply several interface implementation constraints and one base class inheritance constraint on a same type parameter. In this case, the base class must appear in the list of types. You can also use this constraint conjointly with the default constructor constraint. In this case, the default constructor constraint must appear last:
//
//
// Example 8
interface ICustomInterface18 { int Fct1(); }
interface ICustomInterface28 { string Fct2(); }
class BaseClass8{}
class C8<U>
where U : BaseClass8, ICustomInterface18, ICustomInterface28, new() {
public string Fct(U u) { return u.Fct2(); }
}
// You cannot use a sealed class or a one of the System.Object, System.Array, System.Delegate, System.Enum or System.ValueType class as the base class of a type parameter.
//
//
// You also cannot use the static members of T like this:
//
//
// Example 9
class BaseClass9 { public static void Fct(){} }
class C9<T> where T : BaseClass9 {
void F(){
/* commented out to build
// Compilation Error: 'T' is a 'type parameter',
// which is not valid in the given context.
T.Fct();
*/
// Here is the right syntax to call Fct().
BaseClass9.Fct();
}
}
// A type used in a derivation constraint can be an open or closed generic type. Let's illustrate this using the System.IComparable<T> interface. Remember that the types which implement this interface can see their instances compared to an instance of type T.
//
//
// Example 10
class C1A<U> where U : IComparable<int> {
public int Compare( U u, int i ) { return u.CompareTo( i ); }
}
class C2A<U> where U : IComparable<U> {
public int Compare( U u1, U u2 ) { return u1.CompareTo( u2 ); }
}
class C3A<U,V> where U : IComparable<V> {
public int Compare( U u, V v ) { return u.CompareTo( v ); }
}
class C4A<U,V> where U : IComparable<V>, IComparable<int> {
public int Compare( U u, int i ) { return u.CompareTo( i ); }
}
// Note that a type used in a derivation constraint must have a visibility greater or equal to the one of the generic type which contains this parameter type. For example:
//
//
// Example 11
/* commented out to build
internal class BaseClassB{}
// Compilation Error: Inconsistent accessibility:
// constraint type 'BaseClass' is less accessible than 'C<T>'
public class CB<T> where T : BaseClassB{}
*/
// To be used in a generic type, certain functionalities can force you to impose certain derivation constraints. For example, if you wish to use a T type parameter in a catch clause, you must constrain T to derive from System.Exception or of one of its derived classes. Also, if you wish to use the using keyword to automatically dispose of an instance of the type parameter, it must be constraint to use the System.IDisposable interface. Finally, if you wish to use the foreach keyword to enumerate the elements of an instance of the parameter type, it must be constraint to implement the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable<T> interface.
//
//
// Take note that in the special case where T is constrained to implement an interface and T is a value type, the call to a member of the interface on an instance of T will not cause a boxing operation. The following example puts this into evidence:
//
//
// Example 12
//
interface ICounterC{
void Increment();
int Val{get;}
}
struct CounterC : ICounterC {
private int i;
public void Increment() { i++; }
public int Val { get { return i; } }
}
class CC<T> where T : ICounterC, new() {
public void Fct(){
T t = new T();
System.Console.WriteLine( t.Val.ToString() );
t.Increment(); // Modify the state of 't'.
System.Console.WriteLine( t.Val.ToString() );
// Modify the state of a boxed copy of 't'.
(t as ICounterC).Increment();
System.Console.WriteLine( t.Val.ToString() );
}
}
class ProgramC {
static void CMain() {
/* commented out to build
CC<Counter> c = new CC<CounterC>();
c.Fct();
*/
}
}
}

View File

@ -0,0 +1,18 @@
using System;
namespace Tester
{
public class MainClass
{
public static void Main (string[] args)
{
//Tester.DelegateUser.DelegateTest<int>.DelMain(args);
//Tester.RefOut.RefOutTest.RefOutMain(args);
Tester.PartialUser.PartialMain(args);
}
}
}

View File

@ -0,0 +1,23 @@
using System;
namespace Tester.Misc
{
public class Misc
{
public Misc ()
{
}
private int TestProperty
{
get;set;
}
public void Init (string[] args)
{
// Test incrementing a property
TestProperty = 0;
TestProperty++;
}
}
}

View File

@ -0,0 +1,16 @@
using System;
namespace Tester
{
public partial class PartialUser {
public partial class PartialInner
{
private int a = 0;
public PartialInner ()
{
}
}
}
}

View File

@ -0,0 +1,41 @@
using System;
namespace Tester
{
// ordinary outer class
public partial class PartialOuterPartialInner
{
public PartialOuterPartialInner ()
{
}
public partial class PartialMiddle {
// First part of inner
public partial class PartialInner {
// member a
public int a = 0;
}
// middlea member
public int middlea = 0;
}
// a outer member
public int outera = 0;
public partial class PartialMiddle {
// middle b member
public int middleb = 0;
// Second part of inner
public partial class PartialInner {
// member b
public int b = 0;
}
}
}
}

View File

@ -0,0 +1,38 @@
using System;
namespace Tester
{
// ordinary outer class
public partial class PartialOuterPartialInner
{
public partial class PartialMiddle {
// First part of inner
public partial class PartialInner {
// member ab
public int ab = 0;
}
// middlea member
public int middleab = 0;
}
// b outer member
public int outerb = 0;
public partial class PartialMiddle {
// middle b member
public int middlebb = 0;
// Second part of inner
public partial class PartialInner {
// member b
public int bb = 0;
}
}
}
}

View File

@ -0,0 +1,30 @@
#define INFILE
using System;
namespace Tester
{
public partial class PartialUser
{
public PartialUser ()
{
}
partial void doPart(String[] args)
{
Console.WriteLine("Hello from doPart()" + msg);
}
partial void doNothingPart(String[] args)
;
public partial class PartInner {
}
#if MONO && (LINUX || INFILE)
public String ToString() {
return "PartialUser";
}
#endif
}
}

View File

@ -0,0 +1,30 @@
#define INFILE
using System;
namespace Tester
{
public partial class PartialUser
{
string msg = "hello";
public bool doIt(String[] args) {
doNothingPart(args);
doPart(args);
return true;
}
partial void doPart(String[] args)
;
public static void PartialMain(String[] args) {
PartialUser part = new PartialUser();
part.doIt(args);
}
}
}

View File

@ -0,0 +1,59 @@
using System;
namespace Tester.RefOut
{
public class RefOutTest
{
public RefOutTest ()
{
}
public void testref (ref int xarg)
{
int x = xarg;
x++;
xarg = 23;
throw new Exception();
}
void FillArray(out int[] arr)
{
// Initialize the array:
arr = new int[5] { 1, 2, 3, 4, 5 };
}
public void Init (string[] args)
{
int localx = 5;
try {
if(true)
testref (ref localx);
} catch (Exception) {
//
}
finally {
Console.Out.WriteLine ("The x is {0}", localx);
}
int[] theArray; // Initialization is not required
// Pass the array to the callee using out:
FillArray(out theArray);
// Display the array elements:
System.Console.WriteLine("Array elements are:");
for (int i = 0; i < theArray.Length; i++)
{
System.Console.Write(theArray[i] + " ");
}
}
public static void RefOutMain (string[] args)
{
new RefOutTest ().Init (args);
}
}
}

View File

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{DFD1D658-C3A4-4AE6-8E34-195F09575AE5}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>Tester</RootNamespace>
<AssemblyName>Tester</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileUpgradeFlags>
</FileUpgradeFlags>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>3.5</OldToolsVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<Externalconsole>true</Externalconsole>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<DebugType>none</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<Externalconsole>true</Externalconsole>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
<Compile Include="DelegateUser.cs" />
<Compile Include="Generics.cs" />
<Compile Include="Misc.cs" />
<Compile Include="RefOut.cs" />
<Compile Include="Main.cs" />
<Compile Include="PartialUser.cs" />
<Compile Include="PartialUserTwo.cs" />
<Compile Include="PartialInner.cs" />
<Compile Include="FullOuterPartialInner.cs" />
<Compile Include="ophelie.cs" />
<Compile Include="PartialOuterPartialInner.cs" />
<Compile Include="PartialOuterPartialInnerB.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -0,0 +1,23 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual C# Express 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tester", "Tester.csproj", "{DFD1D658-C3A4-4AE6-8E34-195F09575AE5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DFD1D658-C3A4-4AE6-8E34-195F09575AE5}.Debug|x86.ActiveCfg = Debug|x86
{DFD1D658-C3A4-4AE6-8E34-195F09575AE5}.Debug|x86.Build.0 = Debug|x86
{DFD1D658-C3A4-4AE6-8E34-195F09575AE5}.Release|x86.ActiveCfg = Release|x86
{DFD1D658-C3A4-4AE6-8E34-195F09575AE5}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = Tester.csproj
EndGlobalSection
EndGlobal