Skip to content

Commit

Permalink
Correcting Map tests and generator in the same manner we did for hash…
Browse files Browse the repository at this point in the history
… sets -- not guaranteeing size of map/set, but attempting inserts size times.
  • Loading branch information
pholser committed Jun 22, 2012
1 parent f1a17d6 commit c6f329d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,10 @@ public HashMapGenerator() {
public HashMap<?, ?> generate(SourceOfRandomness random, int size) {
HashMap<Object, Object> items = new HashMap<Object, Object>();

for (int itemsAdded = 0; itemsAdded < size;) {
for (int itemsAdded = 0; itemsAdded < size; ++itemsAdded) {
Object key = componentGenerators.get(0).generate(random, size);
Object value = componentGenerators.get(1).generate(random, size);
if (!items.containsKey(key)) {
items.put(key, value);
++itemsAdded;
}
items.put(key, value);
}

return items;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
The MIT License
Copyright (c) 2010-2012 Paul R. Holser, Jr.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.pholser.junit.quickcheck;

import java.util.Map;

import org.junit.Test;
import org.junit.contrib.theories.Theories;
import org.junit.contrib.theories.Theory;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;
import static org.junit.experimental.results.PrintableResult.*;
import static org.junit.experimental.results.ResultMatchers.*;

public class ForAllMapTheoryParameterTypesTest {
@Test
public void huhToHuh() {
assertThat(testResult(MapOfHuhToHuh.class), isSuccessful());
}

@RunWith(Theories.class)
public static class MapOfHuhToHuh {
@Theory
public void shouldHold(@ForAll(sampleSize = 12) Map<?, ?> items) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public class MapOfIntegerToStringTest extends GeneratingUniformRandomValuesForTh
@Override
protected void primeSourceOfRandomness() {
when(randomForParameterGenerator.nextInt(-1, 1)).thenReturn(1);
when(randomForParameterGenerator.nextInt(-2, 2)).thenReturn(-2).thenReturn(-2).thenReturn(2);
when(randomForParameterGenerator.nextInt(-2, 2)).thenReturn(-2).thenReturn(2);
when(randomForParameterGenerator.nextInt(Character.MIN_VALUE, Character.MAX_VALUE))
.thenReturn((int) 'a').thenReturn((int) 'b').thenReturn((int) 'c').thenReturn((int) 'd')
.thenReturn((int) 'e').thenReturn((int) 'f').thenReturn((int) 'g');
.thenReturn((int) 'e');
}

@Override
Expand All @@ -62,13 +62,13 @@ protected int sampleSize() {
protected List<?> randomValues() {
Map<Integer, String> doubleton = newHashMap();
doubleton.put(-2, "bc");
doubleton.put(2, "fg");
doubleton.put(2, "de");
return asList(newHashMap(), singletonMap(1, "a"), doubleton);
}

@Override
public void verifyInteractionWithRandomness() {
verify(randomForParameterGenerator).nextInt(-1, 1);
verify(randomForParameterGenerator, times(7)).nextInt(Character.MIN_VALUE, Character.MAX_VALUE);
verify(randomForParameterGenerator, times(5)).nextInt(Character.MIN_VALUE, Character.MAX_VALUE);
}
}

0 comments on commit c6f329d

Please sign in to comment.