From 8afbbbbf3b059a52360d490ac672bb3aed7ba67c Mon Sep 17 00:00:00 2001 From: Marius Merschformann Date: Fri, 11 Oct 2024 01:21:55 +0200 Subject: [PATCH] Adding SC.Tests project with simple golden file testing --- SC.Tests/Golden.cs | 49 ++++ SC.Tests/SC.Tests.csproj | 48 ++++ SC.Tests/data/sample_contained.json | 230 +++++++++++++++++++ SC.Tests/data/sample_contained.json.golden | 247 +++++++++++++++++++++ SC.Tests/data/sample_offload.json | 224 +++++++++++++++++++ SC.Tests/data/sample_offload.json.golden | 180 +++++++++++++++ SardineCan.sln | 14 ++ 7 files changed, 992 insertions(+) create mode 100644 SC.Tests/Golden.cs create mode 100644 SC.Tests/SC.Tests.csproj create mode 100644 SC.Tests/data/sample_contained.json create mode 100644 SC.Tests/data/sample_contained.json.golden create mode 100644 SC.Tests/data/sample_offload.json create mode 100644 SC.Tests/data/sample_offload.json.golden diff --git a/SC.Tests/Golden.cs b/SC.Tests/Golden.cs new file mode 100644 index 0000000..ba2ce36 --- /dev/null +++ b/SC.Tests/Golden.cs @@ -0,0 +1,49 @@ +using JsonDiffPatchDotNet; +using Newtonsoft.Json.Linq; +using FluentAssertions; +using FluentAssertions.Json; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json; +using SC.CLI; +using SC.ObjectModel; +using SC.ObjectModel.Configuration; +using SC.ObjectModel.Additionals; +using SC.ObjectModel.IO; + + +namespace SC.Tests; + +public class Golden +{ + private static JToken LoadJson(string path) => JToken.Parse(File.ReadAllText(path)); + + private static void WriteJson(JToken json, string path) => + File.WriteAllText(path, json.ToString(formatting: Formatting.Indented)); + + + [Theory()] + [InlineData("data/sample_contained.json")] + [InlineData("data/sample_offload.json")] + public void GoldenInputs(string inputFile) + { + // Load the input file + var instance = Instance.ReadJson(File.ReadAllText(inputFile)); + + // Run calculation + var result = Executor.Execute(instance, new Configuration() { TimeLimitInSeconds = 1 }, Console.WriteLine); + + var update = Environment.GetEnvironmentVariable("UPDATE") == "1"; + if (update) + { + // Just update the golden file + WriteJson(JToken.Parse(JsonIO.To(result.Solution.ToJsonSolution())), inputFile + ".golden"); + } + else + { + // Compare the actual result with the golden file + var expectation = LoadJson(inputFile + ".golden"); + var actual = JToken.Parse(JsonIO.To(result.Solution.ToJsonSolution())); + actual.Should().BeEquivalentTo(expectation); + } + } +} diff --git a/SC.Tests/SC.Tests.csproj b/SC.Tests/SC.Tests.csproj new file mode 100644 index 0000000..9b9f75b --- /dev/null +++ b/SC.Tests/SC.Tests.csproj @@ -0,0 +1,48 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + + + + + PreserveNewest + + + + PreserveNewest + + + + PreserveNewest + + + + PreserveNewest + + + + diff --git a/SC.Tests/data/sample_contained.json b/SC.Tests/data/sample_contained.json new file mode 100644 index 0000000..fb4d97f --- /dev/null +++ b/SC.Tests/data/sample_contained.json @@ -0,0 +1,230 @@ +{ + "name": "jsonTest", + "containers": [ + { + "id": 0, + "length": 600, + "width": 400, + "height": 300 + }, + { + "id": 1, + "length": 600, + "width": 400, + "height": 300 + }, + { + "id": 2, + "length": 500, + "width": 400, + "height": 300 + }, + { + "id": 3, + "length": 300, + "width": 400, + "height": 250 + } + ], + "pieces": [ + { + "id": 0, + "flags": [ + { + "flagId": 0, + "flagValue": 1 + } + ], + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 304, + "width": 336, + "height": 140 + } + ] + }, + { + "id": 1, + "flags": [ + { + "flagId": 0, + "flagValue": 0 + } + ], + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 122, + "width": 245, + "height": 367 + } + ] + }, + { + "id": 2, + "flags": [ + { + "flagId": 0, + "flagValue": 0 + } + ], + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 392, + "width": 145, + "height": 152 + } + ] + }, + { + "id": 3, + "flags": [ + { + "flagId": 0, + "flagValue": 0 + } + ], + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 271, + "width": 214, + "height": 393 + } + ] + }, + { + "id": 4, + "flags": [ + { + "flagId": 0, + "flagValue": 0 + } + ], + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 351, + "width": 398, + "height": 287 + } + ] + }, + { + "id": 5, + "flags": [ + { + "flagId": 0, + "flagValue": 0 + } + ], + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 351, + "width": 245, + "height": 287 + } + ] + }, + { + "id": 6, + "flags": [ + { + "flagId": 0, + "flagValue": 1 + } + ], + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 294, + "width": 234, + "height": 376 + } + ] + }, + { + "id": 7, + "flags": [ + { + "flagId": 0, + "flagValue": 0 + } + ], + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 241, + "width": 78, + "height": 115 + } + ] + }, + { + "id": 8, + "flags": [ + { + "flagId": 0, + "flagValue": 1 + } + ], + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 154, + "width": 395, + "height": 274 + } + ] + }, + { + "id": 9, + "flags": [ + { + "flagId": 0, + "flagValue": 1 + } + ], + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 60, + "width": 183, + "height": 170 + } + ] + } + ], + "rules": { + "flagRules": [ + { + "flagId": 0, + "ruleType": 0, + "parameter": 0 + } + ] + } +} \ No newline at end of file diff --git a/SC.Tests/data/sample_contained.json.golden b/SC.Tests/data/sample_contained.json.golden new file mode 100644 index 0000000..064226a --- /dev/null +++ b/SC.Tests/data/sample_contained.json.golden @@ -0,0 +1,247 @@ +{ + "containers": [ + { + "id": 0, + "length": 600, + "width": 400, + "height": 300, + "assignments": [ + { + "piece": 0, + "position": { + "x": 0, + "y": 0, + "z": 0, + "a": 0, + "b": 0, + "c": 180 + }, + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 304, + "width": 336, + "height": 140 + } + ] + }, + { + "piece": 9, + "position": { + "x": 304, + "y": 0, + "z": 0, + "a": 270, + "b": 0, + "c": 0 + }, + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 60, + "width": 170, + "height": 183 + } + ] + }, + { + "piece": 6, + "position": { + "x": 364, + "y": 0, + "z": 0, + "a": 0, + "b": 90, + "c": 90 + }, + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 234, + "width": 376, + "height": 294 + } + ] + }, + { + "piece": 8, + "position": { + "x": 0, + "y": 0, + "z": 140, + "a": 0, + "b": 270, + "c": 0 + }, + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 274, + "width": 395, + "height": 154 + } + ] + } + ] + }, + { + "id": 1, + "length": 600, + "width": 400, + "height": 300, + "assignments": [ + { + "piece": 5, + "position": { + "x": 0, + "y": 0, + "z": 0, + "a": 0, + "b": 0, + "c": 180 + }, + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 351, + "width": 245, + "height": 287 + } + ] + }, + { + "piece": 1, + "position": { + "x": 0, + "y": 245, + "z": 0, + "a": 90, + "b": 0, + "c": 90 + }, + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 367, + "width": 122, + "height": 245 + } + ] + }, + { + "piece": 7, + "position": { + "x": 351, + "y": 0, + "z": 0, + "a": 0, + "b": 90, + "c": 270 + }, + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 78, + "width": 115, + "height": 241 + } + ] + }, + { + "piece": 2, + "position": { + "x": 429, + "y": 0, + "z": 0, + "a": 0, + "b": 0, + "c": 270 + }, + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 145, + "width": 392, + "height": 152 + } + ] + } + ] + }, + { + "id": 2, + "length": 500, + "width": 400, + "height": 300, + "assignments": [ + { + "piece": 4, + "position": { + "x": 0, + "y": 0, + "z": 0, + "a": 180, + "b": 0, + "c": 270 + }, + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 398, + "width": 351, + "height": 287 + } + ] + } + ] + }, + { + "id": 3, + "length": 300, + "width": 400, + "height": 250, + "assignments": [ + { + "piece": 3, + "position": { + "x": 0, + "y": 0, + "z": 0, + "a": 90, + "b": 0, + "c": 180 + }, + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 271, + "width": 393, + "height": 214 + } + ] + } + ] + } + ], + "offload": [] +} \ No newline at end of file diff --git a/SC.Tests/data/sample_offload.json b/SC.Tests/data/sample_offload.json new file mode 100644 index 0000000..ad5df21 --- /dev/null +++ b/SC.Tests/data/sample_offload.json @@ -0,0 +1,224 @@ +{ + "name": "jsonTestOffload", + "containers": [ + { + "id": 0, + "length": 600, + "width": 400, + "height": 300 + }, + { + "id": 1, + "length": 600, + "width": 400, + "height": 300 + }, + { + "id": 2, + "length": 300, + "width": 400, + "height": 250 + } + ], + "pieces": [ + { + "id": 0, + "flags": [ + { + "flagId": 0, + "flagValue": 1 + } + ], + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 304, + "width": 336, + "height": 140 + } + ] + }, + { + "id": 1, + "flags": [ + { + "flagId": 0, + "flagValue": 0 + } + ], + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 122, + "width": 245, + "height": 367 + } + ] + }, + { + "id": 2, + "flags": [ + { + "flagId": 0, + "flagValue": 0 + } + ], + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 392, + "width": 145, + "height": 152 + } + ] + }, + { + "id": 3, + "flags": [ + { + "flagId": 0, + "flagValue": 0 + } + ], + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 271, + "width": 214, + "height": 393 + } + ] + }, + { + "id": 4, + "flags": [ + { + "flagId": 0, + "flagValue": 0 + } + ], + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 351, + "width": 398, + "height": 287 + } + ] + }, + { + "id": 5, + "flags": [ + { + "flagId": 0, + "flagValue": 0 + } + ], + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 351, + "width": 245, + "height": 287 + } + ] + }, + { + "id": 6, + "flags": [ + { + "flagId": 0, + "flagValue": 1 + } + ], + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 294, + "width": 234, + "height": 376 + } + ] + }, + { + "id": 7, + "flags": [ + { + "flagId": 0, + "flagValue": 0 + } + ], + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 241, + "width": 78, + "height": 115 + } + ] + }, + { + "id": 8, + "flags": [ + { + "flagId": 0, + "flagValue": 1 + } + ], + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 154, + "width": 395, + "height": 274 + } + ] + }, + { + "id": 9, + "flags": [ + { + "flagId": 0, + "flagValue": 1 + } + ], + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 60, + "width": 183, + "height": 170 + } + ] + } + ], + "rules": { + "flagRules": [ + { + "flagId": 0, + "ruleType": 0, + "parameter": 0 + } + ] + } +} \ No newline at end of file diff --git a/SC.Tests/data/sample_offload.json.golden b/SC.Tests/data/sample_offload.json.golden new file mode 100644 index 0000000..09bc5b0 --- /dev/null +++ b/SC.Tests/data/sample_offload.json.golden @@ -0,0 +1,180 @@ +{ + "containers": [ + { + "id": 0, + "length": 600, + "width": 400, + "height": 300, + "assignments": [ + { + "piece": 6, + "position": { + "x": 0, + "y": 0, + "z": 0, + "a": 0, + "b": 90, + "c": 270 + }, + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 234, + "width": 376, + "height": 294 + } + ] + }, + { + "piece": 0, + "position": { + "x": 234, + "y": 0, + "z": 0, + "a": 0, + "b": 0, + "c": 0 + }, + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 304, + "width": 336, + "height": 140 + } + ] + }, + { + "piece": 8, + "position": { + "x": 234, + "y": 0, + "z": 140, + "a": 0, + "b": 90, + "c": 0 + }, + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 274, + "width": 395, + "height": 154 + } + ] + }, + { + "piece": 9, + "position": { + "x": 538, + "y": 0, + "z": 0, + "a": 0, + "b": 0, + "c": 180 + }, + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 60, + "width": 183, + "height": 170 + } + ] + } + ] + }, + { + "id": 1, + "length": 600, + "width": 400, + "height": 300, + "assignments": [ + { + "piece": 3, + "position": { + "x": 0, + "y": 0, + "z": 0, + "a": 0, + "b": 90, + "c": 270 + }, + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 214, + "width": 393, + "height": 271 + } + ] + }, + { + "piece": 4, + "position": { + "x": 214, + "y": 0, + "z": 0, + "a": 180, + "b": 0, + "c": 0 + }, + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 351, + "width": 398, + "height": 287 + } + ] + } + ] + }, + { + "id": 2, + "length": 300, + "width": 400, + "height": 250, + "assignments": [ + { + "piece": 5, + "position": { + "x": 0, + "y": 0, + "z": 0, + "a": 90, + "b": 0, + "c": 270 + }, + "cubes": [ + { + "x": 0, + "y": 0, + "z": 0, + "length": 287, + "width": 351, + "height": 245 + } + ] + } + ] + } + ], + "offload": [ + 1, + 2, + 7 + ] +} \ No newline at end of file diff --git a/SardineCan.sln b/SardineCan.sln index e311fb2..237d374 100644 --- a/SardineCan.sln +++ b/SardineCan.sln @@ -21,6 +21,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SC.Service", "SC.Service\SC EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SC.CLI", "SC.CLI\SC.CLI.csproj", "{049AFB9E-E6CB-47BE-93DE-C0B67CFED040}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SC.Tests", "SC.Tests\SC.Tests.csproj", "{B77B65F0-9413-4452-B507-5BCF7872588C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -139,6 +141,18 @@ Global {049AFB9E-E6CB-47BE-93DE-C0B67CFED040}.Release|x64.Build.0 = Release|x64 {049AFB9E-E6CB-47BE-93DE-C0B67CFED040}.Release|x86.ActiveCfg = Release|x86 {049AFB9E-E6CB-47BE-93DE-C0B67CFED040}.Release|x86.Build.0 = Release|x86 + {B77B65F0-9413-4452-B507-5BCF7872588C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B77B65F0-9413-4452-B507-5BCF7872588C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B77B65F0-9413-4452-B507-5BCF7872588C}.Debug|x64.ActiveCfg = Debug|Any CPU + {B77B65F0-9413-4452-B507-5BCF7872588C}.Debug|x64.Build.0 = Debug|Any CPU + {B77B65F0-9413-4452-B507-5BCF7872588C}.Debug|x86.ActiveCfg = Debug|Any CPU + {B77B65F0-9413-4452-B507-5BCF7872588C}.Debug|x86.Build.0 = Debug|Any CPU + {B77B65F0-9413-4452-B507-5BCF7872588C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B77B65F0-9413-4452-B507-5BCF7872588C}.Release|Any CPU.Build.0 = Release|Any CPU + {B77B65F0-9413-4452-B507-5BCF7872588C}.Release|x64.ActiveCfg = Release|Any CPU + {B77B65F0-9413-4452-B507-5BCF7872588C}.Release|x64.Build.0 = Release|Any CPU + {B77B65F0-9413-4452-B507-5BCF7872588C}.Release|x86.ActiveCfg = Release|Any CPU + {B77B65F0-9413-4452-B507-5BCF7872588C}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE