GeoIP2IPGenerator extends BaseIPGenerator implements IPGenerator { @Override public String pollRandomIP() { long serachAccum = (long) (getRandom().nextDouble() * sumOfRangesLengths); int idx = Collections.binarySearch(accumRangeLengths, serachAccum); idx = idx >= 0 ? idx : -idx - 1; idx = idx >= ranges.size() ? ranges.size() - 1 : idx; IPRange range = ranges.get(idx); int ip = range.from + getRandom().nextInt(range.to - range.from); return IPUtil.convertIPv4(ip); } GeoIP2IPGenerator(final Set<String> countryCodes);  GeoIP2IPGenerator(final Set<String> countryCodes, final long seed);  GeoIP2IPGenerator(final Set<String> countryCodes, final InputStream is, final long seed);  GeoIP2IPGenerator(final Set<String> countryCodes, final InputStream is);  GeoIP2IPGenerator(final Set<String> countryCodes, final File f, final long seed);  GeoIP2IPGenerator(final Set<String> countryCodes, final File f); @Override String pollRandomIP();  }
@Test public strictfp void testIPGeneration() { final int TEST_SIZE = 20_000; final double DOUBLE_PRECISION = 0.01; Map<String, Integer> counts = new HashMap<>(); for (String code : COUNTRY_CODES) { counts.put(code, 0); } for (int i = 0; i < TEST_SIZE; i++) { String ip = generator.pollRandomIP(service, -1); String code = service.getMetaData(ip).getCountryIsoCode(); if (COUNTRY_CODES.contains(code)) { counts.put(code, counts.get(code) + 1); } else { fail("IP " + Objects.toString(ip) + " is in location " + Objects.toString(code)); } } assertEquals(NUM_AU_IPS_IN_TEST / IPS_IN_COUNTRY_CODES, counts.get("AU") / (double) TEST_SIZE, DOUBLE_PRECISION); assertEquals(NUM_CN_IPS_IN_TEST / IPS_IN_COUNTRY_CODES, counts.get("CN") / (double) TEST_SIZE, DOUBLE_PRECISION); assertEquals(NUM_JP_IPS_IN_TEST / IPS_IN_COUNTRY_CODES, counts.get("JP") / (double) TEST_SIZE, DOUBLE_PRECISION); assertFalse(counts.containsKey("TH")); }