import org.junit.*; import static org.junit.Assert.*; import java.util.Collection; import java.util.Iterator; import java.util.HashMap; public class P5tester { public static void main(String[] args) { org.junit.runner.JUnitCore.main("P5tester"); } @Test public void pixel_test() { Pixel pix = Pixel.WHITE; pix = Pixel.GRAY; pix = Pixel.BLACK; assertEquals("there should be three colors in the pixel enum", 3, Pixel.values().length); } Pixel[][] testphoto = { { Pixel.WHITE, Pixel.WHITE, Pixel.GRAY, Pixel.GRAY, Pixel.BLACK }, { Pixel.BLACK, Pixel.WHITE, Pixel.WHITE, Pixel.GRAY, Pixel.GRAY }, { Pixel.BLACK, Pixel.BLACK, Pixel.WHITE, Pixel.WHITE, Pixel.GRAY }, { Pixel.GRAY, Pixel.BLACK, Pixel.BLACK, Pixel.WHITE, Pixel.WHITE }, { Pixel.GRAY, Pixel.GRAY, Pixel.BLACK, Pixel.BLACK, Pixel.WHITE } }; @Test public void photo_test() throws Exception { Photo p = new PhotoProcessor().getPhoto("testphoto.txt"); assertEquals("wrong width", 5, p.getWidth()); assertEquals("wrong height", 5, p.getHeight()); for (int x = 0; x < 5; x++) for (int y = 0; y < 5; y++) assertEquals("bad ("+x+","+y+") pixel", testphoto[y][x], p.getPixel(x,y)); } String[] fgnames = {"p1.txt", "p2.txt", "p3.txt"}; String[] bgnames = {"johnson_center.txt", "washington_monument.txt", "fair_oaks_mall.txt"}; @Test public void fg_loader_test() throws Exception { int i; PhotoProcessor pp = new PhotoProcessor(); pp.loadFGManifest("fgphotos.txt"); Collection fg = pp.getFGPhotos(); assertEquals("wrong number of fg photos", 3, fg.size()); Iterator pi = fg.iterator(); while (pi.hasNext()) { Photo p = pi.next(); String name = p.getName(); for (i=0; i < fgnames.length; i++) if (fgnames[i].equals(name)) break; assertNotEquals(fgnames.length, i); } } @Test public void bg_loader_test() throws Exception { int i; PhotoProcessor pp = new PhotoProcessor(); pp.loadBGManifest("bgplaces.txt"); Collection bg = pp.getBGPhotos(); assertEquals("wrong number of bg photos", 3, bg.size()); Iterator pi = bg.iterator(); while (pi.hasNext()) { Photo p = pi.next(); String name = p.getName(); for (i=0; i < bgnames.length; i++) if (bgnames[i].equals(name)) break; assertNotEquals(bgnames.length, i); } } double[][] scores = { { 0.818115234375, 0.8612060546875, 0.992919921875 }, { 0.870849609375, 0.8375244140625, 0.81640625 }, { 0.682373046875, 0.7965087890625, 0.674560546875 } }; @Test public void matcher_test() throws Exception { PhotoProcessor pp = new PhotoProcessor(); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { String fgname = fgnames[i]; Photo fg = pp.getPhoto(fgname); String bgname = bgnames[j]; Photo bg = pp.getPhoto(bgname); double score = scores[i][j]; double actScore = pp.getMatch(fg, bg); assertEquals("score mismatch " + fgname + "/" + bgname, score, actScore, 0.0001); } } } final double bestScore = 2.6602783203125; @Test public void locator_test() throws Exception { PhotoProcessor pp = new PhotoProcessor(); pp.loadBGManifest("bgplaces.txt"); pp.loadFGManifest("fgphotos.txt"); HashMap hmap = new HashMap(); double actScore = pp.getBestMatch(hmap); assertEquals("wrong best score", bestScore, actScore, 0.0001); assertEquals("wrong number of elements in matching", 3, hmap.size()); //{p3.txt=washington_monument.txt, p2.txt=johnson_center.txt, p1.txt=fair_oaks_mall.txt} assertEquals("bad match", bgnames[2], hmap.get(fgnames[0])); assertEquals("bad match", bgnames[0], hmap.get(fgnames[1])); assertEquals("bad match", bgnames[1], hmap.get(fgnames[2])); } }