diff options
Diffstat (limited to 'src/test/java')
-rw-r--r-- | src/test/java/Tests.java | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/src/test/java/Tests.java b/src/test/java/Tests.java new file mode 100644 index 0000000..cbb154f --- /dev/null +++ b/src/test/java/Tests.java | |||
@@ -0,0 +1,169 @@ | |||
1 | import org.junit.Assert; | ||
2 | import org.junit.Test; | ||
3 | import org.junit.internal.InexactComparisonCriteria; | ||
4 | |||
5 | public class Tests { | ||
6 | |||
7 | // Tolerance used to compare two floating point numbers | ||
8 | private static final float EPSILON = 0.0001f; | ||
9 | |||
10 | // Check whether value is equal to expected integer | ||
11 | private void assertEquals(int expected, int actual) { | ||
12 | Assert.assertEquals(expected, actual); | ||
13 | } | ||
14 | |||
15 | // Check whether value is approximately equal to expected floating point number | ||
16 | private void assertEquals(float expected, float actual) { | ||
17 | Assert.assertEquals(expected, actual, EPSILON); | ||
18 | } | ||
19 | |||
20 | // Check whether integer array has correct size and values | ||
21 | private void assertEquals(int[] expected, int[] actual) { | ||
22 | Assert.assertArrayEquals(expected, actual); | ||
23 | } | ||
24 | |||
25 | // Check whether integer two-dimensional array has correct size and values | ||
26 | private void assertEquals(int[][] expected, int[][] actual) { | ||
27 | Assert.assertArrayEquals(expected, actual); | ||
28 | } | ||
29 | |||
30 | // Check whether floating point two-dimensional array has correct size and similar values | ||
31 | private void assertEquals(float[][] expected, float[][] actual) { | ||
32 | new InexactComparisonCriteria(EPSILON).arrayEquals(null, expected, actual); | ||
33 | } | ||
34 | |||
35 | // Test based on example in section 3.1 (pixel coding) | ||
36 | @Test | ||
37 | public void testColorSinglePixel() { | ||
38 | |||
39 | // Check RGB components and gray level of this bluish color | ||
40 | int color = 0b00100000_11000000_11111111; | ||
41 | assertEquals(0.1254902f, Color.getRed(color)); | ||
42 | assertEquals(0.7529412f, Color.getGreen(color)); | ||
43 | assertEquals(1.0f, Color.getBlue(color)); | ||
44 | assertEquals(0.6261438f, Color.getGray(color)); | ||
45 | |||
46 | // Encode some colors | ||
47 | assertEquals(0x0000ff, Color.getRGB(0.0f, 0.0f, 1.0f)); | ||
48 | assertEquals(0x7f7f7f, Color.getRGB(0.5f)); | ||
49 | assertEquals(0x0000ff, Color.getRGB(-0.5f, 0, 2)); | ||
50 | assertEquals(0x000000, Color.getRGB(-1.0f)); | ||
51 | |||
52 | } | ||
53 | |||
54 | // Test based on example in section 3.2 (image coding) | ||
55 | @Test | ||
56 | public void testColorWholeImage() { | ||
57 | |||
58 | // A 2x2 image | ||
59 | int[][] image = { | ||
60 | {0x20c0ff, 0x123456}, | ||
61 | {0xffffff, 0x000000} | ||
62 | }; | ||
63 | |||
64 | // Convert to grayscale | ||
65 | float[][] gray = { | ||
66 | {0.62614375f, 0.20392157f}, | ||
67 | {1.0f, 0.0f} | ||
68 | }; | ||
69 | assertEquals(gray, Color.toGray(image)); | ||
70 | |||
71 | // Convert back to integer representation | ||
72 | int[][] back = { | ||
73 | {0x9f9f9f, 0x343434}, | ||
74 | {0xffffff, 0x000000} | ||
75 | }; | ||
76 | assertEquals(back, Color.toRGB(gray)); | ||
77 | |||
78 | } | ||
79 | |||
80 | // Test based on example in section 4.1 (image filtering) | ||
81 | @Test | ||
82 | public void testFilterConvolution() { | ||
83 | |||
84 | // A 2x2 grayscale image | ||
85 | float[][] gray = { | ||
86 | {0.5f, 1.0f}, | ||
87 | {0.2f, 0.0f} | ||
88 | }; | ||
89 | |||
90 | // Access some pixels | ||
91 | assertEquals(0.5f, Filter.at(gray, 0, 0)); | ||
92 | assertEquals(0.2f, Filter.at(gray, 1, 0)); | ||
93 | assertEquals(0.2f, Filter.at(gray, 2, -1)); | ||
94 | |||
95 | // Apply smoothing and Sobel | ||
96 | assertEquals(new float[][]{ | ||
97 | {0.49f, 0.62f}, | ||
98 | {0.3f, 0.29f} | ||
99 | }, Filter.smooth(gray)); | ||
100 | assertEquals(new float[][]{ | ||
101 | {1.3f, 1.3f}, | ||
102 | {-0.1f, -0.1f} | ||
103 | }, Filter.sobelX(gray)); | ||
104 | assertEquals(new float[][]{ | ||
105 | {-1.9f, -3.3f}, | ||
106 | {-1.9f, -3.3f} | ||
107 | }, Filter.sobelY(gray)); | ||
108 | assertEquals(new float[][]{ | ||
109 | {2.302173f, 3.5468295f}, | ||
110 | {1.9026297f, 3.3015149f} | ||
111 | }, Filter.sobel(gray)); | ||
112 | |||
113 | } | ||
114 | |||
115 | // Test based on example in section 5.1 (shortest path) | ||
116 | @Test | ||
117 | public void testSeamPath() { | ||
118 | |||
119 | // Simple graph stored as an adjacency list | ||
120 | int[][] successors = new int[][]{ | ||
121 | /* 0 -> */ {1, 3}, | ||
122 | /* 1 -> */ {2}, | ||
123 | /* 2 -> */ {}, | ||
124 | /* 3 -> */ {4}, | ||
125 | /* 4 -> */ {1, 5}, | ||
126 | /* 5 -> */ {2} | ||
127 | }; | ||
128 | float[] costs = new float[]{ | ||
129 | /* 0 : */ 1.0f, | ||
130 | /* 1 : */ 9.0f, | ||
131 | /* 2 : */ 2.0f, | ||
132 | /* 3 : */ 1.0f, | ||
133 | /* 4 : */ 3.0f, | ||
134 | /* 5 : */ 1.0f | ||
135 | }; | ||
136 | |||
137 | // Compute and check shortest path | ||
138 | int[] vertices = Seam.path(successors, costs, 0, 2); | ||
139 | assertEquals(new int[]{0, 3, 4, 5, 2}, vertices); | ||
140 | |||
141 | } | ||
142 | |||
143 | // Test based on example in section 5.2 (resizing) | ||
144 | @Test | ||
145 | public void testSeamResize() { | ||
146 | |||
147 | // A 3x3 image | ||
148 | int[][] image = { | ||
149 | {0x888888, 0x666666, 0xcccccc}, | ||
150 | {0x000000, 0x111111, 0x222222}, | ||
151 | {0xbbbbbb, 0xffffff, 0x666666} | ||
152 | }; | ||
153 | float[][] gray = Color.toGray(image); | ||
154 | |||
155 | // Find best seam, using gray level as energy | ||
156 | int[] seam = Seam.find(gray); | ||
157 | assertEquals(new int[]{1, 1, 2}, seam); | ||
158 | |||
159 | // Remove seam | ||
160 | int[][] resized = Seam.shrink(image, seam); | ||
161 | assertEquals(new int[][]{ | ||
162 | {0x888888, 0xcccccc}, | ||
163 | {0x000000, 0x222222}, | ||
164 | {0xbbbbbb, 0xffffff} | ||
165 | }, resized); | ||
166 | |||
167 | } | ||
168 | |||
169 | } | ||