|
| 1 | +package sqlancer; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 8 | +import static org.junit.jupiter.api.Assertions.fail; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +public class TestRandomly { |
| 17 | + |
| 18 | + private static final int NR_CHECKS = 10000; |
| 19 | + private static final int NR_MIN_RUNS = 100000; |
| 20 | + |
| 21 | + @Test // test that every option is picked |
| 22 | + public void testFromOptions() { |
| 23 | + Integer[] options = { 1, 2, 3 }; |
| 24 | + List<Integer> remainingOptions = new ArrayList<>(Arrays.asList(options)); |
| 25 | + while (!remainingOptions.isEmpty()) { |
| 26 | + Integer pickedOption = Randomly.fromOptions(options); |
| 27 | + remainingOptions.remove(pickedOption); |
| 28 | + } |
| 29 | + assertTrue(remainingOptions.isEmpty()); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testSubset() { |
| 34 | + boolean encounteredEmptySubset = false; |
| 35 | + boolean encounteredOriginalSet = false; |
| 36 | + boolean encounteredStrictSubsetNonEmpty = false; |
| 37 | + Integer[] options = { 1, 2, 3 }; |
| 38 | + List<Integer> optionList = new ArrayList<>(Arrays.asList(options)); |
| 39 | + do { |
| 40 | + List<Integer> subset = Randomly.subset(optionList); |
| 41 | + assertEquals(optionList.size(), 3); // check that the original set hasn't been modified |
| 42 | + assertTrue(optionList.containsAll(subset)); |
| 43 | + if (subset.isEmpty()) { |
| 44 | + encounteredEmptySubset = true; |
| 45 | + } else if (subset.size() == optionList.size()) { |
| 46 | + encounteredOriginalSet = true; |
| 47 | + } else { |
| 48 | + encounteredStrictSubsetNonEmpty = true; |
| 49 | + } |
| 50 | + } while (!encounteredEmptySubset || !encounteredOriginalSet || !encounteredStrictSubsetNonEmpty); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testString() { |
| 55 | + boolean encounteredInteger = false; |
| 56 | + boolean encounteredAscii = false; |
| 57 | + boolean encounteredNonAscii = false; |
| 58 | + boolean encounteredSpace = false; |
| 59 | + Randomly r = new Randomly(); |
| 60 | + int i = 0; |
| 61 | + do { |
| 62 | + String s = r.getString(); |
| 63 | + for (Character c : s.toCharArray()) { |
| 64 | + if (Character.isAlphabetic(c)) { |
| 65 | + encounteredAscii = true; |
| 66 | + } else if (Character.isDigit(c)) { |
| 67 | + encounteredInteger = true; |
| 68 | + } else if (Character.isSpaceChar(c)) { |
| 69 | + encounteredSpace = true; |
| 70 | + } else { |
| 71 | + encounteredNonAscii = true; |
| 72 | + } |
| 73 | + } |
| 74 | + } while (!encounteredInteger || !encounteredAscii || !encounteredNonAscii || !encounteredSpace |
| 75 | + || i++ < NR_MIN_RUNS); |
| 76 | + } |
| 77 | + |
| 78 | + @Test // TODO: also generate and check for NaN |
| 79 | + public void testDouble() { |
| 80 | + Randomly r = new Randomly(); |
| 81 | + boolean encounteredZero = false; |
| 82 | + boolean encounteredPositive = false; |
| 83 | + boolean encounteredNegative = false; |
| 84 | + boolean encounteredInfinity = false; |
| 85 | + do { |
| 86 | + double doubleVal = r.getDouble(); |
| 87 | + if (doubleVal == 0) { |
| 88 | + encounteredZero = true; |
| 89 | + } else if (Double.isInfinite(doubleVal)) { |
| 90 | + encounteredInfinity = true; |
| 91 | + } else if (doubleVal > 0) { |
| 92 | + encounteredPositive = true; |
| 93 | + } else if (doubleVal < 0) { |
| 94 | + encounteredNegative = true; |
| 95 | + } else { |
| 96 | + fail(String.valueOf(doubleVal)); |
| 97 | + } |
| 98 | + } while (!encounteredZero || !encounteredPositive || !encounteredNegative || !encounteredInfinity); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testFiniteDouble() { |
| 103 | + Randomly r = new Randomly(); |
| 104 | + for (int i = 0; i < NR_MIN_RUNS; i++) { |
| 105 | + assertFalse(Double.isInfinite(r.getFiniteDouble())); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testNonZeroInteger() { |
| 111 | + Randomly r = new Randomly(); |
| 112 | + boolean encounteredPositive = false; |
| 113 | + boolean encounteredNegative = false; |
| 114 | + int i = 0; |
| 115 | + do { |
| 116 | + long nonZeroInt = r.getNonZeroInteger(); |
| 117 | + assertNotEquals(0, nonZeroInt); |
| 118 | + if (nonZeroInt > 0) { |
| 119 | + encounteredPositive = true; |
| 120 | + } else { |
| 121 | + encounteredNegative = true; |
| 122 | + } |
| 123 | + } while (!encounteredPositive || !encounteredNegative || i++ < NR_MIN_RUNS); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testPositiveInteger() { |
| 128 | + Randomly r = new Randomly(); |
| 129 | + boolean encounteredZero = false; |
| 130 | + boolean encounteredMaxValue = false; |
| 131 | + int i = 0; |
| 132 | + do { |
| 133 | + long positiveInt = r.getPositiveInteger(); |
| 134 | + assertTrue(positiveInt >= 0); |
| 135 | + if (positiveInt == 0) { |
| 136 | + encounteredZero = true; |
| 137 | + } else if (positiveInt == Long.MAX_VALUE) { |
| 138 | + encounteredMaxValue = true; |
| 139 | + } |
| 140 | + } while (!encounteredZero || !encounteredMaxValue || i++ < NR_MIN_RUNS); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void testBytes() { |
| 145 | + Randomly r = new Randomly(); |
| 146 | + boolean encounteredAllZeroes = false; |
| 147 | + boolean encounteredMax = false; |
| 148 | + boolean encounteredZeroLength = false; |
| 149 | + int i = 0; |
| 150 | + do { |
| 151 | + byte[] bytes = r.getBytes(); |
| 152 | + if (bytes.length == 0) { |
| 153 | + encounteredZeroLength = true; |
| 154 | + } else if (bytes[0] == 0) { |
| 155 | + encounteredAllZeroes = true; |
| 156 | + } else if (bytes[0] == Byte.MAX_VALUE) { |
| 157 | + encounteredMax = true; |
| 158 | + } |
| 159 | + } while (!encounteredAllZeroes || !encounteredMax || !encounteredZeroLength || i++ < NR_MIN_RUNS); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void testNonCachedInteger() { |
| 164 | + assertEquals(0, Randomly.getNotCachedInteger(0, 1)); |
| 165 | + assertThrows(Exception.class, () -> Randomly.getNotCachedInteger(0, 0)); |
| 166 | + assertThrows(Exception.class, () -> Randomly.getNotCachedInteger(5, 0)); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + public void testInteger() { |
| 171 | + Randomly r = new Randomly(); |
| 172 | + // TODO: we should throw an exception instead |
| 173 | + assertEquals(0, r.getInteger(0, 0)); |
| 174 | + assertEquals(0, r.getInteger(0, 1)); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void testLong() { |
| 179 | + Randomly r = new Randomly(); |
| 180 | + // TODO: we should throw an exception instead |
| 181 | + assertEquals(0, r.getLong(0, 0)); |
| 182 | + assertEquals(0, r.getLong(0, 1)); |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments