|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | +using Infidex.Indexing.Compression; |
| 5 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 6 | + |
| 7 | +namespace Infidex.Tests; |
| 8 | + |
| 9 | +[TestClass] |
| 10 | +public class CompactArrayTests |
| 11 | +{ |
| 12 | + [TestMethod] |
| 13 | + public void TestBasicEncodingDecoding() |
| 14 | + { |
| 15 | + long[] values = [5, 2, 9, 100, 0, 5, 10, 90, 9, 1, 65, 10]; |
| 16 | + |
| 17 | + CompactArray arr = CompactArray.Create(values); |
| 18 | + |
| 19 | + Assert.AreEqual(values.Length, arr.Count); |
| 20 | + Assert.AreEqual(7, arr.Width); |
| 21 | + |
| 22 | + for (int i = 0; i < values.Length; i++) |
| 23 | + { |
| 24 | + Assert.AreEqual((ulong)values[i], arr.Get(i)); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + [TestMethod] |
| 29 | + public void TestEmpty() |
| 30 | + { |
| 31 | + long[] values = []; |
| 32 | + CompactArray arr = CompactArray.Create(values); |
| 33 | + Assert.AreEqual(0, arr.Count); |
| 34 | + Assert.AreEqual(1, arr.Width); |
| 35 | + } |
| 36 | + |
| 37 | + [TestMethod] |
| 38 | + public void TestZeroes() |
| 39 | + { |
| 40 | + long[] values = [0, 0, 0, 0]; |
| 41 | + CompactArray arr = CompactArray.Create(values); |
| 42 | + Assert.AreEqual(4, arr.Count); |
| 43 | + Assert.AreEqual(1, arr.Width); |
| 44 | + |
| 45 | + for (int i = 0; i < values.Length; i++) |
| 46 | + { |
| 47 | + Assert.AreEqual(0UL, arr.Get(i)); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + [TestMethod] |
| 52 | + public void TestLargeValues() |
| 53 | + { |
| 54 | + long[] values = [unchecked((long)ulong.MaxValue), 0, unchecked((long)(ulong.MaxValue >> 1)), 1234567890123456789]; |
| 55 | + |
| 56 | + CompactArray arr = CompactArray.Create(values); |
| 57 | + |
| 58 | + Assert.AreEqual(64, arr.Width); |
| 59 | + |
| 60 | + for (int i = 0; i < values.Length; i++) |
| 61 | + { |
| 62 | + Assert.AreEqual((ulong)values[i], arr.Get(i)); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + [TestMethod] |
| 67 | + public void TestBoundaryCrossing() |
| 68 | + { |
| 69 | + long[] values = [1L << 32, (1L << 32) | 1, 12345]; |
| 70 | + |
| 71 | + CompactArray arr = CompactArray.Create(values); |
| 72 | + Assert.IsTrue(arr.Width >= 33); |
| 73 | + |
| 74 | + for (int i = 0; i < values.Length; i++) |
| 75 | + { |
| 76 | + Assert.AreEqual((ulong)values[i], arr.Get(i)); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + [TestMethod] |
| 81 | + public void TestSerialization() |
| 82 | + { |
| 83 | + long[] values = [5, 2, 9, 100, 0, 5, 10, 90, 9, 1, 65, 10]; |
| 84 | + CompactArray original = CompactArray.Create(values); |
| 85 | + |
| 86 | + using MemoryStream ms = new MemoryStream(); |
| 87 | + using BinaryWriter writer = new BinaryWriter(ms); |
| 88 | + original.Write(writer); |
| 89 | + |
| 90 | + ms.Position = 0; |
| 91 | + using BinaryReader reader = new BinaryReader(ms); |
| 92 | + CompactArray loaded = CompactArray.Read(reader); |
| 93 | + |
| 94 | + Assert.AreEqual(original.Count, loaded.Count); |
| 95 | + Assert.AreEqual(original.Width, loaded.Width); |
| 96 | + |
| 97 | + for (int i = 0; i < values.Length; i++) |
| 98 | + { |
| 99 | + Assert.AreEqual(original.Get(i), loaded.Get(i)); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + [TestMethod] |
| 104 | + public void TestOptimizedSerializationIntegrity() |
| 105 | + { |
| 106 | + long[] values = new long[1000]; |
| 107 | + for(int i=0; i<1000; i++) values[i] = (long)i * 123456789; |
| 108 | + CompactArray original = CompactArray.Create(values); |
| 109 | + |
| 110 | + using (MemoryStream ms = new MemoryStream()) |
| 111 | + using (BinaryWriter writer = new BinaryWriter(ms)) |
| 112 | + { |
| 113 | + writer.Write(original.Width); |
| 114 | + writer.Write(original.Count); |
| 115 | + writer.Write(original.Data.Length); |
| 116 | + for(int i=0; i<original.Data.Length; i++) writer.Write(original.Data[i]); |
| 117 | + |
| 118 | + ms.Position = 0; |
| 119 | + using (BinaryReader reader = new BinaryReader(ms)) |
| 120 | + { |
| 121 | + CompactArray loaded = CompactArray.Read(reader); |
| 122 | + |
| 123 | + Assert.AreEqual(original.Count, loaded.Count); |
| 124 | + for(int i=0; i<values.Length; i++) Assert.AreEqual(values[i], (long)loaded.Get(i)); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + using (MemoryStream ms = new MemoryStream()) |
| 129 | + using (BinaryWriter writer = new BinaryWriter(ms)) |
| 130 | + { |
| 131 | + original.Write(writer); |
| 132 | + |
| 133 | + ms.Position = 0; |
| 134 | + using (BinaryReader reader = new BinaryReader(ms)) |
| 135 | + { |
| 136 | + int width = reader.ReadInt32(); |
| 137 | + int count = reader.ReadInt32(); |
| 138 | + int dataLen = reader.ReadInt32(); |
| 139 | + |
| 140 | + Assert.AreEqual(original.Width, width); |
| 141 | + Assert.AreEqual(original.Count, count); |
| 142 | + Assert.AreEqual(original.Data.Length, dataLen); |
| 143 | + |
| 144 | + ulong[] data = new ulong[dataLen]; |
| 145 | + for(int i=0; i<dataLen; i++) data[i] = reader.ReadUInt64(); |
| 146 | + |
| 147 | + CompactArray loaded = new CompactArray(data, width, count); |
| 148 | + for(int i=0; i<values.Length; i++) Assert.AreEqual(values[i], (long)loaded.Get(i)); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + if (BitConverter.IsLittleEndian) |
| 153 | + { |
| 154 | + byte[] manualBytes; |
| 155 | + byte[] optimizedBytes; |
| 156 | + |
| 157 | + using (MemoryStream ms = new MemoryStream()) |
| 158 | + using (BinaryWriter writer = new BinaryWriter(ms)) |
| 159 | + { |
| 160 | + writer.Write(original.Width); |
| 161 | + writer.Write(original.Count); |
| 162 | + writer.Write(original.Data.Length); |
| 163 | + for(int i=0; i<original.Data.Length; i++) writer.Write(original.Data[i]); |
| 164 | + manualBytes = ms.ToArray(); |
| 165 | + } |
| 166 | + |
| 167 | + using (MemoryStream ms = new MemoryStream()) |
| 168 | + using (BinaryWriter writer = new BinaryWriter(ms)) |
| 169 | + { |
| 170 | + original.Write(writer); |
| 171 | + optimizedBytes = ms.ToArray(); |
| 172 | + } |
| 173 | + |
| 174 | + CollectionAssert.AreEqual(manualBytes, optimizedBytes, "Optimized serialization produced different bytes than standard loop!"); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments