Commit 91722eb2 authored by Anya Helene Bagge's avatar Anya Helene Bagge 🦆

lab 3

parents
<?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>inf101.v16.lab3</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
encoding/<project>=UTF-8
package inf101.v16.datastructures;
public interface IGrid<T> {
/**
* @return The height of the grid.
*/
int getHeight();
/**
* @return The width of the grid.
*/
int getWidth();
/**
*
* Set the contents of the cell in the given x,y location.
*
* y must be greater than or equal to 0 and less than getHeight().
* x must be greater than or equal to 0 and less than getWidth().
*
* @param x The column of the cell to change the contents of.
* @param y The row of the cell to change the contents of.
* @param element The contents the cell is to have.
*/
void set(int x, int y, T element);
/**
*
* Get the contents of the cell in the given x,y location.
*
* y must be greater than or equal to 0 and less than getHeight().
* x must be greater than or equal to 0 and less than getWidth().
*
* @param x The column of the cell to get the contents of.
* @param y The row of the cell to get contents of.
*/
T get(int x, int y);
/**
* Make a copy
*
* @return A fresh copy of the grid, with the same elements
*/
IGrid<T> copy();
}
\ No newline at end of file
package inf101.v16.datastructures;
public interface IList<T> {
/**
* Legg til et element på slutten av listen.
*
* @param s Elementet som skal legges til
*
* Etterpå vil size() øke med én, og get(size()-1) vil returnere elementet.
*/
void add(T s);
/**
* Fjern et element fra listen
*
* @param i Indeksen til elementet
* @return elementet som ble fjernet
*
* Etterpå vil alle senere indekser i listen flyttes én posisjon frem.
*/
T remove(int i);
/**
* Returner elementet på posisjon i
* @param i
* @return
*/
T get(int i);
/**
* @return True hvis listen er tom
*/
boolean isEmpty();
/**
* @return Antall elementer i listen
*/
int size();
/**
* Sett elementet på indeks i til s
*
* @param i Index
* @param s Nytt element
*
* Etterpå vil get(i) == s
*/
void set(int i, T s);
/**
* Fjern alle elementer fra listen.
*
* Etterpå vil size() == 0
*/
void clear();
}
package inf101.v16.datastructures;
/**
*
* A Grid contains a set of cells in a square 2D matrix.
*
*/
public class MyGrid<T> implements IGrid<T> {
private IList<T> cells;
private int height;
private int width;
/**
*
* Construct a grid with the given dimensions.
*
* @param width
* @param height
* @param initElement
* What the cells should initially hold (possibly null)
*/
public MyGrid(int width, int height, T initElement) {
if(width <= 0 || height <= 0)
throw new IllegalArgumentException();
this.height = height;
this.width = width;
cells = new MyList<T>(height * width);
for (int i = 0; i < height * width; ++i) {
cells.add(initElement);
}
}
@Override
public int getHeight() {
return height;
}
@Override
public int getWidth() {
return width;
}
@Override
public void set(int x, int y, T elem) {
if(x < 0 || x >= width)
throw new IndexOutOfBoundsException();
if(y < 0 || y >= height)
throw new IndexOutOfBoundsException();
cells.set(x + (y * width), elem);
}
@Override
public T get(int x, int y) {
if(x < 0 || x >= width)
throw new IndexOutOfBoundsException();
if(y < 0 || y >= height)
throw new IndexOutOfBoundsException();
return cells.get(y + (x * width));
}
@Override
public IGrid<T> copy() {
MyGrid<T> newGrid = new MyGrid<>(getWidth(), getHeight(), null);
for (int x = 0; x < width; x++)
for(int y = 0; y < height; y++)
newGrid.set(x, y, get(x, y));
return newGrid;
}
}
package inf101.v16.datastructures;
import java.util.Arrays;
public class MyList<T> implements IList<T> {
private int length;
private T[] data;
public MyList() {
length = 0;
data = (T[]) new Object[10];
}
public MyList(int initialSize) {
if(initialSize < 0)
throw new IllegalArgumentException("initialSize must be positive: " + initialSize);
length = 0;
data = (T[]) new Object[initialSize];
}
@Override
public void add(T s) {
if (length == data.length) {
data = Arrays.copyOf(data, data.length * 2);
}
data[length] = s;
length = length + 1;
}
@Override
public T remove(int i) {
T element = data[i];
for (int x = i; x < length - 1; x++) {
data[x] = data[x + 1];
}
length = length - 1;
return element;
}
@Override
public T get(int i) {
return data[i];
}
@Override
public boolean isEmpty() {
return length == 0;
}
@Override
public int size() {
return length;
}
@Override
public void set(int i, T s) {
data[i] = s;
}
@Override
public void clear() {
length = 0;
}
}
package inf101.v16.tests;
import static org.junit.Assert.*;
import inf101.v16.util.IGenerator;
import inf101.v16.util.generators.IntGenerator;
import inf101.v16.util.generators.MyGridGenerator;
import inf101.v16.util.generators.StringGenerator;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import org.junit.Test;
public class GeneratorTest {
private final Random rng = new Random();
@Test
public void testIntGenerator() {
for (int i = 0; i < 10000; i++) {
generateEqualsProperty(new IntGenerator());
generateEqualsProperty(new IntGenerator(i));
generateEqualsProperty(new IntGenerator(-i, i+1));
generateNotNullProperty(new IntGenerator());
generateNotNullProperty(new IntGenerator(i));
generateNotNullProperty(new IntGenerator(-i, i+1));
}
}
@Test
public void testStringGenerator() {
for (int i = 0; i < 1000; i++) {
generateEqualsProperty(new StringGenerator());
generateEqualsProperty(new StringGenerator(i+1));
generateNotNullProperty(new StringGenerator());
generateNotNullProperty(new StringGenerator(i+1));
}
generateEqualsProperty(new StringGenerator(10, 1000));
}
@Test
public void testMyGridGenerator() {
for (int i = 0; i < 10000; i++) {
generateEqualsProperty(new MyGridGenerator<String>(new StringGenerator()));
generateNotNullProperty(new MyGridGenerator<String>(new StringGenerator()));
}
}
/**
* Test that all elements in the collection returned by gen.generateEquals are actually equal.
*
* @param gen A generator
*/
public <T> void generateEqualsProperty(IGenerator<T> gen) {
List<T> elements = gen.generateEquals(rng, 10);
T obj = elements.get(0);
for (T o : elements)
assertEquals(obj, o);
}
/**
* Test that the generator returns non-null objects
*
* @param gen
*/
public <T> void generateNotNullProperty(IGenerator<T> gen) {
assertNotNull(gen.generate());
}
}
package inf101.v16.tests;
import static org.junit.Assert.*;
import inf101.v16.datastructures.IGrid;
import inf101.v16.util.IGenerator;
import inf101.v16.util.generators.MyGridGenerator;
import inf101.v16.util.generators.IntGenerator;
import inf101.v16.util.generators.StringGenerator;
import java.util.Random;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class GridTest {
private static final int N = 1000000;
private IGenerator<String> strGen = new StringGenerator();
private IGenerator<IGrid<String>> gridGen = new MyGridGenerator<String>(strGen);
private IGenerator<Integer> intGen = new IntGenerator(1, 100);
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
/**
* Test that get gives back the same value after set.
*/
@Test
public void setGetTest() {
IGrid<String> grid = gridGen.generate();
IGenerator<Integer> wGen = new IntGenerator(0, grid.getWidth());
IGenerator<Integer> hGen = new IntGenerator(0, grid.getHeight());
for (int i = 0; i < N; i++) {
int x = wGen.generate();
int y = hGen.generate();
String s = strGen.generate();
setGetProperty(grid, x, y, s);
}
}
/**
* get(x,y) is val after set(x, y, val)
*/
private <T> void setGetProperty(IGrid<T> grid, int x, int y, T val) {
// TODO: fyll inn
fail();
}
/**
* A set on (x1,y1) doesn't affect a get on a different (x2,y2)
*/
private <T> void setGetIndependentProperty(IGrid<T> grid, int x1,
int y1, int x2, int y2, T val) {
if (x1 != x2 && y1 != y2) {
T s = grid.get(x2, y2);
grid.set(x1, y1, val);
assertEquals(s, grid.get(x2, y2));
}
}
}
package inf101.v16.tests;
import static org.junit.Assert.*;
import inf101.v16.datastructures.IList;
import inf101.v16.datastructures.MyList;
import java.util.Random;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class MyListTest {
Random random = new Random();
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void addGetTest() {
IList<Integer> list = new MyList<>();
for(int i = 0; i < 100; i++)
list.add(i);
for(int i = 0; i < 100; i++)
assertEquals(i, (int)list.get(i));
assertEquals(100, list.size());
}
@Test
public void setGetTest() {
IList<Integer> list = new MyList<>();
for(int i = 0; i < 1000; i++)
list.add(random.nextInt());
for(int i = 0; i < 1000; i++) {
int element = random.nextInt();
list.set(i, element);
assertEquals(element, (int)list.get(i));
}
}
}
package inf101.v16.tests;
import static org.junit.Assert.*;
import inf101.v16.util.IGenerator;
import inf101.v16.util.generators.StringGenerator;
import org.junit.Test;
public class StringTest {
private final IGenerator<String> strGen = new StringGenerator();
private final int N = 100000;
@Test
public void stringTest1() {
assertEquals("foo", "FOO".toLowerCase());
}
@Test
public void stringTest2() {
for(int i = 0; i < N; i++) {
String s = strGen.generate();
assertEquals(s + s, s.concat(s));
}
}
}
package inf101.v16.util;
import java.util.Collection;
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.
*
* @param r A random generator
* @return An object of type T
*/
T generate(Random r);
/**
* 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);
/**
* 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 random object.
*
* @return An object of type T
*/
T generate();
}
package inf101.v16.util.generators;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import inf101.v16.util.IGenerator;
public class IntGenerator implements IGenerator<Integer> {
private final long minValue;
private final long diff;
private static final Random commonRandom = new Random();
/**
* 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() {
return generate(commonRandom);
}
@Override
public Integer generate(Random rng) {
long r = minValue + nextLong(rng, diff);
return (int) r;
}
@Override
public List<Integer> generateEquals(int n) {
return generateEquals(commonRandom, n);
}
@Override
public List<Integer> generateEquals(Random rng, int n) {
Integer gen = generate(rng);
List<Integer> list = new ArrayList<Integer>(n);
for (int i = 0; i < n; i++)
list.add(gen);
return list;
}
/**
* 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;
}
public static Random getRandom() {
return commonRandom;
}
}
package inf101.v16.util.generators;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import inf101.v16.util.IGenerator;
import inf101.v16.datastructures.IGrid;
import inf101.v16.datastructures.MyGrid;
public class MyGridGenerator<T> implements IGenerator<IGrid<T>> {
/**
* Generator for the width of a random grid
*/
private final IGenerator<Integer> widthGenerator;
/**
* Generator for the height of a random grid
*/
private final IGenerator<Integer> heightGenerator;
/**
* Generator for one element of a random grid
*/
private final IGenerator<T> elementGenerator;
public MyGridGenerator(IGenerator<T> elementGenerator) {
this.elementGenerator = elementGenerator;
this.widthGenerator = new IntGenerator(1,100); // bredde blir et tall fra 1 til 100
this.heightGenerator = new IntGenerator(1, 100); // høyde blir et tall fra 1 til 100
}
public MyGridGenerator(IGenerator<T> elementGenerator, int maxWidth, int maxHeight) {
if(maxWidth < 1 || maxHeight < 1)
throw new IllegalArgumentException("Width and height must be 1 or greater");
this.elementGenerator = elementGenerator;
this.widthGenerator = new IntGenerator(1, maxWidth);
this.heightGenerator = new IntGenerator(1, maxHeight);
}
public MyGridGenerator(IGenerator<T> elementGenerator, IGenerator<Integer> widthGenerator, IGenerator<Integer> heightGenerator) {
this.elementGenerator = elementGenerator;
this.widthGenerator = widthGenerator;
this.heightGenerator = heightGenerator;
}
@Override
public IGrid<T> generate(Random r) {
// TODO: fyll inn; bruk bredde/høyde-generatorer til å finne størrelsen på
// et nytt grid, opprett et MyGrid-objekt, og fyll det med elementer
// fra elementgeneratoren (T element = elementGenerator.generate(r);)
//
// Viktig: sende med argumentet 'r' når du kaller generate på subgeneratorene