|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using System.Linq; |
| 5 | + |
| 6 | +using numl.AI.Collections; |
| 7 | + |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +using MATH = System.Math; |
| 11 | +using System.Diagnostics; |
| 12 | + |
| 13 | +namespace numl.Tests.CollectionTests |
| 14 | +{ |
| 15 | + [Trait("CollectionTests", "CollectionTests")] |
| 16 | + public class CollectionsTests |
| 17 | + { |
| 18 | + [Fact] |
| 19 | + public void NSortedList_Sort_Test() |
| 20 | + { |
| 21 | + for (int run = 0; run < 2; run++) |
| 22 | + { |
| 23 | + bool reverse = (run != 0); |
| 24 | + |
| 25 | + var sorted = new NSortedList<int>(-1, reverse); |
| 26 | + |
| 27 | + int length = Math.Probability.Sampling.GetUniform(10, 1024); |
| 28 | + |
| 29 | + for (int i = 0; i < length; i++) |
| 30 | + { |
| 31 | + int val = Math.Probability.Sampling.GetUniform(0, length - 1); |
| 32 | + sorted.Add(val); |
| 33 | + } |
| 34 | + |
| 35 | + int remove = (int) MATH.Ceiling(length / 2.0); |
| 36 | + for (int i = 0; i < remove; i++) |
| 37 | + { |
| 38 | + int index = Math.Probability.Sampling.GetUniform(0, sorted.Count - 1); |
| 39 | + sorted.Remove(index); |
| 40 | + } |
| 41 | + |
| 42 | + bool result = false; |
| 43 | + for (int i = 1; i < (length - remove); i++) |
| 44 | + { |
| 45 | + if (reverse) |
| 46 | + { |
| 47 | + result = sorted[i - 1] >= sorted[i]; |
| 48 | + Assert.True(result, $"Item at {i - 1} was larger than expected"); |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + result = sorted[i - 1] <= sorted[i]; |
| 53 | + Assert.True(result, $"Item at {i - 1} was smaller than expected"); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public void NSortedList_Limit_Reverse_Test() |
| 61 | + { |
| 62 | + for (int run = 0; run < 2; run++) |
| 63 | + { |
| 64 | + int length = Math.Probability.Sampling.GetUniform(10, 1024); |
| 65 | + bool reverse = (run != 0); |
| 66 | + |
| 67 | + var sorted = new NSortedList<int>(length, reverse); |
| 68 | + |
| 69 | + for (int i = 0; i < length; i++) |
| 70 | + { |
| 71 | + int val = Math.Probability.Sampling.GetUniform(0, length - 1); |
| 72 | + sorted.Add(val); |
| 73 | + } |
| 74 | + |
| 75 | + length = (length / 2); |
| 76 | + |
| 77 | + sorted.Limit = length; |
| 78 | + |
| 79 | + Assert.True(sorted.Count == length, "Length exceeds specified limit"); |
| 80 | + |
| 81 | + for (int i = 0; i < length / 2; i++) |
| 82 | + { |
| 83 | + int val = Math.Probability.Sampling.GetUniform(0, length - 1); |
| 84 | + sorted.Add(val); |
| 85 | + } |
| 86 | + |
| 87 | + Assert.True(sorted.Count == length, "Length exceeds specified limit after additions"); |
| 88 | + |
| 89 | + bool result = false; |
| 90 | + for (int i = 1; i < sorted.Count; i++) |
| 91 | + { |
| 92 | + if (reverse) |
| 93 | + { |
| 94 | + result = sorted[i - 1] >= sorted[i]; |
| 95 | + Assert.True(result, $"Item at {i - 1} was larger than expected"); |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + result = sorted[i - 1] <= sorted[i]; |
| 100 | + Assert.True(result, $"Item at {i - 1} was smaller than expected"); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + [Fact] |
| 107 | + public void NSortedList_Size_Test() |
| 108 | + { |
| 109 | + var sorted = new NSortedList<int>(); |
| 110 | + |
| 111 | + int length = Math.Probability.Sampling.GetUniform(10, 1024); |
| 112 | + |
| 113 | + var dict = new Dictionary<int, int>(); |
| 114 | + |
| 115 | + for (int i = 0; i < length; i++) |
| 116 | + { |
| 117 | + int val = Math.Probability.Sampling.GetUniform(0, length - 1); |
| 118 | + sorted.Add(val); |
| 119 | + |
| 120 | + dict[val] = (dict.ContainsKey(val) ? dict[val] + 1 : 1); |
| 121 | + } |
| 122 | + |
| 123 | + int remove = (int) MATH.Ceiling(length / 2.0); |
| 124 | + for (int i = 0; i < remove; i++) |
| 125 | + { |
| 126 | + int val = Math.Probability.Sampling.GetUniform(0, sorted.Count - 1); |
| 127 | + |
| 128 | + if (dict.ContainsKey(val)) |
| 129 | + { |
| 130 | + sorted.Remove(val); |
| 131 | + |
| 132 | + dict[val] = 0; |
| 133 | + |
| 134 | + Assert.False(sorted.Contains(val)); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + var contains = (dict.Where(w => w.Value == 0 && sorted.Contains(w.Key))); |
| 139 | + |
| 140 | + bool result = (contains.Count() == 0); |
| 141 | + |
| 142 | + Assert.True(result, "Length was not equal to the elements in the collection"); |
| 143 | + |
| 144 | + bool lresult = (sorted.Select(s => s).Count() == sorted.Count); |
| 145 | + |
| 146 | + Assert.True(lresult, "Enumerator extends beyond number of accessible items in collection"); |
| 147 | + } |
| 148 | + |
| 149 | + [Fact] |
| 150 | + public void NSortedList_Speed_Test() |
| 151 | + { |
| 152 | + int epochs = 10000; |
| 153 | + |
| 154 | + Stopwatch timer = new Stopwatch(); |
| 155 | + |
| 156 | + timer.Start(); |
| 157 | + |
| 158 | + var nsorted = new NSortedList<Foo>(); |
| 159 | + |
| 160 | + for (int i = 0; i < epochs; i++) |
| 161 | + { |
| 162 | + var foo = new Foo(); |
| 163 | + nsorted.Add(foo); |
| 164 | + } |
| 165 | + |
| 166 | + timer.Stop(); |
| 167 | + |
| 168 | + long ts1 = timer.ElapsedMilliseconds; |
| 169 | + |
| 170 | + timer.Stop(); |
| 171 | + Foo._Id = 0; |
| 172 | + |
| 173 | + timer.Start(); |
| 174 | + |
| 175 | + var sorted = new SortedList<int, Foo>(); |
| 176 | + |
| 177 | + for (int i = 0; i < epochs; i++) |
| 178 | + { |
| 179 | + var foo = new Foo(); |
| 180 | + sorted.Add(foo.Weight, foo); |
| 181 | + } |
| 182 | + |
| 183 | + timer.Stop(); |
| 184 | + |
| 185 | + long ts2 = timer.ElapsedMilliseconds; |
| 186 | + |
| 187 | + Assert.True(ts1 < ts2); |
| 188 | + } |
| 189 | + |
| 190 | + public class Foo : IComparable |
| 191 | + { |
| 192 | + internal static int _Id = 0; |
| 193 | + |
| 194 | + public int Weight { get; set; } |
| 195 | + |
| 196 | + public Foo() |
| 197 | + { |
| 198 | + this.Weight = (++_Id); |
| 199 | + } |
| 200 | + |
| 201 | + public int CompareTo(object obj) |
| 202 | + { |
| 203 | + var other = (Foo) obj; |
| 204 | + |
| 205 | + return this.Weight.CompareTo(other.Weight); |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments