23
23
*/
24
24
package dev.testify.core.processor
25
25
26
+ import android.graphics.Bitmap
26
27
import kotlinx.coroutines.DelicateCoroutinesApi
27
28
import kotlinx.coroutines.Dispatchers
28
29
import kotlinx.coroutines.ExperimentalCoroutinesApi
@@ -48,11 +49,15 @@ class ParallelPixelProcessorTest {
48
49
}
49
50
}
50
51
51
- fun setUp (maxNumberOfChunkThreads : Int? = null): ParallelPixelProcessor {
52
+ private fun setUp (
53
+ maxNumberOfChunkThreads : Int? = null,
54
+ baseline : Bitmap = mockBitmap(),
55
+ current : Bitmap = mockBitmap()
56
+ ): ParallelPixelProcessor {
52
57
return ParallelPixelProcessor
53
58
.create(forceSingleThreadedExecution(maxNumberOfChunkThreads))
54
- .baseline(mockBitmap() )
55
- .current(mockBitmap() )
59
+ .baseline(baseline )
60
+ .current(current )
56
61
}
57
62
58
63
@After
@@ -62,45 +67,45 @@ class ParallelPixelProcessorTest {
62
67
}
63
68
64
69
@Test
65
- fun default () {
70
+ fun `WHEN bitmap is processed THEN analyze every pixel` () {
66
71
val pixelProcessor = setUp(maxNumberOfChunkThreads = 1 )
67
72
68
- val index = AtomicInteger (0 )
73
+ val analyzed = AtomicInteger (0 )
69
74
pixelProcessor.analyze { _, _, _ ->
70
- index .incrementAndGet()
75
+ analyzed .incrementAndGet()
71
76
true
72
77
}
73
- assertEquals(DEFAULT_BITMAP_WIDTH * DEFAULT_BITMAP_HEIGHT , index .get())
78
+ assertEquals(DEFAULT_BITMAP_WIDTH * DEFAULT_BITMAP_HEIGHT , analyzed .get())
74
79
}
75
80
76
81
@Test
77
- fun twoCores () {
82
+ fun `WHEN processor has two threads THEN analyze every pixel` () {
78
83
val pixelProcessor = setUp(maxNumberOfChunkThreads = 2 )
79
84
80
- val index = AtomicInteger (0 )
85
+ val analyzed = AtomicInteger (0 )
81
86
pixelProcessor.analyze { _, _, _ ->
82
- index .incrementAndGet()
87
+ analyzed .incrementAndGet()
83
88
true
84
89
}
85
90
86
- assertEquals(DEFAULT_BITMAP_WIDTH * DEFAULT_BITMAP_HEIGHT , index .get())
91
+ assertEquals(DEFAULT_BITMAP_WIDTH * DEFAULT_BITMAP_HEIGHT , analyzed .get())
87
92
}
88
93
89
94
@Test
90
- fun oddNumberOfCores () {
95
+ fun `WHEN there are an odd number of threads THEN analyze every pixel` () {
91
96
val pixelProcessor = setUp(maxNumberOfChunkThreads = 7 )
92
97
93
- val index = AtomicInteger (0 )
98
+ val analyzed = AtomicInteger (0 )
94
99
pixelProcessor.analyze { _, _, _ ->
95
- index .incrementAndGet()
100
+ analyzed .incrementAndGet()
96
101
true
97
102
}
98
103
99
- assertEquals(DEFAULT_BITMAP_WIDTH * DEFAULT_BITMAP_HEIGHT , index .get())
104
+ assertEquals(DEFAULT_BITMAP_WIDTH * DEFAULT_BITMAP_HEIGHT , analyzed .get())
100
105
}
101
106
102
107
@Test
103
- fun oddNumberOfPixels () {
108
+ fun `WHEN there are an odd number of pixels THEN analyze every pixel` () {
104
109
val pixelProcessor = ParallelPixelProcessor
105
110
.create(ParallelProcessorConfiguration (requestedNumberOfChunkThreads = 2 ))
106
111
.baseline(mockBitmap(3 , 3 ))
@@ -112,28 +117,43 @@ class ParallelPixelProcessorTest {
112
117
0 to 2 , 1 to 2 , 2 to 2
113
118
)
114
119
115
- val index = AtomicInteger (0 )
120
+ val analyzed = AtomicInteger (0 )
116
121
pixelProcessor.analyze { _, _, (x, y) ->
117
122
assertTrue(expected.remove(x to y))
118
- index .incrementAndGet()
123
+ analyzed .incrementAndGet()
119
124
true
120
125
}
121
- assertEquals(9 , index .get())
126
+ assertEquals(9 , analyzed .get())
122
127
assertTrue(expected.isEmpty())
123
128
}
124
129
130
+ /* *
131
+ * Assert that the position at [index] matches the expected [position]
132
+ */
125
133
private fun ParallelPixelProcessor.assertPosition (index : Int , position : Pair <Int , Int >) {
126
134
val (x, y) = this .getPosition(index, DEFAULT_BITMAP_WIDTH )
127
135
assertEquals(position, x to y)
128
136
}
129
137
130
138
@Test
131
- fun multicoreChunks () {
139
+ fun `WHEN using multiple threads THEN the positions map correctly` () {
132
140
setUp(maxNumberOfChunkThreads = 2 ).run {
133
141
assertPosition(7 , 7 to 0 )
134
142
assertPosition(500 , 500 to 0 )
135
143
assertPosition(1500 , 420 to 1 )
136
144
assertPosition(2200 , 40 to 2 )
137
145
}
138
146
}
147
+
148
+ @Test
149
+ fun `WHEN a single pixel is different THEN fail early AND do not analyze every pixel` () {
150
+ val pixelProcessor = setUp(maxNumberOfChunkThreads = 1 )
151
+
152
+ val analyzed = AtomicInteger (0 )
153
+ pixelProcessor.analyze { _, _, (x, y) ->
154
+ analyzed.incrementAndGet()
155
+ (x != 1 ) || (y != 0 )
156
+ }
157
+ assertEquals(2 , analyzed.get())
158
+ }
139
159
}
0 commit comments