001// Generated by delombok at Fri Mar 03 18:26:02 UTC 2023 002/* 003 * Licensed to the author under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package de.cuioss.test.generator.internal.net.java.quickcheck; 019 020import java.io.PrintWriter; 021// owolff ok for testcode 022/** 023 * QuickCheck is an implementation of the Haskell QuickCheck generator based 024 * test tool (<a href="http://www.cs.chalmers.se/~rjmh/QuickCheck/">...</a>). 025 */ 026@SuppressWarnings("java:S106") 027public final class QuickCheck { 028 public static final int MAX_NUMBER_OF_RUNS = 200; 029 public static final int MIN_NUMBER_OF_RUNS = 1; 030 public static final String SYSTEM_PROPERTY_RUNS = propertyName("RUNS"); 031 032 /** 033 * Check the {@link Characteristic} for all values generated by the given 034 * {@link Generator}. The execution will fail fast if any of the calls of 035 * the {@link Characteristic#specify(Object)} method throws an exception. 036 * 037 * @param <T> 038 * type of the generated values 039 * @throws CharacteristicException 040 * if a characteristic is not {@code true} for a generated 041 * value 042 * @throws GeneratorException 043 * if generation of the next value failed. 044 */ 045 public static <T> void forAll(Generator<T> generator, Characteristic<T> characteristic) throws GeneratorException, CharacteristicException { 046 runner(characteristic, MAX_NUMBER_OF_RUNS, generator, new PrintWriter(new NullWriter())).forAll(); 047 } 048 049 public static int getDefaultNumberOfRuns() { 050 var runs = Integer.getInteger(SYSTEM_PROPERTY_RUNS, MAX_NUMBER_OF_RUNS); 051 return Math.max(MIN_NUMBER_OF_RUNS, runs); 052 } 053 054 private static String propertyName(String name) { 055 return QuickCheck.class.getSimpleName() + "." + name; 056 } 057 058 /** 059 * Check the {@link Characteristic} for all values generated by the given 060 * {@link Generator}. The execution will fail fast if any of the calls of 061 * the {@link Characteristic#specify(Object)} method throws an exception. 062 * 063 * @param runs 064 * number of runs and generated values for this characteristic 065 * @param <T> 066 * type of the generated values 067 * @throws CharacteristicException 068 * if a characteristic is not {@code true} for a generated 069 * value 070 * @throws GeneratorException 071 * if generation of the next value failed. 072 */ 073 public static <T> void forAll(int runs, Generator<T> generator, Characteristic<T> characteristic) throws GeneratorException, CharacteristicException { 074 runner(characteristic, runs, generator, new PrintWriter(new NullWriter())).forAll(); 075 } 076 077 /** 078 * Check the {@link Characteristic} for all values generated by the given 079 * {@link Generator}. The execution will fail fast if any of the calls of 080 * the {@link Characteristic#specify(Object)} method throws an exception. 081 * 082 * @param <T> 083 * type of the generated values 084 * @throws CharacteristicException 085 * if a characteristic is not {@code true} for a generated 086 * value 087 * @throws GeneratorException 088 * if generation of the next value failed. 089 */ 090 public static <T> void forAllVerbose(Generator<T> generator, Characteristic<T> characteristic) throws GeneratorException, CharacteristicException { 091 runner(characteristic, MAX_NUMBER_OF_RUNS, generator, new PrintWriter(new PrintWriter(System.out))).forAll(); 092 } 093 094 /** 095 * Check the {@link Characteristic} for all values generated by the given 096 * {@link Generator}. The execution will fail fast if any of the calls of 097 * the {@link Characteristic#specify(Object)} method throws an exception. 098 * 099 * @param runs 100 * number of runs and generated values for this characteristic 101 * @param <T> 102 * type of the generated values 103 * @throws CharacteristicException 104 * if a characteristic is not {@code true} for a generated 105 * value 106 * @throws GeneratorException 107 * if generation of the next value failed. 108 */ 109 public static <T> void forAllVerbose(int runs, Generator<T> generator, Characteristic<T> characteristic) throws GeneratorException, CharacteristicException { 110 runner(characteristic, runs, generator, new PrintWriter(new PrintWriter(System.out))).forAll(); 111 } 112 113 /** 114 * All executions of {@link Characteristic#specify(Object)} which execute 115 * this method will be skipped and a new test case 116 * will be generated. Execution will be stopped if it is not possible to 117 * create a new test cases after a reasonable amount of tries. 118 * 119 * @param predicate 120 * Skip the current test case if the predicate is true. 121 */ 122 public static void guard(boolean predicate) { 123 if (!predicate) { 124 throw new GuardException(); 125 } 126 } 127 128 private static <T> Runner<T> runner(Characteristic<T> characteristic, int runs, Generator<T> generator, PrintWriter writer) { 129 return new RunnerImpl<>(characteristic, runs, generator, writer); 130 } 131 132 @java.lang.SuppressWarnings("all") 133 @lombok.Generated 134 private QuickCheck() { 135 throw new java.lang.UnsupportedOperationException("This is a utility class and cannot be instantiated"); 136 } 137}