1 /* 2 * Copyright 2023 Code Intelligence GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.code_intelligence.jazzer.mutation.api; 18 19 import com.google.errorprone.annotations.CheckReturnValue; 20 21 /** 22 * Knows how to initialize and mutate objects of type {@code T} and how to incorporate 23 * (cross over) parts of another object of the same type. 24 * 25 * <p>Certain types can be mutated fully in place. In such cases, prefer implementing the more 26 * versatile {@link InPlaceMutator} instead. 27 * 28 * <p>Implementations 29 * <ul> 30 * <li>MAY weakly associate mutable state with the identity (not equality class) of objects they 31 * have been passed as arguments or returned from initialization functions; 32 * <li>MAY assume that they are only passed arguments that they have initialized or mutated;</li> 33 * <li>SHOULD use {@link com.code_intelligence.jazzer.mutation.support.WeakIdentityHashMap} for 34 * this purpose; 35 * <li>MUST otherwise be deeply immutable; 36 * <li>SHOULD override {@link Object#toString()} to return {@code 37 * Debuggable.getDebugString(this)}. 38 * </ul> 39 * 40 * @param <T> the type this mutator operates on 41 */ 42 public interface ValueMutator<T> extends Debuggable { 43 /** 44 * Implementations 45 * <ul> 46 * <li>SHOULD, when called repeatedly, return a low amount of duplicates. 47 * </ul> 48 * 49 * @return an instance of {@code T} 50 */ init(PseudoRandom prng)51 @CheckReturnValue T init(PseudoRandom prng); 52 53 /** 54 * Implementations 55 * <ul> 56 * <li>MUST return a value that does not {@link Object#equals(Object)} the argument (if 57 * possible); 58 * <li>SHOULD, when called repeatedly, be able to eventually return any valid value of 59 * type {@code T}; 60 * <li>MAY mutate the argument. 61 * </ul> 62 */ mutate(T value, PseudoRandom prng)63 @CheckReturnValue T mutate(T value, PseudoRandom prng); 64 65 /** 66 * Implementations 67 * <ul> 68 * <li>MUST return a value that does not {@link Object#equals(Object)} the arguments (if 69 * possible); 70 * <li>MAY mutate {@code value}. 71 * <li>MUST NOT mutate {@code otherValue}. 72 * </ul> 73 */ crossOver(T value, T otherValue, PseudoRandom prng)74 @CheckReturnValue T crossOver(T value, T otherValue, PseudoRandom prng); 75 } 76