Commit 251947dd authored by Anya Helene Bagge's avatar Anya Helene Bagge 🦆

Forerelesning 11

parent 6b854c07
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<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>inf101v16f11</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>
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Main {
public static void main(String[] args) {
HashMap<Name, String> map = new HashMap<>();
map.put(new Name("papegøye"), "nøtter");
map.put(new Name("ulv"), "biff");
map.put(new Name("Kari"), "Epler");
Name santa = new Name("Julenissen");
map.put(santa, "grøt");
map.put(new Name("kanarifugler"), "salat");
map.put(new Name("kaniner"), "gulrøtter");
map.put(new Name("katt"), "fisk");
likerÅSpise(santa, map);
likerÅSpise(new Name("Kari"), map);
map.put(new Name("Kari"), "poteter");
likerÅSpise(new Name("Kari"), map);
// Hvis nøkkel-objektet endres (slik at hashCoden endres),
// vil man ikke finne den igjen
santa.setName("Santa Calculus");
likerÅSpise(santa, map);
likerÅSpise(new Name("Santa Calculus"), map);
System.out.println(santa.equals(new Name("Santa Calculus")));
// for(Name navn : map.keySet()) {
// likerÅSpise(navn, map);
// }
Set<Name> set = new HashSet<>();
set.add(santa);
set.add(new Name("Tannfeen"));
set.add(new Name("Jon Blund"));
set.add(new Name("Påskeharen"));
System.out.println("Settet har " + set.size() + " elementer");
set.add(new Name("Påskehanen"));
System.out.println("Settet har nå " + set.size() + " elementer");
santa.setName("Sinterklaas");
if(set.contains(santa))
System.out.println(santa + " er kul");
else
System.out.println(santa + " er ikke kul");
santa.setName("Santa Calculus");
if(set.contains(santa))
System.out.println(santa + " er kul");
else
System.out.println(santa + " er ikke kul");
for(Name n : set) {
System.out.println(n);
}
}
static void likerÅSpise(Name navn, Map<Name, String> map) {
System.out.println(navn + " liker å spise " + map.get(navn));
}
}
public class Name {
String name;
public Name(String n) {
name = n;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public String toString() {
return name;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Name other = (Name) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
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.
*/