package tablice;
public class Tablica_1W {
private int tab[];
private int n;
public Tablica_1W(int n)
{
this.n=n;
}
public int[] utworz_tablice()
{
if(n>0)
{
tab = new int[n];
for(int i=0;i<n;i++)
tab[i]=i;
return tab;
}
else
throw new ArrayIndexOutOfBoundsException("przekroczyles zakres tablicy");
}
public boolean modyfikuj(int index,int w)
{
if(tab==null)
utworz_tablice();
if(index>=0 && index<tab.length)
{
tab[index]=w;
return true;
}
else
return false;
}
public int pobierz(int index) throws Exception
{
if(tab==null)
utworz_tablice();
if(index>=0 && index<tab.length)
return tab[index];
else throw new Exception("bledny indeks!");
}
}
package tablice;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class testTablica_1W {
Tablica_1W testowany;
@Before
public void setUp() throws Exception {
testowany = new Tablica_1W(10);
}
@After
public void tearDown() throws Exception {
}
@Test
public void testUtworz_tablice() {
assertNotNull(testowany.utworz_tablice());
}
@Test
public void testModyfikuj() {
assertTrue(testowany.modyfikuj(6,70));
assertFalse(testowany.modyfikuj(10, 10));
}
@Test
public void testPobierz() throws Exception
{
assertEquals(2, testowany.pobierz(2));
try
{
assertEquals(20, testowany.pobierz(20));
fail("jesli jest ok nie powinienes tego zobaczyc");
}
catch(Exception e)
{
System.err.println("Wyjatki sa obslugiwane");
}
}
}