|
| 1 | +using NumericUpDownLib; |
| 2 | +using NUnit.Framework; |
| 3 | +using System; |
| 4 | +using System.Threading; |
| 5 | + |
| 6 | +namespace NUnitTestProject |
| 7 | +{ |
| 8 | + [Apartment(ApartmentState.STA)] |
| 9 | + public class DecimalTests |
| 10 | + { |
| 11 | + [SetUp] |
| 12 | + public void Setup() |
| 13 | + { |
| 14 | + } |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Tests whether values are set correctly as required while maintaining |
| 18 | + /// valid values at all times. |
| 19 | + /// </summary> |
| 20 | + [Test] |
| 21 | + public void TestValues() |
| 22 | + { |
| 23 | + TestAllPermutations(25, 50, 75); |
| 24 | + TestAllPermutations(-25, 50, 75); |
| 25 | + TestAllPermutations(-50, -50, 75); |
| 26 | + TestAllPermutations(50, 50, 50); |
| 27 | + TestAllPermutations(-50, -50, -50); |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Tests all permutions for all sequences of three elements. |
| 32 | + /// </summary> |
| 33 | + /// <param name="min"></param> |
| 34 | + /// <param name="val"></param> |
| 35 | + /// <param name="max"></param> |
| 36 | + public void TestAllPermutations(decimal min, decimal val, decimal max) |
| 37 | + { |
| 38 | + string[,] ctrl = new string[6, 3] |
| 39 | + { |
| 40 | + { "min", "val", "max" }, |
| 41 | + { "val", "min", "max" }, |
| 42 | + { "val", "max", "min" }, |
| 43 | + { "val", "min", "max" }, |
| 44 | + { "min", "max", "val" }, |
| 45 | + { "max", "min", "val" }, |
| 46 | + }; |
| 47 | + |
| 48 | + for (int i = 0; i < 6; i++) |
| 49 | + { |
| 50 | + var range = new DecimalUpDown(); |
| 51 | + string testPermutation = ""; |
| 52 | + |
| 53 | + for (int j = 0; j < 3; j++) |
| 54 | + { |
| 55 | + var itemToSet = ctrl[i, j]; |
| 56 | + |
| 57 | + if (string.IsNullOrEmpty(testPermutation)) |
| 58 | + testPermutation = itemToSet; |
| 59 | + else |
| 60 | + testPermutation += ", " + itemToSet; |
| 61 | + |
| 62 | + switch (itemToSet) |
| 63 | + { |
| 64 | + case "min": |
| 65 | + range.MinValue = min; |
| 66 | + Assert.IsTrue(IsValidRange(range)); |
| 67 | + break; |
| 68 | + |
| 69 | + case "val": |
| 70 | + range.Value = val; |
| 71 | + Assert.IsTrue(IsValidRange(range)); |
| 72 | + break; |
| 73 | + |
| 74 | + case "max": |
| 75 | + range.MaxValue = max; |
| 76 | + Assert.IsTrue(IsValidRange(range)); |
| 77 | + break; |
| 78 | + |
| 79 | + default: |
| 80 | + break; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + Console.WriteLine("Testing Permutation {0}: {1} - min={2}, val={3}, max={4}", i, testPermutation, min, val, max); |
| 85 | + Assert.IsTrue(IsValidRange(range)); |
| 86 | + |
| 87 | + Assert.IsTrue(range.MinValue == min); |
| 88 | + Assert.IsTrue(range.Value == val); |
| 89 | + Assert.IsTrue(range.MaxValue == max); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Determines whether the given set of values defines a valid range or not. |
| 95 | + /// </summary> |
| 96 | + /// <param name="range"></param> |
| 97 | + /// <returns></returns> |
| 98 | + bool IsValidRange(DecimalUpDown range) |
| 99 | + { |
| 100 | + if (range.MinValue <= range.Value && range.Value <= range.MaxValue) |
| 101 | + return true; |
| 102 | + |
| 103 | + return false; |
| 104 | + |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments