Created
June 3, 2012 15:56
-
-
Save Cherubi/2863974 to your computer and use it in GitHub Desktop.
Static Method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
import fi.helsinki.cs.tmc.edutestutils.Points; | |
import fi.helsinki.cs.tmc.edutestutils.Reflex; | |
import fi.helsinki.cs.tmc.edutestutils.Reflex.ClassRef; | |
import fi.helsinki.cs.tmc.edutestutils.Reflex.MethodRef1; | |
import org.junit.AfterClass; | |
import org.junit.Before; | |
import org.junit.Test; | |
import static org.junit.Assert.*; | |
import org.junit.BeforeClass; | |
/** | |
* | |
* @author Usagi-chan | |
*/ | |
public class TekoalynOpettaminenTest { | |
private String luokka = "OppivaTekoaly"; | |
private String mainLuokka = "Main"; | |
private String tarkistus = "54 1|19 2|5 0|36 3|76 3|25 0|10 1|38 1|81 0|48 3|61 0|98 1"; | |
public TekoalynOpettaminenTest() { | |
} | |
@Before | |
public void setUp() { | |
try { | |
Reflex.reflect(luokka).constructor().takingNoParams().invoke(); | |
} catch (Throwable t) { | |
fail("Ei löytynyt luokkaa " + luokka + ". Eihän " + luokka + "si ota parametreja."); | |
} | |
try { | |
Reflex.reflect(mainLuokka).constructor().takingNoParams().invoke(); | |
} catch (Throwable t) { | |
fail("Ei löytynyt luokkaa " + mainLuokka); | |
} | |
} | |
@Test | |
@Points("1.4.3") | |
public void opettamismetodiLoytyy() { | |
loytyykoMetodi("opettaminen", OppivaTekoaly.class, "OppivaTekoaly", OppivaTekoaly.class, "OppivaTekoaly"); | |
} | |
private void loytyykoMetodi(String metodinNimi, Class parametri, String parametrinNimi, Class parametri2, String parametrin2Nimi) { | |
ClassRef parametriluokka = Reflex.reflect("OppivaTekoaly"); | |
Reflex.MethodRef2 metodi = Reflex.reflect(mainLuokka).staticMethod(metodinNimi).returningVoid().taking(parametriluokka.getReferencedClass(), parametriluokka.getReferencedClass()); | |
if (metodi.exists()) { | |
return; | |
} | |
Reflex.MethodRef0<Object, Void> parametritonMetodi = Reflex.reflect(mainLuokka).method(metodinNimi).returningVoid().takingNoParams(); | |
if (parametritonMetodi.exists()) { | |
fail("Luokan " + mainLuokka + " metodin " + metodinNimi + " tulee ottaa parametriksi kaksi OppivaTekoalya."); | |
} | |
fail("Luokalla " + mainLuokka + " tulee olla metodi " + metodinNimi); | |
} | |
@Test | |
@Points("1.4.3") | |
public void opettaminenToimii() throws Throwable { | |
Object tekoaly1 = Reflex.reflect(luokka).constructor().takingNoParams().invoke(); | |
Object tekoaly2 = Reflex.reflect(luokka).constructor().takingNoParams().invoke(); | |
String[] args = {"kdjf", "kdfjs", "ksdj"}; | |
Object main = Reflex.reflect(mainLuokka).constructor().takingNoParams().invoke(); | |
ClassRef parametriluokka = Reflex.reflect("OppivaTekoaly"); | |
Reflex.MethodRef2 oppiminen = Reflex.reflect(mainLuokka).staticMethod("oppiminen").returningVoid().taking(parametriluokka.getReferencedClass(), parametriluokka.getReferencedClass()); | |
oppiminen.invokeOn(mainLuokka, tekoaly1, tekoaly2); | |
String[] lista = tarkistus.split("|"); | |
for (String tapaus : lista) { | |
String[] tikutJaVastaus = tapaus.split(" "); | |
int tikut = Integer.parseInt(tikutJaVastaus[0]); | |
int vastaus = Integer.parseInt(tikutJaVastaus[1]); | |
if (vastaus == 0) { | |
tarkistaEttaTasainenJakauma(tekoaly1, 1, tikut); | |
tarkistaEttaTasainenJakauma(tekoaly2, 2, tikut); | |
} | |
else { | |
tarkistaEttaYksiYleisin(tekoaly1, 1, tikut, vastaus); | |
tarkistaEttaYksiYleisin(tekoaly2, 2, tikut, vastaus); | |
} | |
} | |
} | |
private void tarkistaEttaTasainenJakauma(Object tekoaly, int tekoalynNumero, int tikut) throws Throwable { | |
MethodRef1<Object, Integer, Integer> teeValinta = Reflex.reflect(luokka).method("teeValinta").returning(int.class).taking(int.class); | |
} | |
private void tarkistaEttaYksiYleisin(Object tekoaly, int tekoalynNumero, int tikut, int vastaus) throws Throwable { | |
MethodRef1<Object, Integer, Integer> teeValinta = Reflex.reflect(luokka).method("teeValinta").returning(int.class).taking(int.class); | |
int[] maarat = new int[3]; | |
for (int i=0; i<600; i++) { | |
int pallo = teeValinta.invokeOn(tekoaly, tikut); | |
maarat[pallo-1] = maarat[pallo-1] + 1; | |
} | |
fail(""+maarat[vastaus-1]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment