Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions src/main/scala/floodsim/model/cell/Cell.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package floodsim.model.cell

import floodsim.model.grid.Coordinate

sealed trait Cell:
def coordinate: Coordinate
def dimensions: Double
def waterVolume: Double
def obstacles: Seq[Obstacle]
Expand Down Expand Up @@ -34,7 +31,6 @@ sealed trait Cell:
protected def withUpdatedValues(waterVolume: Double, absorption: AbsorptionRate.AbsorptionRate): Cell

final private case class BaseCell(
coordinate: Coordinate,
dimensions: Double,
waterVolume: Double,
obstacles: Seq[Obstacle],
Expand All @@ -47,9 +43,8 @@ final private case class BaseCell(
copy(waterVolume = math.max(0.0, waterVolume), absorption = absorption)

object Cell:
def apply(cellType: CellType, dimensions: Double, altitude: Double, coordinate: Coordinate): Cell =
def apply(cellType: CellType, dimensions: Double, altitude: Double): Cell =
BaseCell(
coordinate = coordinate,
dimensions = dimensions,
waterVolume = 0.0,
obstacles = Seq.empty,
Expand Down
146 changes: 68 additions & 78 deletions src/test/scala/floodsim/model/cell/CellTest.scala
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package floodsim.model.cell

import floodsim.model.grid.Coordinate
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class CellTest extends AnyFlatSpec with Matchers:

"Cell companion object" should "create a Cell with default values" in {
val coordinate = Coordinate(1, 2)
val cellType = CellType.Field
val dimensions = 10.0
val altitude = 5.0
val cell = Cell(cellType, dimensions, altitude, coordinate)
cell.coordinate shouldBe coordinate
val cell = Cell(cellType, dimensions, altitude)
cell.dimensions shouldBe dimensions
cell.waterVolume shouldBe 0.0
cell.obstacles shouldBe empty
Expand All @@ -22,83 +19,76 @@ class CellTest extends AnyFlatSpec with Matchers:
cell.physics shouldBe DefaultCellPhysics
}

"Cell" should "calculate water height correctly" in {
val cell = Cell(
CellType.Field,
dimensions = 10.0,
altitude = 5.0,
coordinate = Coordinate(0, 0)
)
val cellWithWater = cell.updateWater(100.0)
cellWithWater.waterHeight shouldBe 1.0 // 100 / (10*10) = 1.0
}
"Cell" should "calculate water height correctly" in {
val cell = Cell(
CellType.Field,
dimensions = 10.0,
altitude = 5.0
)
val cellWithWater = cell.updateWater(100.0)
cellWithWater.waterHeight shouldBe 1.0 // 100 / (10*10) = 1.0
}

it should "calculate water speed correctly" in {
val cell = Cell(
CellType.Field,
dimensions = 10.0,
altitude = 5.0,
coordinate = Coordinate(0, 0)
)
cell.waterSpeed shouldBe 10.0 // MaxWaterSpeed
}
it should "calculate water speed correctly" in {
val cell = Cell(
CellType.Field,
dimensions = 10.0,
altitude = 5.0
)
cell.waterSpeed shouldBe 10.0 // MaxWaterSpeed
}

it should "calculate total height correctly" in {
val cell = Cell(
CellType.Field,
dimensions = 10.0,
altitude = 5.0,
coordinate = Coordinate(0, 0)
)
val cellWithWater = cell.updateWater(100.0) // 1.0 water height
cellWithWater.totalHeight shouldBe 6.0 // altitude(5.0) + waterHeight(1.0)
}
it should "calculate total height correctly" in {
val cell = Cell(
CellType.Field,
dimensions = 10.0,
altitude = 5.0
)
val cellWithWater = cell.updateWater(100.0) // 1.0 water height
cellWithWater.totalHeight shouldBe 6.0 // altitude(5.0) + waterHeight(1.0)
}

it should "return the cell type symbol" in {
val concreteCell = Cell(CellType.HouseWithConcrete, 10.0, 5.0, Coordinate(0, 0))
concreteCell.asSymbol shouldBe "C"
}
it should "return the cell type symbol" in {
val concreteCell = Cell(CellType.HouseWithConcrete, 10.0, 5.0)
concreteCell.asSymbol shouldBe "C"
}

it should "update water volume correctly with absorption" in {
val cell = Cell(
CellType.Field, // Field has absorption of 0.9
dimensions = 10.0,
altitude = 5.0,
coordinate = Coordinate(0, 0)
)
val initialWaterVolume = 50.0
val cellWithWater = cell.updateWater(initialWaterVolume)
val additionalWater = 10.0
val updatedCell = cellWithWater.updateWater(additionalWater)
updatedCell.waterVolume should be > cellWithWater.waterVolume
updatedCell.waterVolume should be < (cellWithWater.waterVolume + additionalWater)
updatedCell.absorption.value should be < cellWithWater.absorption.value
}
it should "update water volume correctly with absorption" in {
val cell = Cell(
CellType.Field, // Field has absorption of 0.9
dimensions = 10.0,
altitude = 5.0
)
val initialWaterVolume = 50.0
val cellWithWater = cell.updateWater(initialWaterVolume)
val additionalWater = 10.0
val updatedCell = cellWithWater.updateWater(additionalWater)
updatedCell.waterVolume should be > cellWithWater.waterVolume
updatedCell.waterVolume should be < (cellWithWater.waterVolume + additionalWater)
updatedCell.absorption.value should be < cellWithWater.absorption.value
}

it should "handle negative resulting water volumes" in {
val cell = Cell(
CellType.Field,
dimensions = 10.0,
altitude = 5.0,
coordinate = Coordinate(0, 0)
)
val cellWithWater = cell.updateWater(5.0)
val updatedCell = cellWithWater.updateWater(-10.0)
updatedCell.waterVolume shouldBe 0.0
}
it should "handle negative resulting water volumes" in {
val cell = Cell(
CellType.Field,
dimensions = 10.0,
altitude = 5.0
)
val cellWithWater = cell.updateWater(5.0)
val updatedCell = cellWithWater.updateWater(-10.0)
updatedCell.waterVolume shouldBe 0.0
}

it should "preserve all properties except water volume and absorption when updating water" in {
val coordinate = Coordinate(1, 2)
val cellType = CellType.Field
val dimensions = 10.0
val altitude = 5.0
val cell = Cell(cellType, dimensions, altitude, coordinate)
val updatedCell = cell.updateWater(20.0)
updatedCell.waterVolume should not be cell.waterVolume
updatedCell.coordinate shouldBe cell.coordinate
updatedCell.dimensions shouldBe cell.dimensions
updatedCell.obstacles shouldBe cell.obstacles
updatedCell.altitude shouldBe cell.altitude
updatedCell.cellType shouldBe cell.cellType
updatedCell.physics shouldBe cell.physics
}
it should "preserve all properties except water volume and absorption when updating water" in {
val cellType = CellType.Field
val dimensions = 10.0
val altitude = 5.0
val cell = Cell(cellType, dimensions, altitude)
val updatedCell = cell.updateWater(20.0)
updatedCell.waterVolume should not be cell.waterVolume
updatedCell.dimensions shouldBe cell.dimensions
updatedCell.obstacles shouldBe cell.obstacles
updatedCell.altitude shouldBe cell.altitude
updatedCell.cellType shouldBe cell.cellType
updatedCell.physics shouldBe cell.physics
}
Loading