Skip to content

Commit

Permalink
Unit Tests (#97)
Browse files Browse the repository at this point in the history
More Unit Tests for Programs
  • Loading branch information
umeshma committed Sep 13, 2023
1 parent 696f0bf commit 4a438d5
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 33 deletions.
16 changes: 16 additions & 0 deletions tests/APIDefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ public interface IMathAPI
double div(double x, double y);
[Comment("Negate a number")]
double neg(double x);
[Comment("Return pi")]
double pi();
[Comment("power")]
double power(double x, double power);
[Comment("sqrRoot")]
double sqrRoot(double x);
[Comment("Vectorized addition")]
double addVector(double[] vector);
}

public interface IMathAPIAsync
Expand All @@ -29,6 +37,14 @@ public interface IMathAPIAsync
Task<double> div(double x, double y);
[Comment("Negate a number")]
Task<double> neg(double x);
[Comment("Return pi")]
Task<double> pi();
[Comment("power")]
Task<double> power(double x, double power);
[Comment("sqrRoot")]
Task<double> sqrRoot(double x);
[Comment("Vectorized addition")]
Task<double> addVector(double[] vector);
}

[Comment("This is an API for String Operations")]
Expand Down
34 changes: 34 additions & 0 deletions tests/MathAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace Microsoft.TypeChat.Tests;

public class MathAPI : IMathAPI
{
public static double SmallPi = 3.142;

public static MathAPI Default = new MathAPI();

public double add(double x, double y) => x + y;
Expand All @@ -15,6 +17,22 @@ public class MathAPI : IMathAPI
public double div(double x, double y) => x / y;

public double neg(double x) => -x;

public double pi() => MathAPI.SmallPi;

public double power(double x, double power) => Math.Pow(x, power);

public double sqrRoot(double x) => Math.Sqrt(x);

public double addVector(double[] vector)
{
double sum = 0.0;
for (int i = 0; i < vector.Length; ++i)
{
sum += vector[i];
}
return sum;
}
}

public class MathAPIAsync : IMathAPIAsync
Expand Down Expand Up @@ -58,6 +76,22 @@ public Task<double> unknown(string text)
return SlowResult(double.NaN);
}

public Task<double> pi() => SlowResult(MathAPI.SmallPi);

public Task<double> power(double x, double power) => SlowResult(Math.Pow(x, power));

public Task<double> sqrRoot(double x) => SlowResult(Math.Sqrt(x));

public Task<double> addVector(double[] vector)
{
double sum = 0.0;
for (int i = 0; i < vector.Length; ++i)
{
sum += vector[i];
}
return SlowResult(sum);
}

async Task<double> SlowResult(double result)
{
await Task.Delay(500).ConfigureAwait(false);
Expand Down
9 changes: 7 additions & 2 deletions tests/Schemas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ public int CompareTo(Name other)
}
return cmp;
}

public override string ToString()
{
return $"{FirstName} {LastName}";
}
}

public class Location
Expand Down Expand Up @@ -339,7 +344,7 @@ public class Circle : Shape
public double Radius;
}

public class Canvas
public class Drawing
{
public Shape[] Shapes { get; set; }

Expand All @@ -353,7 +358,7 @@ public class Canvas
{
if (curNumber == objNumber)
{
return (T) shape;
return (T)shape;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/TestEndToEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async Task Translate_Polymorphic()
return;
}

var translator = new JsonTranslator<Canvas>(new LanguageModel(_config.OpenAI));
var translator = new JsonTranslator<Drawing>(new LanguageModel(_config.OpenAI));
string request = "Add a circle of radius 4.5 at 30, 30 and\n" +
"Add a retangle at 5, 5 with height 10 and width 15";

Expand Down
12 changes: 6 additions & 6 deletions tests/TestProgramInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public void Test_Math(string source, double expectedResult)
{
Program program = Json.Parse<Program>(source);
ValidateProgram(program);
FunctionCallValidator<IMathAPI> validator = new FunctionCallValidator<IMathAPI>(MathAPI.Default);
validator.ValidateProgram(program);

Api api = new Api(new MathAPI());
double result = program.Run(api);
Expand All @@ -25,13 +27,11 @@ public async Task Test_MathAsync(string source, double expectedResult)
Program program = Json.Parse<Program>(source);
ValidateProgram(program);

Api api = new Api(MathAPIAsync.Default);
ProgramCompiler compiler = new ProgramCompiler(api.TypeInfo);
Delegate d = compiler.Compile(program, api);
double result = (double)d.DynamicInvoke();
Assert.Equal(expectedResult, result);
FunctionCallValidator<IMathAPIAsync> validator = new FunctionCallValidator<IMathAPIAsync>(MathAPIAsync.Default);
validator.ValidateProgram(program);

result = (double)await program.RunAsync(api);
Api api = new Api(MathAPIAsync.Default);
double result = await program.RunAsync(api);
Assert.Equal(expectedResult, result);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/TextApis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public string Concat(dynamic value, StringBuilder sb)
{
foreach (var kv in obj)
{
sb.Append('[').Append(kv.Key).Append(", ");
sb.Append('(').Append(kv.Key).Append(", ");
sb.Append(kv.Value);
sb.Append("]");
sb.Append(")");
}
}
else
Expand Down
57 changes: 36 additions & 21 deletions tests/mathPrograms.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
"@steps": [
{
"@func": "mul",
"@args": [
3,
5
]
"@args": [3, 5]
}
]
}
Expand All @@ -19,17 +16,11 @@
"@steps": [
{
"@func": "mul",
"@args": [
3,
5
]
"@args": [3, 5]
},
{
"@func": "mul",
"@args": [
4,
7
]
"@args": [4, 7]
},
{
"@func": "add",
Expand All @@ -55,10 +46,7 @@
3,
{
"@func": "mul",
"@args": [
4,
5
]
"@args": [4, 5]
}
]
},
Expand All @@ -70,14 +58,41 @@
},
{
"@func": "mul",
"@args": [
5,
6
]
"@args": [5, 6]
}
]
}
]
}
},
"program3": {
"result": 50,
"source": {
"@steps": [
{
"@func": "addVector",
"@args": [[5, 10, 15, 20]]
}
]
}
},
"program4": {
"result": 78.55,
"source": {
"@steps": [
{
"@func": "mul",
"@args": [
{
"@func": "pi"
},
{
"@func": "power",
"@args": [ 5, 2 ]
}
]
}
]
}
}
}
}
33 changes: 33 additions & 0 deletions tests/mathPrograms_Fail.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,39 @@
}
]
}
},
"program6": {
"result": 50,
"source": {
"@steps": [
{
"@func": "addVector",
"@args": [
5,
10,
15,
20
]
}
]
}
},
"program7": {
"result": 0,
"source": {
"@steps": [
{
"@func": "add",
"@args": [
3,
4,
{
"@func": "pi_x"
}
]
}
]
}
}

}
37 changes: 36 additions & 1 deletion tests/stringPrograms.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"program1": {
"result": "998877[One, 66][Two, 55]",
"result": "998877(One, 66)(Two, 55)",
"source": {
"@steps": [
{
Expand Down Expand Up @@ -91,5 +91,40 @@
}
]
}
},
"program5": {
"result": "35hellogoodbye(city, athens)(country, greece)",
"source": {
"@steps": [
{
"@func": "concat",
"@args": [
[
3,
5,
"Hello",
"Goodbye",
{
"City": "Athens",
"Country": "Greece"
}
]
]
},
{
"@func": "uppercase",
"@args": [
{ "@ref": 0 }
]
},
{
"@func": "lowercase",
"@args": [
{ "@ref": 1 }
]
}
]
}
}

}

0 comments on commit 4a438d5

Please sign in to comment.