All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.jqwik.engine.properties.shrinking.ShrinkableString Maven / Gradle / Ivy

The newest version!
package net.jqwik.engine.properties.shrinking;

import java.util.*;
import java.util.stream.*;

import net.jqwik.api.*;
import net.jqwik.engine.properties.*;
import net.jqwik.engine.support.*;

public class ShrinkableString extends ShrinkableContainer {

	public static final Set> UNIQUE_CHARS_EXTRACTOR = Collections.singleton(FeatureExtractor.identity());

	public ShrinkableString(
		List> elements, int minSize, int maxSize,
		Arbitrary characterArbitrary,
		boolean uniqueChars
	) {
		this(elements, minSize, maxSize, uniquenessExtractors(uniqueChars), characterArbitrary);
	}

	private ShrinkableString(
		List> elements, int minSize, int maxSize,
		Collection> uniquenessExtractors,
		Arbitrary characterArbitrary
	) {
		super(elements, minSize, maxSize, uniquenessExtractors, characterArbitrary);
	}

	private static Collection> uniquenessExtractors(boolean uniqueChars) {
		return uniqueChars ? UNIQUE_CHARS_EXTRACTOR : Collections.emptySet();
	}

	@Override
	String createValue(List> shrinkables) {
		// Using loop instead of stream to make stack traces more readable
		StringBuilder builder = new StringBuilder(shrinkables.size());
		for (Shrinkable shrinkable : shrinkables) {
			builder.appendCodePoint(shrinkable.value());
		}
		return builder.toString();
	}

	@Override
	Shrinkable createShrinkable(List> shrunkElements) {
		return new ShrinkableString(shrunkElements, minSize, maxSize, uniquenessExtractors, elementArbitrary);
	}

	@Override
	public Stream> shrink() {
		if (elements.size() > 100) {
			return JqwikStreamSupport.concat(
				shrinkSizeAggressively(),
				shrinkSizeOfList(),
				shrinkElementsOneAfterTheOther(100)
			);
		}
		return JqwikStreamSupport.concat(
			shrinkSizeOfList(),
			shrinkElementsOneAfterTheOther(0),
			shrinkPairsOfElements(),
			sortElements()
		);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy