| | | 1 | | using NUnit.Framework; |
| | | 2 | | using System.Collections; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using UnityEngine; |
| | | 5 | | using UnityEngine.TestTools; |
| | | 6 | | using System; |
| | | 7 | | |
| | | 8 | | public class LaunchAngleCsvInterpolatorTests { |
| | | 9 | | [Test] |
| | 1 | 10 | | public void TestDataPoint() { |
| | 1 | 11 | | LaunchAngleInput input = new LaunchAngleInput(distance: 9074.84f, altitude: 97.7306f); |
| | 1 | 12 | | LaunchAngleOutput expectedOutput = |
| | | 13 | | new LaunchAngleOutput(launchAngle: 22f, timeToPosition: 21.32f); |
| | | 14 | | |
| | 1 | 15 | | LaunchAngleCsvInterpolator interpolator = new LaunchAngleCsvInterpolator(); |
| | 1 | 16 | | Assert.AreEqual(expectedOutput, interpolator.Plan(input)); |
| | 1 | 17 | | } |
| | | 18 | | |
| | | 19 | | [Test] |
| | 1 | 20 | | public void TestNearestNeighbor() { |
| | 1 | 21 | | LaunchAngleInput input = new LaunchAngleInput(distance: 9076f, altitude: 94f); |
| | 1 | 22 | | LaunchAngleOutput expectedOutput = |
| | | 23 | | new LaunchAngleOutput(launchAngle: 22f, timeToPosition: 21.32f); |
| | | 24 | | |
| | 1 | 25 | | LaunchAngleCsvInterpolator interpolator = new LaunchAngleCsvInterpolator(); |
| | 1 | 26 | | Assert.AreEqual(expectedOutput, interpolator.Plan(input)); |
| | 1 | 27 | | } |
| | | 28 | | |
| | | 29 | | [Test] |
| | 1 | 30 | | public void TestFileNotFound() { |
| | | 31 | | // Inject a mock ConfigLoader that returns an empty string. |
| | 2 | 32 | | LaunchAngleCsvInterpolator.ConfigLoaderDelegate mockLoader = (string path) => ""; |
| | | 33 | | |
| | 1 | 34 | | LogAssert.Expect(LogType.Error, "Failed to load CSV file from Planning/nonexistent.csv."); |
| | 2 | 35 | | Assert.Throws<InvalidOperationException>(() => { |
| | 1 | 36 | | var interpolator = new LaunchAngleCsvInterpolator("Planning/nonexistent.csv", mockLoader); |
| | 1 | 37 | | interpolator.Plan(new LaunchAngleInput()); |
| | 0 | 38 | | }); |
| | 1 | 39 | | } |
| | | 40 | | |
| | | 41 | | [Test] |
| | 1 | 42 | | public void TestInvalidInterpolationData() { |
| | | 43 | | // Create a mock CSV with invalid data format. |
| | 1 | 44 | | string mockCsv = "invalid,csv,data\n1,2,3"; |
| | 2 | 45 | | LaunchAngleCsvInterpolator.ConfigLoaderDelegate mockLoader = (string path) => mockCsv; |
| | | 46 | | |
| | 1 | 47 | | var interpolator = new LaunchAngleCsvInterpolator(null, mockLoader); |
| | 1 | 48 | | var input = new LaunchAngleInput(distance: 1, altitude: 2); |
| | | 49 | | |
| | 3 | 50 | | var ex = Assert.Throws<InvalidOperationException>(() => { interpolator.Plan(input); }); |
| | 1 | 51 | | Assert.That(ex.Message, Is.EqualTo("Interpolator returned invalid data.")); |
| | 1 | 52 | | } |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | public class LaunchAngleDataInterpolatorTests { |
| | | 56 | | private class DummyLaunchAngleDataInterpolator : LaunchAngleDataInterpolator { |
| | | 57 | | public DummyLaunchAngleDataInterpolator() : base() {} |
| | | 58 | | |
| | | 59 | | // Generate the list of launch angle data points to interpolate. |
| | | 60 | | protected override List<LaunchAngleDataPoint> GenerateData() { |
| | | 61 | | return new List<LaunchAngleDataPoint> { |
| | | 62 | | new LaunchAngleDataPoint(new LaunchAngleInput(distance: 1, altitude: 100), |
| | | 63 | | new LaunchAngleOutput(launchAngle: 90, timeToPosition: 10)), |
| | | 64 | | new LaunchAngleDataPoint(new LaunchAngleInput(distance: 100, altitude: 1), |
| | | 65 | | new LaunchAngleOutput(launchAngle: 10, timeToPosition: 20)), |
| | | 66 | | }; |
| | | 67 | | } |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | [Test] |
| | | 71 | | public void TestDataPoint() { |
| | | 72 | | LaunchAngleInput input = new LaunchAngleInput(distance: 1, altitude: 100); |
| | | 73 | | LaunchAngleOutput expectedOutput = new LaunchAngleOutput(launchAngle: 90, timeToPosition: 10); |
| | | 74 | | |
| | | 75 | | DummyLaunchAngleDataInterpolator interpolator = new DummyLaunchAngleDataInterpolator(); |
| | | 76 | | Assert.AreEqual(expectedOutput, interpolator.Plan(input)); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | [Test] |
| | | 80 | | public void TestNearestNeighbor() { |
| | | 81 | | LaunchAngleInput input = new LaunchAngleInput(distance: 1, altitude: 200); |
| | | 82 | | LaunchAngleOutput expectedOutput = new LaunchAngleOutput(launchAngle: 90, timeToPosition: 10); |
| | | 83 | | |
| | | 84 | | DummyLaunchAngleDataInterpolator interpolator = new DummyLaunchAngleDataInterpolator(); |
| | | 85 | | Assert.AreEqual(expectedOutput, interpolator.Plan(input)); |
| | | 86 | | } |
| | | 87 | | } |