You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

141 lines
5.3 KiB

package com.sothr.imagetools.hash
import java.io.File
import java.util.NoSuchElementException
import javax.imageio.ImageIO
import com.sothr.imagetools.hash.HashService.getHash
import com.sothr.imagetools.hash.`type`.{AHash, DHash, PHash}
import com.sothr.imagetools.hash.util.ImageUtil
import scala.collection.mutable
/**
* Test the Hash service and make sure it is consistent
*
* Created by dev on 1/23/14.
*/
class HashServiceTest extends HashServiceBaseTest {
def imageHashTestCase(filePath: String): ImageHash = {
HashService.getImageHashes(ahashSetting, dhashSetting, phashSetting, filePath)
}
test("ImageHash Of Large, Medium, And Small Sample 1 Must Be Similar") {
val largeHash = imageHashTestCase(LargeSampleImage1)
val mediumHash = imageHashTestCase(MediumSampleImage1)
val smallHash = imageHashTestCase(SmallSampleImage1)
assert(HashService.areImageHashesSimilar(ahashSetting, dhashSetting, phashSetting, largeHash, mediumHash))
assert(HashService.areImageHashesSimilar(ahashSetting, dhashSetting, phashSetting, largeHash, smallHash))
assert(HashService.areImageHashesSimilar(ahashSetting, dhashSetting, phashSetting, mediumHash, smallHash))
}
test("Calculate ImageHash Large Sample Image 1") {
debug("Starting 'Calculate ImageHash Large Sample Image 1' test")
val hash = HashService.getImageHashes(ahashSetting, dhashSetting, phashSetting, LargeSampleImage1)
debug(s"Testing that ${hash.hashCode()} = -812844858")
assert(hash.hashCode == -812844858)
}
test("Calculate ImageHash Medium Sample Image 1") {
debug("Starting 'Calculate ImageHash Medium Sample Image 1' test")
val hash = HashService.getImageHashes(ahashSetting, dhashSetting, phashSetting, MediumSampleImage1)
debug(s"Testing that ${hash.hashCode()} = -812836666")
assert(hash.hashCode == -812836666)
}
test("Calculate ImageHash Small Sample Image 1") {
debug("Starting 'Calculate ImageHash Small Sample Image 1' test")
val hash = HashService.getImageHashes(ahashSetting, dhashSetting, phashSetting, SmallSampleImage1)
debug(s"Testing that ${hash.hashCode()} = -812840762")
assert(hash.hashCode == -812840762)
}
test("Benchmark getImageHashes") {
info("Benchmarking getImageHashes")
info("getImageHashes Large Image 3684x2736")
val time = new mutable.MutableList[Long]()
for (runNum <- 0 until benchmarkRuns) {
time += getTime {
imageHashTestCase(LargeSampleImage1)
}
}
val largeMean = getMean(time.toArray[Long])
info(s"The mean time of ${time.size} tests for large was: $largeMean ms")
time.clear()
info("getImageHashes Medium Image 1824x1368")
for (runNum <- 0 until benchmarkRuns) {
time += getTime {
imageHashTestCase(MediumSampleImage1)
}
}
val mediumMean = getMean(time.toArray[Long])
info(s"The mean time of ${time.size} tests for medium was: $mediumMean ms")
time.clear()
info("getImageHashes Small Image 912x684")
for (runNum <- 0 until benchmarkRuns) {
time += getTime {
imageHashTestCase(SmallSampleImage1)
}
}
val smallMean = getMean(time.toArray[Long])
info(s"The mean time of ${time.size} tests for small was: $smallMean ms")
time.clear()
assert(true)
}
test("GetPrecisionSet - Empty Precision Set") {
val emptyPrecisionSet = HashService.getPrecisionSet(
new HashSetting("Test1", false, 8, 0, 0),
new HashSetting("Test2", false, 16, 0, 0),
new HashSetting("Test3", false, 32, 0, 0))
assert(emptyPrecisionSet.isEmpty)
}
test("GetPrecisionSet - Overlapping Precision Set") {
val precisionSet = HashService.getPrecisionSet(
new HashSetting("Test1", true, 8, 0, 0),
new HashSetting("Test2", true, 8, 0, 0),
new HashSetting("Test3", true, 16, 0, 0))
assert(precisionSet.nonEmpty)
assert(2 == precisionSet.size)
}
test("GetPrecisionMap - Empty Precision Set") {
val emptyPrecisionSet = HashService.getPrecisionSet(
new HashSetting("Test1", false, 8, 0, 0),
new HashSetting("Test2", false, 16, 0, 0),
new HashSetting("Test3", false, 32, 0, 0))
val grayImage = ImageUtil.convertToGray(ImageIO.read(new File(SmallSampleImage1)))
val emptyPrecisionMap = HashService.getPrecisionMap(emptyPrecisionSet, grayImage)
assert(emptyPrecisionMap.isEmpty)
}
test("GetPrecisionMap - Overlapping Precision Set") {
val precisionSet = HashService.getPrecisionSet(
new HashSetting("Test1", true, 8, 0, 0),
new HashSetting("Test2", true, 8, 0, 0),
new HashSetting("Test3", true, 16, 0, 0))
val grayImage = ImageUtil.convertToGray(ImageIO.read(new File(SmallSampleImage1)))
val precisionMap = HashService.getPrecisionMap(precisionSet, grayImage)
assert(precisionMap.nonEmpty)
assert(precisionMap.size == 2)
}
test("getHash - Empty Precision Set") {
val emptyPrecisionSet = HashService.getPrecisionSet(
new HashSetting("Test1", false, 8, 0, 0),
new HashSetting("Test2", false, 16, 0, 0),
new HashSetting("Test3", false, 32, 0, 0))
val grayImage = ImageUtil.convertToGray(ImageIO.read(new File(SmallSampleImage1)))
val emptyPrecisionMap = HashService.getPrecisionMap(emptyPrecisionSet, grayImage)
assertThrows[NoSuchElementException] {
val ahash: Long = getHash(ahashSetting, emptyPrecisionMap(ahashSetting.precision), AHash.getHash)
val dhash: Long = getHash(dhashSetting, emptyPrecisionMap(dhashSetting.precision), DHash.getHash)
val phash: Long = getHash(phashSetting, emptyPrecisionMap(phashSetting.precision), PHash.getHash)
}
}
}