SHARE
TWEET

Neighbors - Unit Test

Moortiii Sep 14th, 2019 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This is part of an assignment to solve a puzzle similar to Futoshiki, it is called neighbors
  2. // and an explanation of the game can be found at https://www.brainbashers.com/neighbourshelp.asp
  3.  
  4. /* Puzzle.object */
  5. object Puzzle {
  6.   // The puzzle object retrieves the size directly from the puzzle it is currently
  7.   // set to parse. The size of the board can change between puzzles and this becomes
  8.   // an issue when trying to write unit tests for the solver.
  9.   private val lines = Utilities.getInput("src/puzzles/puzzle")
  10.  
  11.   val puzzle:List[String] = lines.slice(2, lines.length)
  12.   val normalized_puzzle:List[String] = Utilities.getNormalizedPuzzle(puzzle)
  13.   val size:Int = Utilities.getSize(lines.slice(1, 2)(0))
  14.  
  15.   def getPuzzle: List[String] = { puzzle }
  16. }
  17.  
  18. /* Square.scala */
  19.  
  20. // The square uses the Puzzle.size property to create the correct default values
  21. class Square(val x:Int, val y:Int,
  22.              val values:List[Int] = (1 to Puzzle.size).toList,
  23.              val neighbors:List[Direction] = List[Direction](),
  24.              val solved:Boolean = false)
  25. {
  26.  
  27. // This method uses the Puzzle.size to filter out incorrect neighbor values
  28. def getPossibleValuesForNeighbors:List[Int] = {
  29.     val neighbor_values_lists = for(value <- values) yield List(value + 1, value - 1)
  30.    
  31.     neighbor_values_lists
  32.       .flatten
  33.       .distinct
  34.       .filter((value) => value > 0 && value <= Puzzle.size)
  35.       .sortWith(_ < _)
  36. }
  37.  
  38. /* SquareTest */
  39.  
  40. // This test would fail if the currently parsed board is 2x2 as 4 is not a valid neighbor
  41. // I think being able to pass a specific puzzle to my test class would solve the issue
  42. // but I'm not sure how I would do that and if it's the correct approach.
  43.  
  44. test("it is possible to get all possible neighbor values from a square") {
  45.     val square_values = List(1, 3)
  46.     val square_with_values = new Square(1, 1, values = square_values)
  47.     val neighbor_values = square_with_values.getPossibleValuesForNeighbors
  48.    
  49.     assert(neighbor_values === List(2, 4))
  50. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top