Rat { private static BigFraction biasedRandom(boolean biased, int bias, BigFraction min, BigFraction max) { double drnd = biased ? bias(oracle.nextDouble(),bias) : oracle.nextDouble(); BigDecimal rnd = BigDecimal.valueOf(drnd); BigFraction r = BigFraction.valueOf(rnd); BigFraction offset = (max.subtract(min)).multiply(r); BigFraction res = min.add(offset); return res; }  static BigFraction BigFractionFromString(String value); static NamedType TypeFromString(String type); static EvaluatableValue ValueFromTypedFraction(NamedType ntype, BigFraction fvalue); static EvaluatableValue ValueFromString(String type, String value); static Expr toExpr(BigFraction x); static Expr cast(String name, Type type); static BigFraction biasedRandom(Type type, boolean biased, int bias, BigFraction min, BigFraction max); static BigFraction roundDown(BigFraction max); static BigFraction roundUp(BigFraction min); static final Random oracle; }
@Test public void testRandomCoverage() { try { System.out.println("Testing random coverage" + "\n"); boolean stdOutput = true; boolean fileOutput = false; final int totalCount = 10000; final NamedType nt = NamedType.INT; final int minI = -128; final int maxI = 127; final BigFraction min = new BigFraction(BigInteger.valueOf(minI)); final BigFraction max = new BigFraction(BigInteger.valueOf(maxI)); ArrayList<Integer> bias = new ArrayList<Integer>(); bias.add(-1); bias.add(0); bias.add(1); int aBias; ArrayList<Integer> missing = new ArrayList<Integer>(); for(int bIndex = 0; bIndex < bias.size(); bIndex++){ aBias = bias.get(bIndex); if(stdOutput){ System.out.println("bias: " + aBias); } BufferedWriter myOut = null; if(fileOutput){ String myFileName = buildOutputFilename("random_freq_", aBias, totalCount); String rangeString = "_range_" + min + "--" + max; String typeString = "_type-" + nt.toString(); myFileName = myFileName.concat(rangeString).concat(typeString).concat(".txt"); myOut = new BufferedWriter(new FileWriter(myFileName)); System.out.println(myFileName + "\n"); } HashMap<Integer,Integer> hm = new HashMap<Integer, Integer>(); for(int i = 0; i < totalCount; i += 1){ BigFraction resBF = Rat.biasedRandom(nt, true, aBias, min, max); double res = resBF.doubleValue(); if(hm.containsKey((int)res)){ Integer freq = hm.get((int)res); freq = freq + 1; hm.put((int)res, freq); } else { hm.put((int)res, 1); } } if(fileOutput){ for(int i = minI; i <= maxI; i++){ if(hm.containsKey(i)){ int freq = hm.get(i); myOut.write(i + ", " + freq + "\n"); } else myOut.write(i + ", " + 0 + "\n"); } } if(! (myOut==null)){ myOut.close(); } for(int i = minI; i <= maxI; i++){ if(! hm.containsKey(i)) missing.add(i); } if(stdOutput){ System.out.println("missing count: " + missing.size()); System.out.println("missing: \n" + missing + "\n"); } Assert.assertFalse(missing.contains(minI)); Assert.assertFalse(missing.contains(maxI)); missing.clear(); } } catch (Throwable e) { throw new Error(e); } }