Made WordAddressableMemoryTest less random and better at finding errors.
authorChristian Femers <femers@in.tum.de>
Mon, 16 Sep 2019 00:39:16 +0000 (02:39 +0200)
committerChristian Femers <femers@in.tum.de>
Mon, 16 Sep 2019 00:39:16 +0000 (02:39 +0200)
net.mograsim.machine/test/net/mograsim/machine/standard/memory/WordAddressableMemoryTest.java

index 9d38b89..9571e93 100644 (file)
@@ -4,8 +4,10 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 
 import java.math.BigInteger;
 import java.util.Random;
+import java.util.stream.LongStream;
 
-import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
 
 import net.mograsim.logic.core.timeline.Timeline;
 import net.mograsim.logic.core.types.Bit;
@@ -19,8 +21,9 @@ class WordAddressableMemoryTest
 
        private Timeline t = new Timeline(10);
 
-       @Test
-       public void wordAddressableMemoryLargeTest()
+       @ParameterizedTest(name = "seed = {0}")
+       @MethodSource("seedsForRandom")
+       public void wordAddressableMemoryLargeTest(long seed)
        {
                CoreWire rW = new CoreWire(t, 1, 2);
                CoreWire data = new CoreWire(t, 16, 2);
@@ -34,13 +37,12 @@ class WordAddressableMemoryTest
                                new WordAddressableMemory(MainMemoryDefinition.create(64, 16, 4096L, Long.MAX_VALUE)), data.createReadWriteEnd(),
                                rW.createReadOnlyEnd(), address.createReadOnlyEnd());
 
-               Random r = new Random();
+               Random r = new Random(seed);
                for (long j = 1; j > 0; j *= 2)
                {
-                       for (int i = 0; i < 50; i++)
+                       for (int i = 0; i < 100; i++)
                        {
-                               String sAddress = String.format("%64s", BigInteger.valueOf(4096 + i + j).toString(2)).replace(' ', '0');
-                               BitVector bAddress = BitVector.parse(sAddress);
+                               BitVector bAddress = BitVector.from(4096 + i + j, 64);
                                addressI.feedSignals(bAddress);
                                t.executeAll();
                                String random = BigInteger.valueOf(Math.abs(r.nextInt())).toString(5);
@@ -56,9 +58,13 @@ class WordAddressableMemoryTest
                                dataI.clearSignals();
                                t.executeAll();
 
-                               assertEquals(dataI.getValues(), vector);
+                               assertEquals(dataI.getValues(), vector, "seed=" + seed + ", j=" + j + ", i=" + i);
                        }
                }
        }
 
+       public static LongStream seedsForRandom()
+       {
+               return LongStream.range(0, 100);
+       }
 }