Commit 1d333f7a authored by Anya Helene Bagge's avatar Anya Helene Bagge 🦆

kopi fra f07

parent 584e22da
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>inf101v16f08</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import inf101.v16.datastructures.IList;
import inf101.v16.datastructures.MyList;
import inf101.v16.datastructures.Range;
public class Main {
public static void main(String[] args) {
String[] a = {"Hei,", "på", "deg", "!"};
List<String> list = new ArrayList<>();
list.add("Hei");
list.add("på");
list.add("deg!");
Iterator<String> iterator = list.iterator();
while(iterator.hasNext())
System.out.println(iterator.next());
for(int i : new Range(0, 10)) {
System.out.println(i);
}
}
}
package inf101.tests;
import static org.junit.Assert.fail;
import java.util.Random;
import static org.junit.Assert.assertEquals;
import inf101.util.IGenerator;
import inf101.util.generators.IntGenerator;
import inf101.util.generators.MyListGenerator;
import inf101.util.generators.StringGenerator;
import inf101.v16.datastructures.IList;
import inf101.v16.datastructures.MyList;
import inf101.v16.datastructures.Range;
import org.junit.Test;
public class MyListTest {
private static final int N = 10000;
private IGenerator<String> strGen = new StringGenerator();
private IGenerator<Integer> intGen = new IntGenerator();
private IGenerator<IList<String>> listGen = new MyListGenerator<>(strGen);
private IGenerator<IList<Integer>> intListGen = new MyListGenerator<>(intGen);
/**
* all cells are equals to val, after grid.clear(val)
*/
private <T> void clearProperty(IList<T> list) {
list.clear();
assertEquals(0, list.size());
}
/**
* Test that clear sets all elements to the given value
*/
@Test
public void clearTest() {
for (int i = 0; i < N / 1000; i++) {
IList<String> list = listGen.generate();
clearProperty(list);
}
}
@Test
public void equalsTest() {
StandardProperties.allEqualsTests(listGen, N / 100);
}
/**
* A set on an index doesn't affect a get on a different index
*/
private <T> void setGetIndependentProperty(IList<T> list, int a, int b, T val) {
if (a != b) {
T s = list.get(b);
list.set(a, val);
assertEquals(s, list.get(b));
}
}
/**
* Test that set() and get() on different coordinates are independent
*/
@Test
public void setGetIndepTest() {
IList<String> list = listGen.generate();
IGenerator<Integer> iGen = new IntGenerator(0, list.size());
for (int i = 0; i < N; i++) {
int a = iGen.generate();
int b = iGen.generate();
String s = strGen.generate();
setGetIndependentProperty(list, a, b, s);
}
}
/**
* get(a) is val after set(a, val)
*/
private <T> void setGetProperty(IList<T> list, int a, T val) {
list.set(a, val);
assertEquals(val, list.get(a));
}
@Test
public void setGetTest0() {
Random random = new Random();
for (int i : new Range(0, 100000)) {
IList<Integer> list = intListGen.generate();
if (list.size() > 0) {
int index = random.nextInt(list.size());
int elem = intGen.generate();
setGetProperty(list, index, elem);
}
}
}
/**
* Test that get gives back the same value after set.
*/
@Test
public void setGetTest() {
IList<String> list = listGen.generate();
IGenerator<Integer> iGen = new IntGenerator(0, list.size());
for (int i = 0; i < N; i++) {
int x = iGen.generate();
String s = strGen.generate();
setGetProperty(list, x, s);
}
}
@Test
public void iteratorTest() {
for (int i = 0; i < N; i++) {
IList<String> list = listGen.generate();
iteratorProperty(list);
}
}
private <T> void iteratorProperty(IList<T> list) {
int i = 0;
for (T obj : list) {
assertEquals(list.get(i), obj);
i = i + 1;
}
assertEquals(list.size(), i);
}
}
package inf101.tests;
import static org.junit.Assert.*;
import java.util.Random;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import inf101.v16.datastructures.IList;
import inf101.v16.datastructures.MyList;
public class MyOldListTest {
Random random = new Random();
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void setGetTest() {
IList<String> list = new MyList<>();
// enkelt forsøk på å lage tilfeldige testdata
for(int i = 0; i < random.nextInt(10000)+1; i++)
list.add(Integer.toString(random.nextInt()));
for(int i = 0; i < 10000; i++) {
setGetProperty(list, random.nextInt(list.size()), "banan");
}
}
@Test
public void setGetTest2() {
IList<String> list = new MyList<>();
list.add("Hei");
list.add("Hadet");
setGetProperty2(list, 1, 0, "banan");
setGetProperty2(list, 0, 1, "fdsaf");
}
/**
* Egenskap: hvis vi kaller add(), vil siste element være verdien vi la til
*
* @param list
* @param s
*/
public void addGetProperty(IList<String> list, String s) {
list.add(s);
assertEquals(s, list.get(list.size()-1));
}
/**
* Egenskap: hvis vi kaller add(), vil størrelsen øke med 1
*
* @param list
* @param s
*/
public void addSizeProperty(IList<String> list, String s) {
int size = list.size();
list.add(s);
assertEquals(size+1, list.size());
}
/**
* Egenskap: hvis vi kaller set(), vil get() returnere samme verdi
*
* @param list
* @param i
* @param s
*/
public void setGetProperty(IList<String> list, int i, String s) {
list.set(i, s);
assertEquals(s, list.get(i));
}
/**
* Egenskap: hvis vi kaller set(), vil verdiene på andre indekser ikke endre seg
*
* @param list
* @param i
* @param j
* @param s
*/
public void setGetProperty2(IList<String> list, int i, int j, String s) {
if(i != j) {
String jString = list.get(j);
list.set(i, s);
assertEquals(jString, list.get(j));
}
}
}
package inf101.tests;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import inf101.util.IGenerator;
import inf101.util.generators.IntGenerator;
import inf101.v16.datastructures.IRange;
import inf101.v16.datastructures.Range;
public class RangeTest {
private static final int N = 10000;
@Test
public void rangeTest() {
IGenerator<Integer> lowGen = new IntGenerator(); // any integer
IGenerator<Integer> positiveGen = new IntGenerator(100000); // any positiv integer < 100000
for(int i = 0; i < N; i++) {
int low = lowGen.generate();
int high = low + positiveGen.generate();
IRange range = new Range(low, high);
rangeProperty(range, low, high);
}
}
private void rangeProperty(IRange range, int low, int high) {
int x = low;
for(int y : range) {
assertEquals(x, y);
x = x + 1;
}
assertEquals(high, x);
}
}
package inf101.tests;
import static org.junit.Assert.assertEquals;
import inf101.util.IGenerator;
import java.util.List;
public class StandardProperties {
/**
* Test that all the standard properties of equals() hold for objects
* supplied by the generator.
*
* @param gen
* A data generator
* @param n
* The number of times to run the tests
*/
public static <T> void allEqualsTests(IGenerator<T> gen, int n) {
equalsIsReflexiveTest(gen, n);
equalsIsSymmetricTest(gen, n);
equalsIsTransitiveTest(gen, n);
hashCodeEqualsTest(gen, n);
}
/**
* A generic test for the reflexivity property.
*
* Will generate n sets of objects with the generator, and check the
* property with them.
*
* @param gen
* A data generator
* @param n
* The number of times to run the test
* @see #reflexiveProperty(Object)
*/
public static <T> void equalsIsReflexiveTest(IGenerator<T> gen, int n) {
for (int i = 0; i < n; i++) {
StandardProperties.reflexiveProperty(gen.generate());
}
}
/**
* A generic test for the symmetry property.
*
* Will generate n sets of objects with the generator, and check the
* property with them.
*
* @param gen
* A data generator
* @param n
* The number of times to run the test
* @see #symmetricProperty(Object, Object)
*/
public static <T> void equalsIsSymmetricTest(IGenerator<T> gen, int n) {
for (int i = 0; i < n; i++) {
symmetricProperty(gen.generate(), gen.generate());
List<T> ss = gen.generateEquals(2);
symmetricProperty(ss.get(0), ss.get(1));
}
}
/**
* A generic test for the transitivity property.
*
* Will generate n sets of objects with the generator, and check the
* property with them.
*
* @param gen
* A data generator
* @param n
* The number of times to run the test
* @see #transitiveProperty(Object, Object, Object)
*/
public static <T> void equalsIsTransitiveTest(IGenerator<T> gen, int n) {
for (int i = 0; i < n; i++) {
transitiveProperty(gen.generate(), gen.generate(), gen.generate());
List<T> ss = gen.generateEquals(3);
transitiveProperty(ss.get(0), ss.get(1), ss.get(2));
}
}
/**
* A generic test for the hashcode property.
*
* @param gen
* A data generator
* @param n
* The number of times to run the test.
* @see #hashCodeProperty(Object, Object)
*/
public static <T> void hashCodeEqualsTest(IGenerator<T> gen, int n) {
for (int i = 0; i < n; i++) {
hashCodeProperty(gen.generate(), gen.generate());
List<T> ss = gen.generateEquals(2);
hashCodeProperty(ss.get(0), ss.get(1));
}
}
/**
* Checks the equals/hashCode property, i.e. that s1.equals(s2) implies
* s1.hashCode() == s2.hashCode().
*
* @param s1
* @param s2
*/
public static <T> void hashCodeProperty(T s1, T s2) {
if (s1.equals(s2)) {
assertEquals(s1.hashCode(), s2.hashCode());
}
}
/**
* Checks the reflexivity property for equals, i.e. that s.equals(s).
*
* @param s
*/
public static <T> void reflexiveProperty(T s) {
assertEquals(s, s);
}
/**
* Checks the symmetry property for equals, i.e. that s1.equals(s2) ==
* s2.equals(s1).
*
* @param s1
* @param s2
*/
public static <T> void symmetricProperty(T s1, T s2) {
if (s1.equals(s2)) {
assertEquals(s2, s1);
}
}
/**
* Checks the transitivity property for equals, i.e. that s1.equals(s2) and
* s2.equals(s3) implies s1.equals(s3).
*
* @param s1
* @param s2
* @param s3
*/
public static <T> void transitiveProperty(T s1, T s2, T s3) {
if (s1.equals(s2) && s2.equals(s3)) {
assertEquals(s1, s3);
}
}
}
package inf101.util;
import java.util.List;
import java.util.Random;
/**
* An interface for generators of random data values (to be used in testing).
*
* @param <T>
* The type of data generated.
*/
public interface IGenerator<T> {
/**
* Generate a random object.
*
* @return An object of type T
*/
T generate();
/**
* Generate a random object.
*
* @param r
* A random generator
* @return An object of type T
*/
T generate(Random r);
/**
* Generate a number of equal objects.
*
* @param n
* The number of objects to generate.
* @return A list of objects, with the property that for any two objects a,b
* in the collection a.equals(b).
*
*/
List<T> generateEquals(int n);
/**
* Generate a number of equal objects.
*
* @param r
* A random generator
* @param n
* The number of objects to generate.
* @return A list of objects, with the property that for any two objects a,b
* in the collection a.equals(b).
*
*/
List<T> generateEquals(Random r, int n);
}
package inf101.util.generators;
import inf101.util.IGenerator;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public abstract class AbstractGenerator<T> implements IGenerator<T> {
private static final Random commonRandom = new Random();
@Override
public T generate() {
return generate(commonRandom);
}
@Override
public List<T> generateEquals(int n) {
return generateEquals(commonRandom, n);
}
@Override
public List<T> generateEquals(Random r, int n) {
long seed = r.nextLong();
List<T> list = new ArrayList<T>();
for (int i = 0; i < n; i++) {
list.add(generate(new Random(seed)));
}
return list;
}
}
package inf101.util.generators;
import java.util.Random;
public class IntGenerator extends AbstractGenerator<Integer> {
private final long minValue;
private final long diff;
/**
* Generate a random long in the interval [0,n)
*
* @param rng
* A random generator
* @param n
* The maximum value (exclusive)
* @return A uniformly distributed random integer in the range 0 (inclusive)
* to n (exclusive)
*
* @author Adapted from JDK implementation for nextInt(n)
*/
public static long nextLong(Random rng, long n) {
if (n <= 0) {
throw new IllegalArgumentException("n must be positive");
}
long bits, val;
do {
bits = rng.nextLong() & ~Long.MIN_VALUE; // force to positive
val = bits % n;
} while (bits - val + (n - 1L) < 0L);
return val;
}
/**
* Make a generator for the full range of integers.
*/
public IntGenerator() {
this.minValue = Integer.MIN_VALUE;
long maxValue = Integer.MAX_VALUE + 1L; // generate up to and including
// Integer.MAX_VALUE
diff = maxValue - minValue;
}
/**
* Make a generator for positive numbers from 0 (inclusive) to maxValue
* (exclusive).
*
* @param maxValue
* The max value, or 0 for the full range of positive integers
*/
public IntGenerator(int maxValue) {
if (maxValue < 0) {
throw new IllegalArgumentException("maxValue must be positive or 0");
}
this.minValue = 0;
long mv = Integer.MAX_VALUE + 1L; // generate up to and including
// Integer.MAX_VALUE
if (maxValue != 0) {
mv = maxValue;
}
diff = mv - minValue;
}
/**
* Make a generator for numbers from minValue (inclusive) to maxValue
* (exclusive).
*
* @param minValue
* The minimum value
* @param maxValue
* The maximum value, minValue < maxValue
*/
public IntGenerator(int minValue, int maxValue) {
if (minValue >= maxValue) {
throw new IllegalArgumentException(
"minValue must be less than maxValue");
}
this.minValue = minValue;
diff = ((long) maxValue) - ((long) minValue);
}
@Override
public Integer generate(Random rng) {
long r = minValue + nextLong(rng, diff);
return (int) r;
}
}
package inf101.util.generators;
import inf101.util.IGenerator;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class ListGenerator<T> extends AbstractGenerator<List<T>> {
/**
* Generator for the length of the list
*/
private final IGenerator<Integer> lengthGenerator;
/**
* Generator for one element of a random grid
*/
private final IGenerator<T> elementGenerator;
public ListGenerator(IGenerator<T> elementGenerator) {
this.elementGenerator = elementGenerator;
this.lengthGenerator = new IntGenerator(1, 100