@Property public void setToNearestShouldNeverBeBelowMinDate( @InRange(min = "01/01/1900", max = "12/31/2099", format = "MM/dd/yyyy") Date date, @InRange(min = "01/01/1900", max = "12/31/2099", format = "MM/dd/yyyy") Date minDate ) { DefaultDateRangeLimiter limiter = new DefaultDateRangeLimiter(); Calendar day = Calendar.getInstance(); day.setTime(date); Calendar minDay = Calendar.getInstance(); minDay.setTime(minDate); limiter.setMinDate(minDay); Assert.assertTrue(Utils.trimToMidnight(minDay).getTimeInMillis() <= limiter.setToNearestDate(day).getTimeInMillis()); }
/** * <p>Tells this generator to produce values within a specified * {@linkplain InRange#min() minimum} and/or {@linkplain InRange#max() * maximum}, inclusive, with uniform distribution, down to the * nanosecond.</p> * * <p>If an endpoint of the range is not specified, the generator will use * dates with values of either {@link LocalDateTime#MIN} or * {@link LocalDateTime#MAX} as appropriate.</p> * * <p>{@link InRange#format()} describes * {@linkplain DateTimeFormatter#ofPattern(String) how the generator is to * interpret the range's endpoints}.</p> * * @param range annotation that gives the range's constraints */ public void configure(InRange range) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern(range.format()); if (!defaultValueOf(InRange.class, "min").equals(range.min())) min = LocalDateTime.parse(range.min(), formatter); if (!defaultValueOf(InRange.class, "max").equals(range.max())) max = LocalDateTime.parse(range.max(), formatter); if (min.compareTo(max) > 0) throw new IllegalArgumentException(String.format("bad range, %s > %s", range.min(), range.max())); }
/** * Tells this generator to produce values within a specified minimum and/or * maximum, inclusive, with uniform distribution. * * {@link InRange#min} and {@link InRange#max} take precedence over * {@link InRange#minInt()} and {@link InRange#maxInt()}, if non-empty. * * @param range annotation that gives the range's constraints */ public void configure(InRange range) { min = range.min().isEmpty() ? range.minInt() : Integer.parseInt(range.min()); max = range.max().isEmpty() ? range.maxInt() : Integer.parseInt(range.max()); }
/** * Tells this generator to produce values within a specified minimum and/or * maximum, inclusive, with uniform distribution. * * {@link InRange#min} and {@link InRange#max} take precedence over * {@link InRange#minShort()} and {@link InRange#maxShort()}, if non-empty. * * @param range annotation that gives the range's constraints */ public void configure(InRange range) { min = range.min().isEmpty() ? range.minShort() : Short.parseShort(range.min()); max = range.max().isEmpty() ? range.maxShort() : Short.parseShort(range.max()); }
/** * Tells this generator to produce values within a specified minimum and/or * maximum, inclusive, with uniform distribution. * * {@link InRange#min} and {@link InRange#max} take precedence over * {@link InRange#minLong()} and {@link InRange#maxLong()}, if non-empty. * * @param range annotation that gives the range's constraints */ public void configure(InRange range) { min = range.min().isEmpty() ? range.minLong() : Long.parseLong(range.min()); max = range.max().isEmpty() ? range.maxLong() : Long.parseLong(range.max()); }
/** * <p>Tells this generator to produce values within a specified * {@linkplain InRange#min() minimum} and/or {@linkplain InRange#max() * maximum}, inclusive, with uniform distribution, down to the * nanosecond.</p> * * <p>If an endpoint of the range is not specified, the generator will use * instants with values of either {@link Instant#MIN} or * {@link Instant#MAX} as appropriate.</p> * * <p>{@linkplain InRange#format()} is ignored. Instants are always * parsed using {@link java.time.format.DateTimeFormatter#ISO_INSTANT}.</p> * * @param range annotation that gives the range's constraints */ public void configure(InRange range) { if (!defaultValueOf(InRange.class, "min").equals(range.min())) min = Instant.parse(range.min()); if (!defaultValueOf(InRange.class, "max").equals(range.max())) max = Instant.parse(range.max()); if (min.compareTo(max) > 0) throw new IllegalArgumentException(String.format("bad range, %s > %s", range.min(), range.max())); }
/** * Tells this generator to produce values within a specified minimum and/or * maximum, inclusive, with uniform distribution. * * {@link InRange#min} and {@link InRange#max} take precedence over * {@link InRange#minChar()} and {@link InRange#maxChar()}, if non-empty. * * @param range annotation that gives the range's constraints */ public void configure(InRange range) { min = range.min().isEmpty() ? range.minChar() : range.min().charAt(0); max = range.max().isEmpty() ? range.maxChar() : range.max().charAt(0); }
/** * Tells this generator to produce values within a specified minimum and/or * maximum, inclusive, with uniform distribution. * * {@link InRange#min} and {@link InRange#max} take precedence over * {@link InRange#minByte()} and {@link InRange#maxByte()}, if non-empty. * * @param range annotation that gives the range's constraints */ public void configure(InRange range) { min = range.min().isEmpty() ? range.minByte() : Byte.parseByte(range.min()); max = range.max().isEmpty() ? range.maxByte() : Byte.parseByte(range.max()); }
/** * Tells this generator to produce values within a specified minimum * (inclusive) and/or maximum (exclusive) with uniform distribution. * * {@link InRange#min} and {@link InRange#max} take precedence over * {@link InRange#minFloat()} and {@link InRange#maxFloat()}, if non-empty. * * @param range annotation that gives the range's constraints */ public void configure(InRange range) { min = range.min().isEmpty() ? range.minFloat() : Float.parseFloat(range.min()); max = range.max().isEmpty() ? range.maxFloat() : Float.parseFloat(range.max()); }
/** * Tells this generator to produce values within a specified minimum * (inclusive) and/or maximum (exclusive) with uniform distribution. * * {@link InRange#min} and {@link InRange#max} take precedence over * {@link InRange#minDouble()} and {@link InRange#maxDouble()}, * if non-empty. * * @param range annotation that gives the range's constraints */ public void configure(InRange range) { min = range.min().isEmpty() ? range.minDouble() : Double.parseDouble(range.min()); max = range.max().isEmpty() ? range.maxDouble() : Double.parseDouble(range.max()); }
@Override public int[] generate(SourceOfRandomness r, GenerationStatus generationStatus) { int minSize = size != null ? size.min() : 0; int maxSize = size != null ? size.max() : Integer.MAX_VALUE; int size = r.nextInt(minSize, maxSize); int[] requests = new int[size]; int minRange = range != null ? range.minInt() : 0; int maxRange = range != null ? range.maxInt() : 1024; for (int i = 0; i < size; i++) { int request; // Maybe re-use if (i > 0 && r.nextBoolean()) { int idx = r.nextInt(0, i); request = requests[idx]; } else { request = r.nextInt(minRange, maxRange); } requests[i] = request; } return requests; } }
/** * <p>Tells this generator to produce values within a specified * {@linkplain InRange#min() minimum} and/or {@linkplain InRange#max() * maximum}, inclusive, with uniform distribution, down to the * nanosecond.</p> * * <p>Instances of this class are configured using {@link Instant} * strings.</p> * * <p>If an endpoint of the range is not specified, the generator will use * instants with values of either {@link Instant#MIN} or * {@link Instant#MAX} as appropriate.</p> * * <p>{@linkplain InRange#format()} is ignored. Instants are always * parsed using {@link java.time.format.DateTimeFormatter#ISO_INSTANT}.</p> * * @param range annotation that gives the range's constraints */ public void configure(InRange range) { if (!defaultValueOf(InRange.class, "min").equals(range.min())) min = Instant.parse(range.min()); if (!defaultValueOf(InRange.class, "max").equals(range.max())) max = Instant.parse(range.max()); if (min.compareTo(max) > 0) throw new IllegalArgumentException(String.format("bad range, %s > %s", range.min(), range.max())); }
@Property public void setToNearestShouldNeverBeInDisabledDays( @InRange(min = "01/01/1900", max = "12/31/2099", format = "MM/dd/yyyy") Date date, @InRange(min = "01/01/1900", max = "12/31/2099", format = "MM/dd/yyyy") Date[] dates ) { DefaultDateRangeLimiter limiter = new DefaultDateRangeLimiter(); Calendar day = Calendar.getInstance(); day.setTime(date); Calendar[] disableds = datesToCalendars(dates); limiter.setDisabledDays(disableds); Assert.assertFalse(Arrays.asList(disableds).contains(limiter.setToNearestDate(day))); }
/** * <p>Tells this generator to produce values within a specified * {@linkplain InRange#min() minimum} and/or {@linkplain InRange#max() * maximum}, inclusive, with uniform distribution, down to the * nanosecond.</p> * * <p>If an endpoint of the range is not specified, the generator will use * times with values of either {@link LocalTime#MIN} or * {@link LocalTime#MAX} as appropriate.</p> * * <p>{@link InRange#format()} describes * {@linkplain DateTimeFormatter#ofPattern(String) how the generator is to * interpret the range's endpoints}.</p> * * @param range annotation that gives the range's constraints */ public void configure(InRange range) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern(range.format()); if (!defaultValueOf(InRange.class, "min").equals(range.min())) min = LocalTime.parse(range.min(), formatter); if (!defaultValueOf(InRange.class, "max").equals(range.max())) max = LocalTime.parse(range.max(), formatter); if (min.compareTo(max) > 0) throw new IllegalArgumentException(String.format("bad range, %s > %s", range.min(), range.max())); }
/** * Tells this generator to produce values within a specified minimum and/or * maximum, inclusive, with uniform distribution. * * {@link InRange#min} and {@link InRange#max} take precedence over * {@link InRange#minInt()} and {@link InRange#maxInt()}, if non-empty. * * @param range annotation that gives the range's constraints */ public void configure(InRange range) { min = range.min().isEmpty() ? range.minInt() : Integer.parseInt(range.min()); max = range.max().isEmpty() ? range.maxInt() : Integer.parseInt(range.max()); }
/** * <p>Tells this generator to produce values within a specified * {@linkplain InRange#min() minimum} and/or * {@linkplain InRange#max() maximum} inclusive, with uniform * distribution.</p> * * <p>If an endpoint of the range is not specified, its value takes on * a magnitude influenced by * {@link com.pholser.junit.quickcheck.generator.GenerationStatus#size()}.</p> * @param range annotation that gives the range's constraints */ public void configure(InRange range) { if (!defaultValueOf(InRange.class, "min").equals(range.min())) min = new BigInteger(range.min()); if (!defaultValueOf(InRange.class, "max").equals(range.max())) max = new BigInteger(range.max()); if (min != null && max != null) checkRange(Ranges.Type.INTEGRAL, min, max); }
@Property public void setToNearestShouldNeverBeAboveMaxDate( @InRange(min = "01/01/1800", max = "12/31/2099", format = "MM/dd/yyyy") Date date, @InRange(min = "01/01/1800", max = "12/31/2099", format = "MM/dd/yyyy") Date maxDate ) { DefaultDateRangeLimiter limiter = new DefaultDateRangeLimiter(); Calendar day = Calendar.getInstance(); day.setTime(date); Calendar minDay = Calendar.getInstance(); minDay.set(Calendar.YEAR, 1800); minDay.set(Calendar.MONTH, Calendar.JANUARY); minDay.set(Calendar.DAY_OF_MONTH, 1); Utils.trimToMidnight(minDay); Calendar maxDay = Calendar.getInstance(); maxDay.setTime(maxDate); limiter.setMinDate(minDay); limiter.setMaxDate(maxDay); Assert.assertTrue(Utils.trimToMidnight(maxDay).getTimeInMillis() >= limiter.setToNearestDate(day).getTimeInMillis()); }
/** * <p>Tells this generator to produce values within a specified * {@linkplain InRange#min() minimum} and/or {@linkplain InRange#max() * maximum}, inclusive, with uniform distribution.</p> * * <p>If an endpoint of the range is not specified, the generator will use * dates with values of either {@link LocalDate#MIN} or * {@link LocalDate#MAX} as appropriate.</p> * * <p>{@link InRange#format()} describes * {@linkplain DateTimeFormatter#ofPattern(String) how the generator is to * interpret the range's endpoints}.</p> * * @param range annotation that gives the range's constraints */ public void configure(InRange range) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern(range.format()); if (!defaultValueOf(InRange.class, "min").equals(range.min())) min = LocalDate.parse(range.min(), formatter); if (!defaultValueOf(InRange.class, "max").equals(range.max())) max = LocalDate.parse(range.max(), formatter); if (min.compareTo(max) > 0) throw new IllegalArgumentException(String.format("bad range, %s > %s", range.min(), range.max())); }
/** * Tells this generator to produce values within a specified minimum and/or * maximum, inclusive, with uniform distribution. * * {@link InRange#min} and {@link InRange#max} take precedence over * {@link InRange#minInt()} and {@link InRange#maxInt()}, if non-empty. * * @param range annotation that gives the range's constraints */ public void configure(InRange range) { min = range.min().isEmpty() ? range.minInt() : Integer.parseInt(range.min()); max = range.max().isEmpty() ? range.maxInt() : Integer.parseInt(range.max()); }
/** * <p>Tells this generator to produce values within a specified * {@linkplain InRange#min() minimum} (inclusive) and/or * {@linkplain InRange#max() maximum} (exclusive), with uniform * distribution.</p> * * <p>If an endpoint of the range is not specified, its value takes on * a magnitude influenced by * {@link com.pholser.junit.quickcheck.generator.GenerationStatus#size()}.</p> * * @param range annotation that gives the range's constraints */ public void configure(InRange range) { if (!defaultValueOf(InRange.class, "min").equals(range.min())) min = new BigDecimal(range.min()); if (!defaultValueOf(InRange.class, "max").equals(range.max())) max = new BigDecimal(range.max()); if (min != null && max != null) checkRange(Ranges.Type.FLOAT, min, max); }