Advertisement
jules0707

BlurSuite

Feb 23rd, 2017
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 7.21 KB | None | 0 0
  1. package scalashop
  2.  
  3. import java.util.concurrent._
  4. import scala.collection._
  5. import org.scalatest.FunSuite
  6. import org.junit.runner.RunWith
  7. import org.scalatest.junit.JUnitRunner
  8. import common._
  9. import org.scalatest._
  10.  
  11. @RunWith(classOf[JUnitRunner])
  12. class BlurSuite extends FunSuite {
  13.  
  14.   test("boxBlurKernel should correctly handle radius 0") {
  15.     val src = new Img(5, 5)
  16.  
  17.     for (x <- 0 until 5; y <- 0 until 5)
  18.       src(x, y) = rgba(x, y, x + y, math.abs(x - y))
  19.  
  20.     for (x <- 0 until 5; y <- 0 until 5)
  21.       assert(boxBlurKernel(src, x, y, 0) === rgba(x, y, x + y, math.abs(x - y)),
  22.         "boxBlurKernel(_,_,0) should be identity.")
  23.   }
  24.  
  25.   test("boxBlurKernel should return the correct value on an interior pixel " +
  26.     "of a 3x4 image with radius 1") {
  27.     val src = new Img(3, 4)
  28.     src(0, 0) = 0; src(1, 0) = 1; src(2, 0) = 2
  29.     src(0, 1) = 3; src(1, 1) = 4; src(2, 1) = 5
  30.     src(0, 2) = 6; src(1, 2) = 7; src(2, 2) = 8
  31.     src(0, 3) = 50; src(1, 3) = 11; src(2, 3) = 16
  32.  
  33.     assert(boxBlurKernel(src, 1, 2, 1) === 12,
  34.       s"(boxBlurKernel(1, 2, 1) should be 12, " +
  35.         s"but it's ${boxBlurKernel(src, 1, 2, 1)})")
  36.   }
  37.  
  38.  
  39.    test("boxBlurKernel should return the correct value on an interior pixel " +
  40.     "of a 3x3 image with radius 1") {
  41.     val src = new Img(3, 4)
  42.     src(0, 0) = 0; src(1, 0) = 1; src(2, 0) = 2
  43.     src(0, 1) = 3; src(1, 1) = 4; src(2, 1) = 5
  44.     src(0, 2) = 6; src(1, 2) = 7; src(2, 2) = 8
  45.  
  46.     assert(boxBlurKernel(src, 1, 1, 1) === 4,
  47.       s"(boxBlurKernel(1, 1, 1) should be 4, " +
  48.         s"but it's ${boxBlurKernel(src, 1, 1, 1)})")
  49.   }
  50.  
  51.  
  52.  
  53.  
  54.  
  55.   test("HorizontalBoxBlur.blur with radius 1 should correctly blur the entire 3x3 image") {
  56.     val w = 3
  57.     val h = 3
  58.     val src = new Img(w, h)
  59.     val dst = new Img(w, h)
  60.    
  61.     src(0, 0) = 0; src(1, 0) = 1; src(2, 0) = 2
  62.     src(0, 1) = 3; src(1, 1) = 4; src(2, 1) = 5
  63.     src(0, 2) = 6; src(1, 2) = 7; src(2, 2) = 8
  64.  
  65.     HorizontalBoxBlur.blur(src, dst, 0, 2, 1)
  66.  
  67.     def check(x: Int, y: Int, expected: Int) =
  68.       assert(dst(x, y) == expected,
  69.         s"(destination($x, $y) should be $expected)")
  70.  
  71.     check(0, 0, 2)
  72.     check(1, 0, 2)
  73.     check(2, 0, 3)
  74.    
  75.     check(0, 1, 3)
  76.     check(1, 1, 4)
  77.     check(2, 1, 4)
  78.    
  79.     check(0, 2, 0)
  80.     check(1, 2, 0)
  81.     check(2, 2, 0)
  82.   }
  83.  
  84.   test("VerticalBoxBlur.blur with radius 1 and 1 task should correctly blur the entire 3x3 image") {
  85.     val w = 3
  86.     val h = 3
  87.     val src = new Img(w, h)
  88.     val dst = new Img(w, h)
  89.     src(0, 0) = 0; src(1, 0) = 1; src(2, 0) = 2
  90.     src(0, 1) = 3; src(1, 1) = 4; src(2, 1) = 5
  91.     src(0, 2) = 6; src(1, 2) = 7; src(2, 2) = 8
  92.  
  93.     VerticalBoxBlur.blur(src, dst, 0, 3, 1)
  94.  
  95.     def checkBox(x: Int, y: Int, expected: Int) =
  96.       assert(dst(x, y) == expected,
  97.         s"(destination($x, $y) should be $expected)")
  98.  
  99.     checkBox(0, 0, 2)
  100.     checkBox(1, 0, 2)
  101.     checkBox(2, 0, 3)
  102.  
  103.     checkBox(0, 1, 3)
  104.     checkBox(1, 1, 4)
  105.     checkBox(2, 1, 4)
  106.  
  107.     checkBox(0, 2, 5)
  108.     checkBox(1, 2, 5)
  109.     checkBox(2, 2, 6)
  110.  
  111.   }
  112.  
  113.   test("VerticalBoxBlur.blur with radius 1 should correctly blur all the pixels of the entire 7x2 image") {
  114.     val w = 7
  115.     val h = 2
  116.     val r = 1
  117.     val src = new Img(w, h)
  118.     val dst = new Img(w, h)
  119.     src(0, 0) = 0; src(1, 0) = 1; src(2, 0) = 2; src(3, 0) = 3; src(4, 0) = 4; src(5, 0) = 5; src(6, 0) = 6
  120.     src(0, 1) = 7; src(1, 1) = 8; src(2, 1) = 9; src(3, 1) = 10; src(4, 1) = 11; src(5, 1) = 12; src(6, 1) = 13;
  121.  
  122.     def checkBox(x: Int, y: Int, expected: Int) =
  123.       assert(scalashop.boxBlurKernel(src, x, y, r) == expected,
  124.         s"(destination($x, $y) should be $expected)")
  125.  
  126.     checkBox(0, 0, 4)
  127.     checkBox(1, 0, 4)
  128.     checkBox(2, 0, 5)
  129.     checkBox(3, 0, 6)
  130.     checkBox(4, 0, 7)
  131.     checkBox(5, 0, 8)
  132.     checkBox(6, 0, 9)
  133.  
  134.     checkBox(0, 1, 4)
  135.     checkBox(1, 1, 4)
  136.     checkBox(2, 1, 5)
  137.     checkBox(3, 1, 6)
  138.     checkBox(4, 1, 7)
  139.     checkBox(5, 1, 8)
  140.     checkBox(6, 1, 9)
  141.  
  142.   }
  143.  
  144.   test("HorizontalBoxBlur.blur with radius 1 should correctly blur the entire 2x4 image") {
  145.     val w = 2
  146.     val h = 4
  147.     val r = 1
  148.     val src = new Img(w, h)
  149.     val dst = new Img(w, h)
  150.     val from = 0
  151.  
  152.     src(0, 0) = 0; src(1, 0) = 1; src(0, 1) = 2; src(1, 1) = 3
  153.     src(0, 2) = 4; src(1, 2) = 5; src(0, 3) = 6; src(1, 3) = 7
  154.  
  155.     HorizontalBoxBlur.blur(src, dst, from, h, r)
  156.  
  157.     def check(x: Int, y: Int, expected: Int) =
  158.       assert(dst(x, y) == expected,
  159.         s"(destination($x, $y) should be $expected)")
  160.  
  161.     check(0, 0, 1);
  162.     check(1, 0, 1);
  163.     check(0, 1, 2);
  164.     check(1, 1, 2);
  165.     check(0, 2, 4);
  166.     check(1, 2, 4);
  167.     check(0, 3, 5);
  168.     check(1, 3, 5)
  169.  
  170.   }
  171.  
  172.   test("HorinzontalBoxBlur.parBlur with radius 1 and task 1 should correctly blur the entire 3*3 image") {
  173.     val w = 3
  174.     val h = 3
  175.     val r = 1
  176.     val n = 1
  177.     val src = new Img(w, h)
  178.     val dst = new Img(w, h)
  179.     val from = 0
  180.  
  181.     src(0, 0) = 0; src(1, 0) = 1; src(2, 0) = 2;
  182.     src(0, 1) = 3; src(1, 1) = 4; src(2, 1) = 5;
  183.     src(0, 2) = 6; src(1, 2) = 7; src(2, 2) = 8;
  184.  
  185.     HorizontalBoxBlur.parBlur(src, dst, n, r)
  186.     def check(x: Int, y: Int, expected: Int) =
  187.       assert(dst(x, y) == expected,
  188.         s"(destination($x, $y) should be $expected)")
  189.  
  190.     check(0, 0, 2);
  191.     check(1, 0, 2);
  192.     check(0, 1, 3);
  193.  
  194.     check(0, 1, 3);
  195.     check(0, 2, 5);
  196.     check(1, 2, 5);
  197.  
  198.     check(0, 2, 5);
  199.     check(1, 2, 5);
  200.     check(2, 2, 6)
  201.  
  202.   }
  203.  
  204.   test("VerticalBoxBlur.blur with radius 2 should correctly blur the entire " +
  205.     "4x3 image") {
  206.     val w = 4
  207.     val h = 3
  208.     val src = new Img(w, h)
  209.     val dst = new Img(w, h)
  210.     src(0, 0) = 0; src(1, 0) = 1; src(2, 0) = 2; src(3, 0) = 9
  211.     src(0, 1) = 3; src(1, 1) = 4; src(2, 1) = 5; src(3, 1) = 10
  212.     src(0, 2) = 6; src(1, 2) = 7; src(2, 2) = 8; src(3, 2) = 11
  213.  
  214.     VerticalBoxBlur.blur(src, dst, 0, 4, 2)
  215.  
  216.     def check(x: Int, y: Int, expected: Int) =
  217.       assert(dst(x, y) == expected,
  218.         s"(destination($x, $y) should be $expected)")
  219.  
  220.     check(0, 0, 4)
  221.     check(1, 0, 5)
  222.     check(2, 0, 5)
  223.     check(3, 0, 6)
  224.     check(0, 1, 4)
  225.     check(1, 1, 5)
  226.     check(2, 1, 5)
  227.     check(3, 1, 6)
  228.     check(0, 2, 4)
  229.     check(1, 2, 5)
  230.     check(2, 2, 5)
  231.     check(3, 2, 6)
  232.   }
  233.  
  234.   test("VerticalBoxBlur.parBlur with radius 1 and tasks 3 should correctly blur the entire " +
  235.     "7x2 image") {
  236.     val w = 7
  237.     val h = 2
  238.     val n = 7
  239.     val r = 1
  240.     val src = new Img(w, h)
  241.     val dst = new Img(w, h)
  242.     src(0, 0) = 0; src(1, 0) = 1; src(2, 0) = 2; src(3, 0) = 3; src(4, 0) = 4; src(5, 0) = 5; src(6, 0) = 6
  243.     src(0, 1) = 7; src(1, 1) = 8; src(2, 1) = 9; src(3, 1) = 10; src(4, 1) = 11; src(5, 1) = 12; src(6, 1) = 13;
  244.  
  245.     VerticalBoxBlur.parBlur(src, dst, n, r)
  246.  
  247.     def checkBox(x: Int, y: Int, expected: Int) =
  248.       assert(dst(x, y) == expected,
  249.         s"(destination($x, $y) should be $expected)")
  250.  
  251.     checkBox(0, 0, 4)
  252.     checkBox(1, 0, 4)
  253.     checkBox(2, 0, 5)
  254.     checkBox(3, 0, 6)
  255.     checkBox(4, 0, 7)
  256.     checkBox(5, 0, 8)
  257.     checkBox(6, 0, 9)
  258.  
  259.     checkBox(0, 1, 4)
  260.     checkBox(1, 1, 4)
  261.     checkBox(2, 1, 5)
  262.     checkBox(3, 1, 6)
  263.     checkBox(4, 1, 7)
  264.     checkBox(5, 1, 8)
  265.     checkBox(6, 1, 9)
  266.   }
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement