BubbleTest.java 3.0 KB
Newer Older
1
/*
2
 * The MIT License
I
Ilkka Seppälä 已提交
3
 * Copyright © 2014-2019 Ilkka Seppälä
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
23

24 25
package com.iluwatar.spatialpartition;

26 27 28 29 30
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

31
import java.util.ArrayList;
S
Subhrodip Mohanta 已提交
32
import java.util.HashMap;
33 34 35
import org.junit.jupiter.api.Test;

/**
36 37
 * Testing methods in Bubble class.
 */
38 39 40 41 42

class BubbleTest {

  @Test
  void moveTest() {
43 44 45
    var b = new Bubble(10, 10, 1, 2);
    var initialX = b.coordinateX;
    var initialY = b.coordinateY;
46 47
    b.move();
    //change in x and y < |2|
48 49
    assertTrue(b.coordinateX - initialX < 2 && b.coordinateX - initialX > -2);
    assertTrue(b.coordinateY - initialY < 2 && b.coordinateY - initialY > -2);
50 51 52 53
  }

  @Test
  void touchesTest() {
54 55 56
    var b1 = new Bubble(0, 0, 1, 2);
    var b2 = new Bubble(1, 1, 2, 1);
    var b3 = new Bubble(10, 10, 3, 1);
57
    //b1 touches b2 but not b3
58 59
    assertTrue(b1.touches(b2));
    assertFalse(b1.touches(b3));
60 61 62 63
  }

  @Test
  void popTest() {
64 65
    var b1 = new Bubble(10, 10, 1, 2);
    var b2 = new Bubble(0, 0, 2, 2);
S
Subhrodip Mohanta 已提交
66
    var bubbles = new HashMap<Integer, Bubble>();
67
    bubbles.put(1, b1);
68 69
    bubbles.put(2, b2);
    b1.pop(bubbles);
S
Subhrodip Mohanta 已提交
70
    //after popping, bubble no longer in hashMap containing all bubbles
71 72
    assertNull(bubbles.get(1));
    assertNotNull(bubbles.get(2));
73 74 75 76
  }

  @Test
  void handleCollisionTest() {
77 78 79
    var b1 = new Bubble(0, 0, 1, 2);
    var b2 = new Bubble(1, 1, 2, 1);
    var b3 = new Bubble(10, 10, 3, 1);
S
Subhrodip Mohanta 已提交
80
    var bubbles = new HashMap<Integer, Bubble>();
81 82
    bubbles.put(1, b1);
    bubbles.put(2, b2);
83
    bubbles.put(3, b3);
84 85
    var bubblesToCheck = new ArrayList<Point>();
    bubblesToCheck.add(b2);
86 87 88
    bubblesToCheck.add(b3);
    b1.handleCollision(bubblesToCheck, bubbles);
    //b1 touches b2 and not b3, so b1, b2 will be popped
89 90 91
    assertNull(bubbles.get(1));
    assertNull(bubbles.get(2));
    assertNotNull(bubbles.get(3));
92 93
  }
}