├── .editorconfig ├── .gitignore ├── Assignment1 ├── README.md └── Tester.java ├── Assignment2 └── Minitester.java ├── Assignment3 └── Tester.java ├── FinalProject └── StudentTests.java └── README.md /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | indent_size = 2 7 | indent_style = space 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/santoshlite/comp250Tester/63b6dd6f3ba77d65e2cae5d6cfb2f413f49f1514/.gitignore -------------------------------------------------------------------------------- /Assignment1/README.md: -------------------------------------------------------------------------------- 1 | ## AirportTest() 2 | 3 | | Index | Name | 4 | | :---: | :---: | 5 | | #1 | Airport getFees() Test1 | 6 | | #2 | Airport getDistance() Test1 | 7 | | #3 | Airport getDistance() Test2 | 8 | 9 | ## RoomTest() 10 | 11 | | Index | Name | 12 | | :---: | :---: | 13 | | #1 | Room Constructor Test1 | 14 | | #2 | Room Constructor Test2 | 15 | | #3 | Room Constructor Test4 | 16 | | #4 | Room Copy Constructor Test1 | 17 | | #5 | Room findAvailableRoom() Test1 | 18 | | #6 | Room makeRoomAvailable() Test1 | 19 | | #7 | Room makeRoomAvailable() Test2 | 20 | | #8 | Room changeAvailability Test1| 21 | | #9 | Room changeAvailability Test2| 22 | 23 | 24 | ## HotelTest() 25 | 26 | | Index | Name | 27 | | :---: | :---: | 28 | | #1 | Hotel Constructor Test1 | 29 | | #2 | Hotel reserveRoom() Test1 | 30 | | #3 | Hotel reserveRoom() Test2 | 31 | | #4 | Hotel cancelRoom() Test1 | 32 | | #5 | Hotel cancelRoom() Test2 | 33 | 34 | ## CustomerTest() 35 | 36 | | Index | Name | 37 | | :---: | :---: | 38 | | #1 | Customer Constructor Test1 | 39 | | #2 | Customer addFunds() Test1 | 40 | | #3 | Customer addToBasket(Reservation) Test1 | 41 | | #4 | Customer addToBasket(HotelReservation) Test2 | 42 | | #5 | Customer addToBasket(With Breakfast) Test3 | 43 | | #6 | Customer addToBasket(Without Breakfast) Test4 | 44 | | #7 | Customer removeFromBasket(Reservation) Test1 | 45 | | #8 | Customer removeFromBasket(Reservation) Test2 | 46 | | #9 | Customer checkOut() Test1 | 47 | 48 | ## ReservationTest() 49 | 50 | | Index | Name | 51 | | :---: | :---: | 52 | | #1 | Reservation reservationName() Test1 | 53 | 54 | ## HotelReservationTest() 55 | 56 | | Index | Name | 57 | | :---: | :---: | 58 | | #1 | HotelReservation getNumOfNights() Test1 | 59 | | #2 | HotelReservation getCost() Test1 | 60 | | #3 | HotelReservation equals() Test1 | 61 | | #4 | HotelReservation equals() Test2 | 62 | | #5 | HotelReservation equals() Test3 | 63 | 64 | ## FlightReservationTest() 65 | 66 | | Index | Name | 67 | | :---: | :---: | 68 | | #1 | FlightReservation getCost() Test1 | 69 | | #2 | FlightReservation equals() Test1 | 70 | | #3 | FlightReservation equals() Test2 | 71 | | #4 | FlightReservation equals() Test3 | 72 | 73 | ## BnBReservationTest() 74 | 75 | | Index | Name | 76 | | :---: | :---: | 77 | | #1 | BnBReservation reservationName() Test1 | 78 | | #2 | BnBReservation getCost() Test1 | 79 | 80 | ## BasketTest() 81 | 82 | | Index | Name | 83 | | :---: | :---: | 84 | | #1 | Basket add() Test1 | 85 | | #2 | Basket add() Test2 | 86 | | #3 | Basket getProducts() Test1 | 87 | | #4 | Basket getNumOfReservations Test1 | 88 | | #5 | Basket remove() Test1 | 89 | | #6 | Basket clear() Test1 | 90 | | #7 | Basket getTotalCost Test1 | 91 | -------------------------------------------------------------------------------- /Assignment1/Tester.java: -------------------------------------------------------------------------------- 1 | package assignment1; 2 | 3 | // To make the tester work, you might need to setUp some import libraries. To do so: 4 | // 1: Hover over the red text saying junit (or other libraries that are red) 5 | // 2: click add to classpath or import statement (depends on the IDE) 6 | import org.junit.jupiter.api.*; 7 | import static org.junit.jupiter.api.Assertions.*; 8 | import java.lang.reflect.Field; 9 | import java.util.Arrays; 10 | 11 | 12 | public class Tester {} 13 | 14 | class AirportTest { // 2 points 15 | 16 | @Test 17 | @Tag("score:1") @DisplayName("Airport getFees() Test1") 18 | void getFees_Test1() { 19 | Airport a = new Airport(200, 100, 50); 20 | assertEquals(50, a.getFees(), 21 | "Airport: getFees() did not return the correct fees"); 22 | } 23 | 24 | @Test 25 | @Tag("score:1") @DisplayName("Airport getDistance() Test1") 26 | void getDistance_Test1() { 27 | Airport a = new Airport(44, 120, 100); 28 | Airport b = new Airport(50, 112, 100); 29 | assertEquals(10, Airport.getDistance(a, b), 30 | "Airport: getDistance() did not return the correct distance"); 31 | } 32 | 33 | //Test if the method rounds up properly 34 | @Test 35 | @Tag("score:1") @DisplayName("Airport getDistance() Test2") 36 | void getDistance_Test2() { 37 | Airport a = new Airport(44, 120, 100); 38 | Airport b = new Airport(55, 113, 100); 39 | assertEquals(14, Airport.getDistance(a, b), //Exact value 13.04 should be rounded up to 14 40 | "Airport: getDistance() did not return the correct distance"); 41 | } 42 | } 43 | 44 | class RoomTest { // 6 points 45 | 46 | @Test 47 | @Tag("score:1") @DisplayName("Room Constructor Test1") 48 | void roomConstructor_Test1() { 49 | Room room = new Room("double"); 50 | assertEquals("double", room.getType(), 51 | "Room: getType() did not return the correct type for a double room"); 52 | assertEquals(9000, room.getPrice(), 53 | "Room: getPrice() did not return the correct price for a double room"); 54 | 55 | room = new Room("queen"); 56 | assertEquals("queen", room.getType(), 57 | "Room: getType() did not return the correct type for a queen room"); 58 | assertEquals(11000, room.getPrice(), 59 | "Room: getPrice() did not return the correct price for a queen room"); 60 | 61 | room = new Room("king"); 62 | assertEquals("king", room.getType(), 63 | "Room: getType() did not return the correct type for a king room"); 64 | assertEquals(15000, room.getPrice(), 65 | "Room: getPrice() did not return the correct price for a king room"); 66 | 67 | } 68 | 69 | 70 | @Test 71 | @Tag("score:1") @DisplayName("Room Constructor Test4") 72 | void roomConstructor_Test4() { 73 | assertThrows(IllegalArgumentException.class, () -> new Room("twin"), 74 | "Room: constructor did not throw an exception for an invalid room type"); 75 | } 76 | 77 | @Test 78 | @Tag("score:1") @DisplayName("Room Copy Constructor Test1") 79 | void copyConstructor_Test1() { 80 | Room room = new Room("double"); 81 | Room copyRoom = new Room(room); 82 | 83 | assertNotSame(room, copyRoom, 84 | "Room: copy constructor did not create a new object"); 85 | 86 | assertEquals("double", copyRoom.getType(), 87 | "Room: getType() did not return the correct type for its copy"); 88 | 89 | assertEquals(9000, copyRoom.getPrice(), 90 | "Room: getPrice() did not return the correct price for its copy"); 91 | } 92 | 93 | @Test 94 | @Tag("score:1") @DisplayName("Room findAvailableRoom() Test1") 95 | void findAvailableRoom_Test1() { 96 | Room[] rooms = {new Room("king"), new Room("queen"), new Room("double")}; 97 | assertEquals(rooms[1], Room.findAvailableRoom(rooms, "queen"), 98 | "Room: findAvailableRoom() did not return the correct room"); 99 | } 100 | 101 | //Check if the method returns null when no room is found 102 | @Test 103 | @Tag("score:1") @DisplayName("Room findAvailableRoom() Test2") 104 | void findAvailableRoom_Test2() { 105 | Room[] rooms = {new Room("king"), new Room("king"), new Room("double")}; 106 | assertNull(Room.findAvailableRoom(rooms, "queen"), 107 | "Room: findAvailableRoom() did not return the correct room"); 108 | } 109 | 110 | 111 | @Test 112 | @Tag("score:1") @DisplayName("Room findAvailableRoom() Test3") 113 | void findAvailableRoom_Test3() { 114 | Room[] rooms = {new Room("king"), new Room("queen"), new Room("double"),new Room("queen")}; 115 | assertEquals(rooms[1], Room.findAvailableRoom(rooms, "queen"), 116 | "Room: findAvailableRoom() did not return the correct room"); 117 | } 118 | 119 | @Test 120 | @Tag("score:1") @DisplayName("Room findAvailableRoom() Test4") 121 | void findAvailableRoom_Test4() { 122 | Room[] rooms = {}; 123 | assertNull(Room.findAvailableRoom(rooms, "queen"), 124 | "Room: findAvailableRoom() did not return the correct room"); 125 | } 126 | 127 | @Test 128 | @Tag("score:1") @DisplayName("Room findAvailableRoom() Test7") 129 | void findAvailableRoom_Test7() { 130 | Room[] rooms = {new Room("king"), null, new Room("queen"), new Room("double"),new Room("queen")}; 131 | assertEquals(rooms[2], Room.findAvailableRoom(rooms, "queen"), 132 | "Room: findAvailableRoom() did not return the correct room"); 133 | } 134 | 135 | @Test 136 | @Tag("score:1") @DisplayName("Room makeRoomAvailable() Test1") 137 | void makeRoomAvailable_Test1() { 138 | Room[] rooms = {new Room("double"), new Room("king"), new Room("queen")}; 139 | assertFalse(Room.makeRoomAvailable(rooms, "king"), 140 | "Room: makeRoomAvailable() did not return the correct value" ); 141 | } 142 | 143 | //Check if the method returns false if the input room is not in the list 144 | @Test 145 | @Tag("score:1") @DisplayName("Room makeRoomAvailable() Test2") 146 | void makeRoomAvailable_Test2() { 147 | Room[] rooms = {new Room("double"), new Room("queen"), new Room("queen")}; 148 | assertFalse(Room.makeRoomAvailable(rooms, "king"), 149 | "Room: makeRoomAvailable() did not return the correct value" ); 150 | } 151 | 152 | 153 | @Test 154 | @Tag("score:1") @DisplayName("Room makeRoomAvailable() Test3") 155 | void makeRoomAvailable_Test3() { 156 | Room doubleRoom = new Room("double"); 157 | Room[] rooms = {doubleRoom, new Room("queen"), new Room("queen")}; 158 | doubleRoom.changeAvailability(); 159 | assertTrue(Room.makeRoomAvailable(rooms, "double"), 160 | "Room: makeRoomAvailable() did not return the correct value" ); 161 | } 162 | 163 | 164 | @Test 165 | @Tag("score:1") @DisplayName("Room makeRoomAvailable() Test6") 166 | void makeRoomAvailable_Test6() { 167 | 168 | Room king = new Room("double"); 169 | Room[] rooms = {king, null}; 170 | assertFalse(Room.makeRoomAvailable(rooms, "double"), 171 | "Room: makeRoomAvailable() did not return the correct value" ); 172 | } 173 | 174 | @Test 175 | @Tag("score:1") @DisplayName("Room makeRoomAvailable() Test7") 176 | void makeRoomAvailable_Test7() { 177 | 178 | Room aDouble = new Room("double"); 179 | Room aKing = new Room("king"); 180 | aKing.changeAvailability(); // Making the aKing room not available/false 181 | Room[] rooms = {aDouble, null, aKing}; // Should go through room null without return false 182 | assertTrue(Room.makeRoomAvailable(rooms, "king"), 183 | "Room: makeRoomAvailable() did not return the correct value" ); 184 | } 185 | 186 | @Test 187 | @Tag("score:1") @DisplayName("Room changeAvailability Test1") 188 | void changeAvailability_Test1() { 189 | Room room = new Room("double"); 190 | Room[] rooms = {room}; 191 | room.changeAvailability(); 192 | assertTrue(Room.makeRoomAvailable(rooms, "double"), 193 | "Room: changeAvailability() did not change the availability of the room"); 194 | 195 | } 196 | 197 | @Test 198 | //Check if the method returns false if all rooms are already available 199 | @Tag("score:1") @DisplayName("Room changeAvailability Test2") 200 | void changeAvailability_Test2() { 201 | Room room1 = new Room("double"); 202 | Room room2 = new Room("double"); 203 | Room[] rooms = {room1,room2}; 204 | assertFalse(Room.makeRoomAvailable(rooms, "double"), 205 | "Room: changeAvailability() did not return false"); 206 | 207 | } 208 | 209 | } 210 | 211 | class HotelTest { // 7 points 212 | @Test 213 | @Tag("score:2") @DisplayName("Hotel Constructor Test1") 214 | void deepCopyConstructor_Test1 () throws IllegalAccessException { 215 | Room[] rooms = {new Room("double"), new Room("queen"), new Room("king")}; 216 | Hotel hotel = new Hotel("Hotel1", rooms); 217 | 218 | Room[] roomsCopy = new Room[rooms.length]; 219 | String name = ""; 220 | 221 | Field[] fields = Hotel.class.getDeclaredFields(); 222 | for (Field field : fields) { 223 | if (field.getType() == Room[].class) { 224 | field.setAccessible(true); 225 | roomsCopy = (Room[]) field.get(hotel); 226 | } else if (field.getType() == String.class) { 227 | field.setAccessible(true); 228 | name = (String) field.get(hotel); 229 | } 230 | } 231 | 232 | assertFalse(Arrays.deepEquals(rooms, roomsCopy), 233 | "Hotel: deep copy constructor did not create a deep copy of the input rooms array"); 234 | assertEquals("Hotel1", name, 235 | "Hotel: Constructor did not set the name correctly"); 236 | } 237 | @Test 238 | @Tag ("score:2") @DisplayName("Hotel reserveRoom() Test1") 239 | void reserveRoom_Test1() { 240 | Room[] rooms = {new Room("double")}; 241 | Hotel hotel1 = new Hotel("Hotel1", rooms); 242 | assertEquals(9000, hotel1.reserveRoom("double"), 243 | "Hotel: reserveRoom() did not return the correct price of the room after reserving it"); 244 | } 245 | 246 | @Test 247 | @Tag ("score:1") @DisplayName("Hotel reserveRoom() Test2") 248 | void reserveRoom_Test2() { 249 | Room[] rooms = {new Room("double")}; 250 | Hotel hotel1 = new Hotel("Hotel1", rooms); 251 | assertThrows(IllegalArgumentException.class, () -> hotel1.reserveRoom("king"), 252 | "Hotel: reserveRoom() did not throw an exception for an invalid room type"); 253 | } 254 | 255 | @Test 256 | @Tag ("score:1") @DisplayName("Hotel cancelRoom() Test1") 257 | void cancelRoom_Test1() { 258 | Room[] rooms = {new Room("double")}; 259 | Hotel hotel1 = new Hotel("Hotel1", rooms); 260 | hotel1.reserveRoom("double"); 261 | assertTrue(hotel1.cancelRoom("double"), 262 | "Hotel: cancelRoom() did not return the correct value"); 263 | } 264 | 265 | @Test 266 | @Tag ("score:1") @DisplayName("Hotel cancelRoom() Test2") 267 | void cancelRoom_Test2() { 268 | Room[] rooms = {new Room("double")}; 269 | Hotel hotel1 = new Hotel("Hotel1", rooms); 270 | assertFalse(hotel1.cancelRoom("king"), 271 | "Hotel: cancelRoom() did not return the correct value"); 272 | } 273 | 274 | // empty room listing 275 | @Test 276 | @Tag ("score:1") @DisplayName("Hotel cancelRoom() Test3") 277 | void cancelRoom_Test3() { 278 | Room[] rooms = {}; 279 | Hotel hotel1 = new Hotel("Hotel1", rooms); 280 | assertFalse(hotel1.cancelRoom("king"), 281 | "Hotel: cancelRoom() did not return the correct value"); 282 | } 283 | 284 | // input null value for type of room 285 | @Test 286 | @Tag ("score:1") @DisplayName("Hotel cancelRoom() Test4") 287 | void cancelRoom_Test4() { 288 | Room[] rooms = {}; 289 | Hotel hotel1 = new Hotel("Hotel1", rooms); 290 | assertFalse(hotel1.cancelRoom(null), 291 | "Hotel: cancelRoom() did not return the correct value"); 292 | } 293 | 294 | } 295 | 296 | class CustomerTest { // 7 points 297 | @Test 298 | @Tag("score:1") @DisplayName("Customer Constructor Test1") 299 | void customerConstructor_Test1() { 300 | Customer customer = new Customer("bob", 100); 301 | assertEquals("bob", customer.getName(), 302 | "Customer: getName() did not return the correct name"); 303 | assertEquals(100, customer.getBalance(), 304 | "Customer: getBalance() did not return the correct balance"); 305 | assertEquals(0, customer.getBasket().getNumOfReservations(), 306 | "Customer: getBasket() did not return the correct number of reservations"); 307 | } 308 | 309 | @Test 310 | @Tag("score:1") @DisplayName("Customer addFunds() Test1") 311 | void addFunds_Test1() { 312 | Customer customer = new Customer("bob", 100); 313 | assertEquals(101, customer.addFunds(1), 314 | "Customer: addFunds() did not return the correct funds"); 315 | assertEquals(101, customer.getBalance(), 316 | "Customer: getBalance() did not return the correct balance"); 317 | } 318 | 319 | @Test 320 | @Tag("score:1") @DisplayName("Customer addToBasket(Reservation) Test1") 321 | void addToBasket_Test1_Reservation() { 322 | Customer customer = new Customer("bob", 100); 323 | Room[] rooms = {new Room("double")}; 324 | Hotel hotel = new Hotel("barcelo", rooms); 325 | HotelReservation reservation = new HotelReservation("bob", hotel, "double", 2); 326 | 327 | assertEquals(1, customer.addToBasket(reservation), 328 | "Customer: addToBasket(Reservation) did not return the correct number of reservations in the basket"); 329 | } 330 | 331 | 332 | @Test 333 | @Tag("score:1") @DisplayName("Customer addToBasket(HotelReservation) Test2") 334 | void addToBasket_Test2_Reservation() { 335 | Customer customer = new Customer("bob", 100); 336 | Room[] rooms = {new Room("double")}; 337 | Hotel hotel = new Hotel("barcelo", rooms); 338 | 339 | assertEquals(1, customer.addToBasket(hotel, "double", 2, false), 340 | "Customer: addToBasket() for the Hotel type did not return the correct number of reservations in the basket"); 341 | } 342 | 343 | @Test 344 | @Tag("score:1") @DisplayName("Customer addToBasket(Without Breakfast) Test3") 345 | void addToBasket_Test3_Reservation() { 346 | Customer customer = new Customer("Killua", 100); 347 | Room[] rooms = {new Room("double")}; 348 | Hotel hotel = new Hotel("Greed Island", rooms); 349 | customer.addToBasket(hotel, "double", 2, false); 350 | 351 | int totalPrice = customer.getBasket().getTotalCost(); 352 | 353 | assertEquals(18000, totalPrice, 354 | "Customer: addToBasket() for the Hotel type did not return the correct number of reservations in the basket"); 355 | } 356 | 357 | @Test 358 | @Tag("score:1") @DisplayName("Customer addToBasket(With Breakfast) Test4") 359 | void addToBasket_Test4_Reservation() { 360 | Customer customer = new Customer("Killua", 100); 361 | Room[] rooms = {new Room("double")}; 362 | Hotel hotel = new Hotel("Greed Island", rooms); 363 | customer.addToBasket(hotel, "double", 2, true); 364 | 365 | int totalPrice = customer.getBasket().getTotalCost(); 366 | 367 | assertEquals(20000, totalPrice, 368 | "Customer: addToBasket() for the Hotel type did not return the correct number of reservations in the basket"); 369 | } 370 | 371 | 372 | @Test 373 | @Tag("score:1") @DisplayName("Customer addToBasket(FlightReservation) Test5") 374 | void addToBasket_Test5_Reservation() { 375 | Customer customer = new Customer("bob", 100); 376 | Airport airport1 = new Airport(100, 200, 1000); 377 | Airport airport2 = new Airport(10, 20, 2000); 378 | 379 | assertEquals(1, customer.addToBasket(airport1, airport2), 380 | "Customer: addToBasket() for the Flight type did not return the correct number of reservations in the basket"); 381 | } 382 | 383 | @Test 384 | @Tag("score:1") @DisplayName("Customer removeFromBasket(Reservation) Test1") 385 | void removeFromBasket_Test1() { 386 | Customer customer = new Customer("bob", 100); 387 | 388 | Room[] rooms = {new Room("double")}; 389 | Hotel hotel = new Hotel("barcelo", rooms); 390 | Reservation reservation = new HotelReservation("bob", hotel, "double", 2); 391 | customer.addToBasket(reservation); 392 | 393 | assertTrue(customer.removeFromBasket(reservation), 394 | "Customer: removeFromBasket(Reservation) did not return the correct value"); 395 | } 396 | 397 | 398 | @Test 399 | @Tag("score:1") @DisplayName("Customer removeFromBasket(Reservation) Test2") 400 | void removeFromBasket_Test2() { 401 | Customer customer = new Customer("Bob", 100); 402 | Customer customer2 = new Customer("Not Bob", 100); 403 | 404 | Room[] rooms = {new Room("double")}; 405 | Hotel hotel = new Hotel("Average Hotel", rooms); 406 | Reservation reservation = new HotelReservation("Bob", hotel, "double", 2); 407 | customer.addToBasket(reservation); 408 | 409 | assertFalse(customer2.removeFromBasket(reservation), 410 | "Customer: removeFromBasket(Reservation) did not return the correct value"); 411 | } 412 | 413 | 414 | // check if it works when we remove null 415 | @Test 416 | @Tag("score:1") @DisplayName("Customer removeFromBasket(Reservation) Test3") 417 | void removeFromBasket_Test3() { 418 | Customer customer = new Customer("Bob", 100); 419 | 420 | Room[] rooms = {}; 421 | Hotel hotel = new Hotel("Average Hotel", rooms); 422 | 423 | Airport a1 = new Airport(44, 120, 100); 424 | Airport a2 = new Airport(45, 120, 100); 425 | 426 | assertFalse(customer.removeFromBasket(null), 427 | "Customer: removeFromBasket(Reservation) did not return the correct value"); 428 | } 429 | 430 | @Test 431 | @Tag("score:1") @DisplayName("Customer checkOut() Test1") 432 | void checkout_Tes1() { 433 | Customer customer = new Customer("bob", 100000); 434 | 435 | Room[] rooms = {new Room("double")}; 436 | Hotel hotel = new Hotel("barcelo", rooms); 437 | HotelReservation reservation = new HotelReservation("bob", hotel, "double", 2); 438 | customer.addToBasket(reservation); 439 | 440 | assertEquals(82000, customer.checkOut(), 441 | "Customer: checkOut() did not return the correct balance after checkOut"); 442 | } 443 | 444 | 445 | } 446 | 447 | class ReservationTest { // 2 points 448 | @Test 449 | @Tag("score:2") @DisplayName("Reservation reservationName() Test1") 450 | void reservationName() { 451 | Reservation fakeReservation = new ReservationTest.FakeReservation("Alex"); 452 | assertEquals("Alex", fakeReservation.reservationName(), 453 | "Reservation: reservationName() returns the wrong name."); 454 | } 455 | private static class FakeReservation extends Reservation { 456 | 457 | public FakeReservation(String name) { 458 | super(name); 459 | } 460 | 461 | @Override 462 | public int getCost() { 463 | return 0; 464 | } 465 | 466 | @Override 467 | public boolean equals(Object o) { 468 | return false; 469 | } 470 | } 471 | } 472 | 473 | class HotelReservationTest { // 4 points 474 | 475 | @Test 476 | @Tag("score:1") @DisplayName("HotelReservation getNumOfNights() Test1") 477 | void getNumOfNights() { 478 | Room[] rooms = {new Room("double")}; 479 | Hotel hotel1 = new Hotel("Hotel1", rooms); 480 | HotelReservation hotelReservation1 = new HotelReservation("Alex", hotel1, "double", 2); 481 | assertEquals(2, hotelReservation1.getNumOfNights(), 482 | "HotelReservation: getNumOfNights() returns the wrong number of nights."); 483 | } 484 | 485 | @Test 486 | @Tag("score:1") @DisplayName("HotelReservation getCost() Test1") 487 | void getCost() { 488 | Room[] rooms = {new Room("double")}; 489 | Hotel hotel1 = new Hotel("Hotel1", rooms); 490 | HotelReservation hotelReservation1 = new HotelReservation("Alex", hotel1, "double", 2); 491 | assertEquals(18000, hotelReservation1.getCost(), 492 | "HotelReservation: getCost() returns the wrong cost."); 493 | } 494 | 495 | @Test 496 | @Tag("score:1") @DisplayName("HotelReservation equals() Test1") 497 | void testEquals1() { 498 | Room[] rooms = {new Room("double")}; 499 | Hotel hotel1 = new Hotel("Hotel1", rooms); 500 | HotelReservation hotelReservation1 = new HotelReservation("Alex", hotel1, "double", 2); 501 | HotelReservation hotelReservation2 = hotelReservation1; 502 | 503 | assertTrue(hotelReservation1.equals(hotelReservation2), 504 | "HotelReservation: equals() returns the wrong value."); 505 | } 506 | 507 | @Test 508 | @Tag("score:1") @DisplayName("HotelReservation equals() Test2") 509 | void testEquals2() { 510 | Room[] rooms = {new Room("double"), new Room("king")}; 511 | Hotel hotel1 = new Hotel("Hotel1", rooms); 512 | HotelReservation hotelReservation1 = new HotelReservation("Alex", hotel1, "double", 2); 513 | HotelReservation hotelReservation2 = new HotelReservation("Bob", hotel1, "king", 1); 514 | assertFalse(hotelReservation1.equals(hotelReservation2), "HotelReservation: equals() returns the wrong value"); 515 | } 516 | 517 | 518 | @Test 519 | @Tag("score:1") @DisplayName("HotelReservation equals() Test3") 520 | void testEquals3() { 521 | Room[] rooms = {new Room("double"), new Room("double")}; 522 | Hotel hotel1 = new Hotel("Hotel1", rooms); 523 | HotelReservation hotelReservation1 = new HotelReservation("Alex", hotel1, "double", 2); 524 | HotelReservation hotelReservation2 = new HotelReservation("Alex", hotel1, "double", 2); 525 | assertTrue(hotelReservation1.equals(hotelReservation2), "HotelReservation: equals() returns the wrong value"); 526 | } 527 | 528 | // check equality with null 529 | @Test 530 | @Tag("score:1") @DisplayName("HotelReservation equals() Test4") 531 | void testEquals4() { 532 | Room[] rooms = {new Room("double"), new Room("double")}; 533 | Hotel hotel1 = new Hotel("Hotel1", rooms); 534 | HotelReservation hotelReservation1 = new HotelReservation("Alex", hotel1, "double", 2); 535 | HotelReservation hotelReservation2 = new HotelReservation("Alex", hotel1, "double", 2); 536 | assertFalse(hotelReservation1.equals(null), "HotelReservation: equals() returns the wrong value"); 537 | } 538 | 539 | // check equality with another reservation 540 | @Test 541 | @Tag("score:1") @DisplayName("HotelReservation equals() Test5") 542 | void testEquals5() { 543 | Room[] rooms = {new Room("double"), new Room("double")}; 544 | Hotel hotel1 = new Hotel("Hotel1", rooms); 545 | HotelReservation hotelReservation1 = new HotelReservation("Alex", hotel1, "double", 2); 546 | HotelReservation hotelReservation2 = new HotelReservation("Alex", hotel1, "double", 2); 547 | 548 | Airport airport1 = new Airport(44, 120, 100); 549 | Airport airport2 = new Airport(50, 112, 110); 550 | FlightReservation flightReservation1 = new FlightReservation("Alex", airport1, airport2); 551 | 552 | assertFalse(hotelReservation1.equals(flightReservation1), "HotelReservation: equals() returns the wrong value"); 553 | } 554 | 555 | } 556 | 557 | class FlightReservationTest { // 3 points 558 | 559 | @Test 560 | @Tag("score:1") @DisplayName("FlightReservation getCost() Test1") 561 | void getCost() { 562 | Airport airport1 = new Airport(44, 120, 100); 563 | Airport airport2 = new Airport(50, 112, 110); 564 | FlightReservation flightReservation1 = new FlightReservation("Alex", airport1, airport2); 565 | assertEquals(5593, flightReservation1.getCost(), 566 | "FlightReservation: getCost() returns the wrong cost."); 567 | } 568 | 569 | @Test 570 | @Tag("score:1") @DisplayName("FlightReservation equals() Test1") 571 | void testEquals1() { 572 | Airport airport1 = new Airport(44, 120, 100); 573 | Airport airport2 = new Airport(50, 112, 110); 574 | 575 | FlightReservation flightReservation1 = new FlightReservation("Alex", airport1, airport2); 576 | FlightReservation flightReservation2 = flightReservation1; 577 | 578 | assertTrue(flightReservation1.equals(flightReservation2), 579 | "FlightReservation: equals() returns the wrong value"); 580 | } 581 | 582 | @Test 583 | @Tag("score:1") @DisplayName("FlightReservation equals() Test2") 584 | void testEquals2() { 585 | Airport airport1 = new Airport(44, 120, 100); 586 | Airport airport2 = new Airport(50, 112, 110); 587 | 588 | FlightReservation flightReservation1 = new FlightReservation("Alex", airport1, airport2); 589 | FlightReservation flightReservation2 = new FlightReservation("Alex", airport2, airport1); 590 | 591 | assertFalse(flightReservation1.equals(flightReservation2), 592 | "FlightReservation: equals() returns the wrong value."); 593 | } 594 | 595 | 596 | // check equals(null) 597 | @Test 598 | @Tag("score:1") @DisplayName("FlightReservation equals() Test4") 599 | void testEquals4() { 600 | Airport airport1 = new Airport(44, 120, 100); 601 | Airport airport2 = new Airport(50, 112, 110); 602 | 603 | FlightReservation flightReservation1 = new FlightReservation("Alex", airport1, airport2); 604 | FlightReservation flightReservation2 = new FlightReservation("Alex", airport1, airport2); 605 | 606 | assertFalse(flightReservation1.equals(null), 607 | "FlightReservation: equals() returns the wrong value"); 608 | } 609 | 610 | // check equality with another reservation 611 | @Test 612 | @Tag("score:1") @DisplayName("FlightReservation equals() Test5") 613 | void testEquals5() { 614 | Room[] rooms = {new Room("double"), new Room("double")}; 615 | Hotel hotel1 = new Hotel("Hotel1", rooms); 616 | HotelReservation hotelReservation1 = new HotelReservation("Alex", hotel1, "double", 2); 617 | HotelReservation hotelReservation2 = new HotelReservation("Alex", hotel1, "double", 2); 618 | 619 | Airport airport1 = new Airport(44, 120, 100); 620 | Airport airport2 = new Airport(50, 112, 110); 621 | FlightReservation flightReservation1 = new FlightReservation("Alex", airport1, airport2); 622 | 623 | assertFalse(flightReservation1.equals(hotelReservation1), "HotelReservation: equals() returns the wrong value"); 624 | } 625 | } 626 | 627 | class BnBReservationTest { // 2 points 628 | @Test 629 | @Tag("score:1") @DisplayName("BnBReservation reservationName() Test1") 630 | void reservationName() { 631 | Room[] rooms = {new Room("double")}; 632 | Hotel hotel1 = new Hotel("Hotel1", rooms); 633 | 634 | BnBReservation bnBReservation = new BnBReservation("Alex", hotel1, "double", 2); 635 | assertEquals("Alex", bnBReservation.reservationName(), 636 | "BnBReservation: getCost() returns the wrong name."); 637 | } 638 | 639 | @Test 640 | @Tag("score:1") @DisplayName("BnBReservation getCost() Test1") 641 | void getCost() { 642 | Room[] rooms = {new Room("double")}; 643 | Hotel hotel1 = new Hotel("Hotel1", rooms); 644 | 645 | BnBReservation bnBReservation = new BnBReservation("Alex", hotel1, "double", 2); 646 | assertEquals(20000, bnBReservation.getCost(), 647 | "BnBReservation: getCost() returns the wrong value."); 648 | } 649 | 650 | // check equality with HotelReservation 651 | @Test 652 | @Tag("score:1") @DisplayName("BnBReservation equals() Test1") 653 | void testEquals1() { 654 | Room[] rooms = {new Room("double"), new Room("double")}; 655 | Hotel hotel1 = new Hotel("Hotel1", rooms); 656 | HotelReservation hotelReservation = new HotelReservation("Alex", hotel1, "double", 2); 657 | BnBReservation bnBReservation = new BnBReservation("Alex", hotel1, "double", 2); 658 | 659 | assertFalse(bnBReservation.equals(hotelReservation), "HotelReservation: equals() returns the wrong value"); 660 | } 661 | } 662 | 663 | class BasketTest { // 7 points 664 | 665 | @Test 666 | @Tag("score:1") @DisplayName("Basket add() Test1") 667 | void addTest1() { 668 | Basket basket = new Basket(); 669 | Reservation reservation1 = new BasketTest.FakeReservation("Alex"); 670 | Reservation reservation2 = new BasketTest.FakeReservation("Bob"); 671 | 672 | int number = basket.add(reservation1); 673 | number = basket.add(reservation2); 674 | 675 | assertEquals(2, number, 676 | "Basket: add() returns the wrong number of reservations now in the basket."); 677 | } 678 | 679 | @Test 680 | @Tag("score:1") @DisplayName("Basket add() Test2") 681 | void addTest2() { 682 | Basket basket = new Basket(); 683 | int number = 0; 684 | for(int i = 0; i < 30; i++){ 685 | number = basket.add(new HotelReservation("Marty", new Hotel("Gatewater", new Room[]{new Room("queen")}), "queen", 4)); 686 | } 687 | assertEquals(30, number, "Basket: add() returns the wrong number of reservations now in the basket when adding many items."); 688 | } 689 | 690 | @Test 691 | @Tag("score:2") @DisplayName("Basket getProducts() Test1") 692 | void getProducts2() { 693 | Basket basket = new Basket(); 694 | HotelReservation hotelReservation1 = new HotelReservation("Alex", new Hotel("Hotel1", new Room[]{new Room("double")}), "double", 2); 695 | FlightReservation flightReservation1 = new FlightReservation("Bob", new Airport(44, 120, 100), new Airport(50, 112, 110)); 696 | 697 | basket.add(hotelReservation1); 698 | basket.add(flightReservation1); 699 | 700 | Reservation[] expected = {hotelReservation1, flightReservation1}; 701 | Reservation[] output = basket.getProducts(); 702 | 703 | for (int i = 0; i < expected.length; i++) { 704 | assertEquals(expected[i], output[i], 705 | "Basket: getProducts() returns the wrong order of reservations."); 706 | } 707 | } 708 | 709 | @Test 710 | @Tag("score:1") @DisplayName("Basket getNumOfReservations Test1") 711 | void getNumOfReservations() { 712 | Basket basket1 = new Basket(); 713 | Reservation reservation1 = new BasketTest.FakeReservation("Alex"); 714 | Reservation reservation2 = new BasketTest.FakeReservation("Bob"); 715 | 716 | basket1.add(reservation1); 717 | basket1.add(reservation2); 718 | 719 | assertEquals(2, basket1.getNumOfReservations(), 720 | "Basket: getNumOfReservations() returns the wrong number of reservations"); 721 | } 722 | 723 | 724 | @Test 725 | @Tag("score:1") @DisplayName("Basket remove() Test1") 726 | void remove() { 727 | Basket basket1 = new Basket(); 728 | Airport airport1 = new Airport(44, 120, 100); 729 | Airport airport2 = new Airport(50, 112, 110); 730 | FlightReservation reservation1 = new FlightReservation("Alex", airport1, airport2); 731 | 732 | basket1.add(reservation1); 733 | 734 | boolean test = basket1.remove(reservation1); 735 | 736 | assertEquals(0, basket1.getNumOfReservations(), 737 | "Basket: remove() didn't remove the reservation"); 738 | 739 | assertTrue(test, "Basket: remove() returns the wrong value"); 740 | 741 | } 742 | 743 | @Test 744 | @Tag("score:1") @DisplayName("Basket remove() Test2") 745 | void remove2() { 746 | Basket basket1 = new Basket(); 747 | Airport airport1 = new Airport(44, 120, 100); 748 | Airport airport2 = new Airport(50, 112, 110); 749 | boolean status; 750 | 751 | FlightReservation reservation1 = new FlightReservation("Gon", airport1, airport2); 752 | FlightReservation reservation2 = new FlightReservation("Killua", airport1, airport2); 753 | 754 | basket1.add(reservation1); 755 | basket1.add(reservation2); 756 | basket1.add(reservation1); 757 | basket1.add(reservation1); 758 | // basket1 == {reservation1, reservation2, reservation1, reservation1, . . .} 759 | basket1.remove(reservation1); 760 | // basket1 == {reservation2, reservation1, reservation1, . . .} AND NOT {reservation2, . . .} 761 | 762 | status = (basket1.getProducts()[0].equals(reservation2) && basket1.getProducts()[1].equals(reservation1) && basket1.getProducts()[2].equals(reservation1)); 763 | 764 | assertTrue(status, "Basket: remove() didn't remove the FIRST reservation input"); 765 | } 766 | 767 | @Test 768 | @Tag("score:1") @DisplayName("Basket remove() Test3") 769 | void remove3() { 770 | Basket basket1 = new Basket(); 771 | Airport airport1 = new Airport(44, 120, 100); 772 | Airport airport2 = new Airport(50, 112, 110); 773 | boolean status; 774 | 775 | FlightReservation reservation1 = new FlightReservation("Gon", airport1, airport2); 776 | FlightReservation reservation2 = new FlightReservation("Killua", airport1, airport2); 777 | 778 | basket1.add(reservation1); 779 | basket1.add(reservation1); 780 | basket1.add(reservation1); 781 | basket1.add(reservation2); 782 | // basket1 == {reservation1, reservation2, reservation1, reservation1, . . .} 783 | basket1.remove(reservation2); 784 | // basket1 == {reservation2, reservation1, reservation1, . . .} AND NOT {reservation2, . . .} 785 | 786 | status = (basket1.getProducts()[0].equals(reservation1) && basket1.getProducts()[1].equals(reservation1) && basket1.getProducts()[2].equals(reservation1)); 787 | 788 | assertTrue(status, "Basket: remove() didn't remove the FIRST reservation input"); 789 | } 790 | 791 | @Test 792 | @Tag("score:1") @DisplayName("Basket remove() Test4") 793 | void remove4() { 794 | Basket basket1 = new Basket(); 795 | Airport airport1 = new Airport(44, 120, 100); 796 | Airport airport2 = new Airport(50, 112, 110); 797 | boolean status; 798 | 799 | FlightReservation reservation1 = new FlightReservation("Gon", airport1, airport2); 800 | FlightReservation reservation2 = new FlightReservation("Killua", airport1, airport2); 801 | 802 | basket1.add(reservation2); 803 | basket1.add(reservation1); 804 | basket1.add(reservation1); 805 | basket1.add(reservation1); 806 | // basket1 == {reservation1, reservation2, reservation1, reservation1, . . .} 807 | basket1.remove(reservation2); 808 | // basket1 == {reservation2, reservation1, reservation1, . . .} AND NOT {reservation2, . . .} 809 | 810 | status = (basket1.getProducts()[0].equals(reservation1) && basket1.getProducts()[1].equals(reservation1) && basket1.getProducts()[2].equals(reservation1)); 811 | 812 | assertTrue(status, "Basket: remove() didn't remove the FIRST reservation input"); 813 | } 814 | 815 | @Test 816 | @Tag("score:1") @DisplayName("Basket clear() Test1") 817 | void clear() { 818 | Basket basket1 = new Basket(); 819 | Reservation reservation1 = new BasketTest.FakeReservation("Alex"); 820 | basket1.add(reservation1); 821 | basket1.add(reservation1); 822 | basket1.clear(); 823 | assertEquals(0, basket1.getNumOfReservations(), 824 | "Basket: clear() doesn't clear the array of reservations in the basket"); 825 | } 826 | 827 | 828 | @Test 829 | @Tag("score:1") @DisplayName("Basket getTotalCost Test1") 830 | void getTotalCost() { 831 | Basket basket1 = new Basket(); 832 | Airport airport1 = new Airport(44, 120, 100); 833 | Airport airport2 = new Airport(50, 112, 110); 834 | FlightReservation flightReservation1 = new FlightReservation("Alex", airport1, airport2); 835 | basket1.add(flightReservation1); 836 | basket1.add(flightReservation1); 837 | 838 | assertEquals(11186, basket1.getTotalCost(), 839 | "Basket: getTotalCost() returns the wrong cost."); 840 | } 841 | 842 | private static class FakeReservation extends Reservation { 843 | 844 | public FakeReservation(String name) { 845 | super(name); 846 | } 847 | 848 | @Override 849 | public int getCost() { 850 | return 0; 851 | } 852 | 853 | @Override 854 | public boolean equals(Object o) { 855 | return false; 856 | } 857 | } 858 | } -------------------------------------------------------------------------------- /Assignment2/Minitester.java: -------------------------------------------------------------------------------- 1 | package assignment2; 2 | 3 | import org.junit.jupiter.api.*; 4 | import java.lang.reflect.Field; 5 | import java.util.NoSuchElementException; 6 | import static org.junit.jupiter.api.Assertions.*; 7 | 8 | public class Minitester {} 9 | 10 | class SyntaxTest { 11 | 12 | @Test 13 | @Tag("score:0") 14 | @DisplayName("Syntax Test for MyList") 15 | void syntaxTestMyList() throws NoSuchMethodException { 16 | 17 | Class mList = MyList.class; 18 | //Check if MyList is an Interface 19 | assertTrue(mList.isInterface()); 20 | 21 | // Check the methods by signature 22 | // Return NoSuchMethodException if the input type or method name is not matched 23 | assertEquals("getSize", mList.getMethod("getSize").getName()); 24 | assertEquals("isEmpty", mList.getMethod("isEmpty").getName()); 25 | assertEquals("add", mList.getMethod("add", Object.class).getName()); 26 | assertEquals("clear", mList.getMethod("clear").getName()); 27 | assertEquals("remove", mList.getMethod("remove").getName()); 28 | 29 | // Check the return type of each method 30 | assertEquals("int", mList.getMethod("getSize").getReturnType().getName()); 31 | assertEquals("boolean", mList.getMethod("isEmpty").getReturnType().getName()); 32 | assertEquals("void", mList.getMethod("add", Object.class).getReturnType().getName()); 33 | assertEquals("void", mList.getMethod("clear").getReturnType().getName()); 34 | assertEquals(Object.class.getName(), mList.getMethod("remove").getReturnType().getName()); 35 | 36 | } 37 | 38 | @Test 39 | @Tag("score:0") 40 | @DisplayName("Syntax Test for MyLinkedList") 41 | void syntaxTestMyLinkedList() throws NoSuchMethodException, NoSuchFieldException { 42 | Class mLinkedList = MyLinkedList.class; 43 | 44 | // Check if we have a field named size and its type is int 45 | assertEquals("int", mLinkedList.getDeclaredField("size").getType().getName()); 46 | 47 | // Check isEmpty() 48 | assertEquals("isEmpty", mLinkedList.getMethod("isEmpty").getName()); 49 | assertEquals("boolean", mLinkedList.getMethod("isEmpty").getReturnType().getName()); 50 | 51 | // Check getSize() 52 | assertEquals("getSize", mLinkedList.getMethod("getSize").getName()); 53 | assertEquals("int", mLinkedList.getMethod("getSize").getReturnType().getName()); 54 | 55 | } 56 | 57 | @Test 58 | @Tag("score:0") 59 | @DisplayName("Syntax Test for MyDoublyLinkedList") 60 | void syntaxTestMyDoublyLinkedList() throws NoSuchMethodException, NoSuchFieldException { 61 | 62 | Class dList = MyDoublyLinkedList.class; 63 | 64 | // Check if MyDoublyLinkedList extends MyLinkedList 65 | assertEquals(MyLinkedList.class, dList.getSuperclass()); 66 | 67 | // Check add() 68 | assertEquals("add", dList.getMethod("add", Object.class).getName()); 69 | assertEquals("void", dList.getMethod("add", Object.class).getReturnType().getName()); 70 | 71 | // Check remove() 72 | assertEquals("remove", dList.getMethod("remove").getName()); 73 | assertEquals(Object.class.getName(), dList.getMethod("remove").getReturnType().getName()); 74 | 75 | // Check addFirst and addLast 76 | assertEquals("addFirst", dList.getMethod("addFirst", Object.class).getName()); 77 | assertEquals("void", dList.getMethod("addFirst", Object.class).getReturnType().getName()); 78 | assertEquals("addLast", dList.getMethod("addLast", Object.class).getName()); 79 | assertEquals("void", dList.getMethod("addLast", Object.class).getReturnType().getName()); 80 | 81 | // Check removeFirst and removeLast 82 | assertEquals("removeFirst", dList.getMethod("removeFirst").getName()); 83 | assertEquals(Object.class.getName(), dList.getMethod("removeFirst").getReturnType().getName()); 84 | assertEquals("removeLast", dList.getMethod("removeLast").getName()); 85 | assertEquals(Object.class.getName(), dList.getMethod("removeLast").getReturnType().getName()); 86 | 87 | // Check peekFirst and peekLast 88 | assertEquals("peekFirst", dList.getMethod("peekFirst").getName()); 89 | assertEquals(Object.class.getName(), dList.getMethod("peekFirst").getReturnType().getName()); 90 | assertEquals("peekLast", dList.getMethod("peekLast").getName()); 91 | assertEquals(Object.class.getName(), dList.getMethod("peekLast").getReturnType().getName()); 92 | 93 | // Check clear 94 | assertEquals("clear", dList.getMethod("clear").getName()); 95 | assertEquals("void", dList.getMethod("clear").getReturnType().getName()); 96 | 97 | // Check equals 98 | assertEquals("equals", dList.getMethod("equals", Object.class).getName()); 99 | assertEquals("boolean", dList.getMethod("equals", Object.class).getReturnType().getName()); 100 | 101 | } 102 | 103 | @Test 104 | @Tag("score:0") 105 | @DisplayName("Syntax Test for MyStack") 106 | void syntaxTestMyStack() throws NoSuchMethodException, NoSuchFieldException { 107 | Class stack = MyStack.class; 108 | 109 | // the name of the private field is not given 110 | // a constructor with no inputs shall be created by default 111 | 112 | // check push 113 | assertEquals("push", stack.getMethod("push", Object.class).getName()); 114 | assertEquals("void", stack.getMethod("push", Object.class).getReturnType().getName()); 115 | 116 | // check pop 117 | assertEquals("pop", stack.getMethod("pop").getName()); 118 | assertEquals(Object.class.getName(), stack.getMethod("pop").getReturnType().getName()); 119 | 120 | // check peek 121 | assertEquals("peek", stack.getMethod("peek").getName()); 122 | assertEquals(Object.class.getName(), stack.getMethod("peek").getReturnType().getName()); 123 | 124 | // check isEmpty 125 | assertEquals("isEmpty", stack.getMethod("isEmpty").getName()); 126 | assertEquals("boolean", stack.getMethod("isEmpty").getReturnType().getName()); 127 | 128 | // Check clear 129 | assertEquals("clear", stack.getMethod("clear").getName()); 130 | assertEquals("void", stack.getMethod("clear").getReturnType().getName()); 131 | 132 | // Check getSize 133 | assertEquals("getSize", stack.getMethod("getSize").getName()); 134 | assertEquals("int", stack.getMethod("getSize").getReturnType().getName()); 135 | 136 | } 137 | 138 | @Test 139 | @Tag("score:0") 140 | @DisplayName("Syntax Test for MyQueue") 141 | void syntaxTestMyQueue() throws NoSuchMethodException, NoSuchFieldException { 142 | Class queue = MyQueue.class; 143 | 144 | // check field 145 | boolean found = false; 146 | for (Field f : queue.getDeclaredFields()) { 147 | if (f.getType().equals(MyDoublyLinkedList.class)) { 148 | found = true; 149 | break; 150 | } 151 | } 152 | assertTrue(found); 153 | 154 | // check enqueue 155 | assertEquals("enqueue", queue.getMethod("enqueue", Object.class).getName()); 156 | assertEquals("void", queue.getMethod("enqueue", Object.class).getReturnType().getName()); 157 | 158 | // check dequeue 159 | assertEquals("dequeue", queue.getMethod("dequeue").getName()); 160 | assertEquals(Object.class.getName(), queue.getMethod("dequeue").getReturnType().getName()); 161 | 162 | // check isEmpty 163 | assertEquals("isEmpty", queue.getMethod("isEmpty").getName()); 164 | assertEquals("boolean", queue.getMethod("isEmpty").getReturnType().getName()); 165 | 166 | // Check clear 167 | assertEquals("clear", queue.getMethod("clear").getName()); 168 | assertEquals("void", queue.getMethod("clear").getReturnType().getName()); 169 | 170 | // Check equals 171 | assertEquals("equals", queue.getMethod("equals", Object.class).getName()); 172 | assertEquals("boolean", queue.getMethod("equals", Object.class).getReturnType().getName()); 173 | 174 | } 175 | 176 | @Test 177 | @Tag("score:0") 178 | @DisplayName("Syntax Test for Position") 179 | void syntaxTestPosition() throws NoSuchMethodException, NoSuchFieldException { 180 | Class position = Position.class; 181 | // check constructors 182 | assertEquals(Position.class.getName(), position.getConstructor(int.class, int.class).getName()); 183 | assertEquals(Position.class.getName(), position.getConstructor(Position.class).getName()); 184 | 185 | // Check reset(int,int) 186 | assertEquals("reset", position.getMethod("reset", int.class, int.class).getName()); 187 | assertEquals("void", position.getMethod("reset", int.class, int.class).getReturnType().getName()); 188 | 189 | // Check reset(Position) 190 | assertEquals("reset", position.getMethod("reset", Position.class).getName()); 191 | assertEquals("void", position.getMethod("reset", Position.class).getReturnType().getName()); 192 | 193 | // Check getDistance 194 | assertEquals("getDistance", position.getMethod("getDistance", Position.class, Position.class).getName()); 195 | assertEquals("int", position.getMethod("getDistance", Position.class, Position.class).getReturnType().getName()); 196 | 197 | // Check getX and getY 198 | assertEquals("getX", position.getMethod("getX").getName()); 199 | assertEquals("int", position.getMethod("getX").getReturnType().getName()); 200 | assertEquals("getY", position.getMethod("getY").getName()); 201 | assertEquals("int", position.getMethod("getY").getReturnType().getName()); 202 | 203 | // Check moveWest 204 | assertEquals("moveWest", position.getMethod("moveWest").getName()); 205 | assertEquals("void", position.getMethod("moveWest").getReturnType().getName()); 206 | 207 | // Check moveEast 208 | assertEquals("moveEast", position.getMethod("moveEast").getName()); 209 | assertEquals("void", position.getMethod("moveEast").getReturnType().getName()); 210 | 211 | // Check moveWest 212 | assertEquals("moveNorth", position.getMethod("moveNorth").getName()); 213 | assertEquals("void", position.getMethod("moveNorth").getReturnType().getName()); 214 | 215 | // Check moveWest 216 | assertEquals("moveSouth", position.getMethod("moveSouth").getName()); 217 | assertEquals("void", position.getMethod("moveSouth").getReturnType().getName()); 218 | 219 | // Check equals 220 | assertEquals("equals", position.getMethod("equals", Object.class).getName()); 221 | assertEquals("boolean", position.getMethod("equals", Object.class).getReturnType().getName()); 222 | 223 | } 224 | 225 | @Test 226 | @Tag("score:0") 227 | @DisplayName("Syntax Test for TargetQueue") 228 | void syntaxTestTargetQueue() throws NoSuchMethodException, NoSuchFieldException { 229 | 230 | Class tq = TargetQueue.class; 231 | 232 | // Check if TargetQueue extends MyQueue 233 | assertEquals(MyQueue.class, tq.getSuperclass()); 234 | 235 | // check field 236 | boolean found = false; 237 | for (Field f : tq.getDeclaredFields()) { 238 | if (f.getType().equals(MyStack.class)) { 239 | found = true; 240 | break; 241 | } 242 | } 243 | assertTrue(found); 244 | 245 | // Check clear 246 | assertEquals("clear", tq.getMethod("clear").getName()); 247 | assertEquals("void", tq.getMethod("clear").getReturnType().getName()); 248 | 249 | // Check addTargets 250 | assertEquals("addTargets", tq.getMethod("addTargets", String.class).getName()); 251 | assertEquals("void", tq.getMethod("addTargets", String.class).getReturnType().getName()); 252 | } 253 | 254 | 255 | @Test 256 | @Tag("score:0") 257 | @DisplayName("Syntax Test for Direction") 258 | void syntaxTestDirection() throws NoSuchMethodException, NoSuchFieldException { 259 | assertEquals("NORTH", Direction.NORTH.toString()); 260 | assertEquals("SOUTH", Direction.SOUTH.toString()); 261 | assertEquals("WEST", Direction.WEST.toString()); 262 | assertEquals("EAST", Direction.EAST.toString()); 263 | } 264 | 265 | 266 | @Test 267 | @Tag("score:0") 268 | @DisplayName("Syntax Test for ActionQueue") 269 | void syntaxTestActionQueue() throws NoSuchMethodException, NoSuchFieldException { 270 | 271 | Class aq = ActionQueue.class; 272 | 273 | // Check if ActionQueue extends MyQueue 274 | assertEquals(MyQueue.class, aq.getSuperclass()); 275 | 276 | // Check clear 277 | assertEquals("clear", aq.getMethod("clear").getName()); 278 | assertEquals("void", aq.getMethod("clear").getReturnType().getName()); 279 | 280 | // Check loadFromEncodedString 281 | assertEquals("loadFromEncodedString", aq.getMethod("loadFromEncodedString", String.class).getName()); 282 | assertEquals("void", aq.getMethod("loadFromEncodedString", String.class).getReturnType().getName()); 283 | 284 | } 285 | 286 | @Test 287 | @Tag("score:0") 288 | @DisplayName("Syntax Test for Region") 289 | void syntaxTestRegion() throws NoSuchMethodException, NoSuchFieldException{ 290 | Class region = Region.class; 291 | 292 | // check constructor for region 293 | assertEquals(Region.class.getName(), region.getConstructor(int.class,int.class,int.class,int.class).getName()); 294 | 295 | // Check contains 296 | assertEquals("contains", region.getMethod("contains", Position.class).getName()); 297 | assertEquals("boolean", region.getMethod("contains", Position.class).getReturnType().getName()); 298 | 299 | } 300 | 301 | @Test 302 | @Tag("score:0") 303 | @DisplayName("Syntax Test for Caterpillar") 304 | void syntaxTestCaterpillar() throws NoSuchMethodException, NoSuchFieldException{ 305 | Class caterpillar = Caterpillar.class; 306 | 307 | // Check if Caterpillar extends MyDoublyLinkedList 308 | assertEquals(MyDoublyLinkedList.class,caterpillar.getSuperclass()); 309 | 310 | // Check getHead 311 | assertEquals("getHead", caterpillar.getMethod("getHead").getName()); 312 | assertEquals(Position.class.getName(), caterpillar.getMethod("getHead").getReturnType().getName()); 313 | 314 | // Check eat 315 | assertEquals("eat", caterpillar.getMethod("eat", Position.class).getName()); 316 | assertEquals("void", caterpillar.getMethod("eat", Position.class).getReturnType().getName()); 317 | 318 | // Check move 319 | assertEquals("move", caterpillar.getMethod("move", Position.class).getName()); 320 | assertEquals("void", caterpillar.getMethod("move", Position.class).getReturnType().getName()); 321 | 322 | // Check selfCollision 323 | assertEquals("selfCollision", caterpillar.getMethod("selfCollision", Position.class).getName()); 324 | assertEquals("boolean", caterpillar.getMethod("selfCollision", Position.class).getReturnType().getName()); 325 | 326 | } 327 | 328 | @Test 329 | @Tag("score:0") 330 | @DisplayName("Syntax Test for GameState") 331 | void syntaxTestGameState() throws NoSuchMethodException, NoSuchFieldException { 332 | assertEquals("WALL_COLLISION", GameState.WALL_COLLISION.toString()); 333 | assertEquals("SELF_COLLISION", GameState.SELF_COLLISION.toString()); 334 | assertEquals("NO_MORE_ACTION", GameState.NO_MORE_ACTION.toString()); 335 | assertEquals("EAT", GameState.EAT.toString()); 336 | assertEquals("MOVE", GameState.MOVE.toString()); 337 | assertEquals("DONE", GameState.DONE.toString()); 338 | } 339 | 340 | @Test 341 | @Tag("score:0") 342 | @DisplayName("Syntax Test for World") 343 | void syntaxTestWorld() throws NoSuchMethodException, NoSuchFieldException { 344 | Class world = World.class; 345 | 346 | 347 | // check all 6 fields are present 348 | boolean foundCaterpillar = false; 349 | boolean foundPosition = false; 350 | boolean foundActionQueue = false; 351 | boolean foundTargetQueue = false; 352 | boolean foundRegion = false; 353 | boolean foundGameState = false; 354 | 355 | for (Field f : world.getDeclaredFields()) { 356 | if (f.getType().equals(Caterpillar.class)) { 357 | foundCaterpillar = true; 358 | } else if (f.getType().equals(Position.class)) { 359 | foundPosition = true; 360 | } else if (f.getType().equals(ActionQueue.class)) { 361 | foundActionQueue = true; 362 | } else if (f.getType().equals(TargetQueue.class)) { 363 | foundTargetQueue = true; 364 | } else if (f.getType().equals(Region.class)) { 365 | foundRegion = true; 366 | } else if (f.getType().equals(GameState.class)) { 367 | foundGameState = true; 368 | } else { 369 | continue; 370 | } 371 | } 372 | assertTrue(foundCaterpillar && foundPosition && foundActionQueue && foundTargetQueue && foundRegion && foundGameState); 373 | 374 | // check constructor for world 375 | assertEquals(World.class.getName(), world.getConstructor(TargetQueue.class, ActionQueue.class).getName()); 376 | 377 | // Check step 378 | assertEquals("step", world.getMethod("step").getName()); 379 | assertEquals("void", world.getMethod("step").getReturnType().getName()); 380 | 381 | // Check getState 382 | assertEquals("getState", world.getMethod("getState").getName()); 383 | assertEquals(GameState.class.getName(), world.getMethod("getState").getReturnType().getName()); 384 | 385 | // Check getCaterpillar 386 | assertEquals("getCaterpillar", world.getMethod("getCaterpillar").getName()); 387 | assertEquals(Caterpillar.class.getName(), world.getMethod("getCaterpillar").getReturnType().getName()); 388 | 389 | // Check getFood 390 | assertEquals("getFood", world.getMethod("getFood").getName()); 391 | assertEquals(Position.class.getName(), world.getMethod("getFood").getReturnType().getName()); 392 | 393 | // Check isRunning 394 | assertEquals("isRunning", world.getMethod("isRunning").getName()); 395 | assertEquals("boolean", world.getMethod("isRunning").getReturnType().getName()); 396 | 397 | } 398 | } 399 | 400 | class Part1Test { 401 | 402 | // ==================== MY DOUBLY LINKED LIST TEST =================== // 403 | @Test 404 | @Tag("score:1") 405 | @DisplayName("MyDLL add() test1") 406 | public void shouldAdd() { 407 | MyDoublyLinkedList list = new MyDoublyLinkedList<>(); 408 | 409 | list.add(2); 410 | list.addFirst(5); 411 | list.addFirst(9); 412 | list.addFirst(0); // {0, 9, 5, 2} 413 | 414 | assertEquals(4, list.getSize()); 415 | assertEquals(0, list.peekFirst()); 416 | assertEquals(2, list.peekLast()); 417 | 418 | list = new MyDoublyLinkedList<>(); 419 | list.add(2); 420 | list.add(5); 421 | list.addLast(9); 422 | list.addFirst(0); // {0, 2, 5, 9} 423 | 424 | assertEquals(4, list.getSize()); 425 | assertEquals(0, list.peekFirst()); 426 | assertEquals(9, list.peekLast()); 427 | } 428 | 429 | @Test 430 | @Tag("score:1") 431 | @DisplayName("MyDLL remove() test1") 432 | public void shouldRemove() { 433 | MyDoublyLinkedList list = new MyDoublyLinkedList<>(); 434 | 435 | list.add(1); 436 | list.add(2); 437 | list.add(3); 438 | 439 | Number removedItem = list.removeFirst(); // {2, 3} 440 | 441 | assertEquals(1, removedItem); 442 | assertEquals(2, list.peekFirst()); 443 | assertEquals(2, list.getSize()); 444 | 445 | list = new MyDoublyLinkedList<>(); 446 | list.add(1); 447 | list.add(2); 448 | list.add(3); 449 | list.addFirst(4); 450 | list.addFirst(5); // {5, 4, 1, 2, 3} 451 | 452 | removedItem = list.removeLast(); 453 | 454 | assertEquals(3, removedItem); 455 | assertEquals(2, list.peekLast()); 456 | assertEquals(4, list.getSize()); 457 | } 458 | 459 | @Test 460 | @Tag("score:1") 461 | @DisplayName("MyDLL exception handling test1") 462 | public void shouldThrowExceptionOnEmptyList() { 463 | MyDoublyLinkedList list = new MyDoublyLinkedList<>(); 464 | assertThrows(NoSuchElementException.class, () -> list.peekLast()); 465 | assertThrows(NoSuchElementException.class, () -> list.peekFirst()); 466 | } 467 | 468 | @Test 469 | @Tag("score:1") 470 | @DisplayName("MyDLL peek() test1") 471 | public void shouldPeek() { 472 | MyDoublyLinkedList list = new MyDoublyLinkedList<>(); 473 | 474 | list.addLast(1); 475 | list.add(2); 476 | list.addFirst(3); 477 | 478 | assertEquals(3, list.peekFirst()); 479 | assertEquals(2, list.peekLast()); 480 | } 481 | 482 | @Test 483 | @Tag("score:1") 484 | @DisplayName("MyDLL clear() test1") 485 | public void shouldClear() { 486 | MyDoublyLinkedList list = new MyDoublyLinkedList<>(); 487 | 488 | list.addLast(1); 489 | list.add(2); 490 | list.addFirst(3); 491 | 492 | list.clear(); 493 | 494 | assertEquals(0, list.getSize()); 495 | assertFalse(list.iterator().hasNext()); 496 | } 497 | 498 | @Test 499 | @Tag("score:1") 500 | @DisplayName("MyDLL equals() test1") 501 | public void shouldCheckEquals() { 502 | MyDoublyLinkedList list1 = new MyDoublyLinkedList<>(); 503 | MyDoublyLinkedList list2 = new MyDoublyLinkedList<>(); 504 | 505 | list1.add(1); 506 | list1.add(2); 507 | list1.add(3); 508 | 509 | list2.add(1); 510 | list2.add(2); 511 | list2.add(3); 512 | 513 | assertTrue(list1.equals(list2)); 514 | } 515 | 516 | @Test 517 | @Tag("score:1") 518 | @DisplayName("MyDLL equals() test2") 519 | public void shouldCheckNotEquals() { 520 | MyDoublyLinkedList list1 = new MyDoublyLinkedList<>(); 521 | MyDoublyLinkedList list2 = new MyDoublyLinkedList<>(); 522 | 523 | list1.add(1); 524 | list1.add(2); 525 | 526 | list2.add(2); 527 | list2.add(1); 528 | 529 | assertFalse(list1.equals(list2)); 530 | } 531 | 532 | 533 | // ==================== MYQUEUE TEST =================== // 534 | 535 | @Test 536 | @Tag("score:2") 537 | @DisplayName("MyQueue enqueue(), dequeue() and clear() test") 538 | public void shouldEnqueueAndDequeue() { 539 | MyQueue queue = new MyQueue(); 540 | 541 | queue.enqueue(1); 542 | queue.enqueue(2); 543 | queue.enqueue(3); 544 | 545 | assertFalse(queue.isEmpty()); 546 | 547 | assertEquals(queue.dequeue(), 1); 548 | assertEquals(queue.dequeue(), 2); 549 | assertEquals(queue.dequeue(), 3); 550 | 551 | assertTrue(queue.isEmpty()); 552 | 553 | queue.enqueue(1); 554 | queue.enqueue(2); 555 | queue.enqueue(3); 556 | 557 | queue.clear(); 558 | 559 | assertTrue(queue.isEmpty()); 560 | } 561 | 562 | @Test 563 | @Tag("score:1") 564 | @DisplayName("MyQueue equals() test") 565 | public void shouldCheckEqual() { 566 | MyQueue queue1 = new MyQueue(); 567 | MyQueue queue2 = new MyQueue(); 568 | 569 | queue1.enqueue(1); 570 | queue1.enqueue(2); 571 | queue1.enqueue(3); 572 | 573 | queue2.enqueue(1); 574 | queue2.enqueue(2); 575 | queue2.enqueue(3); 576 | 577 | assertTrue(queue1.equals(queue2)); 578 | } 579 | 580 | // ==================== MYSTACK TEST =================== // 581 | @Test 582 | @Tag("score:1") 583 | @DisplayName("MyStack push() and pop() test1") 584 | public void shouldPush() { 585 | MyStack stack = new MyStack(); 586 | 587 | stack.push(1); 588 | stack.push(2); 589 | stack.push(3); 590 | 591 | assertEquals(stack.getSize(), 3); 592 | assertEquals(stack.peek(), 3); 593 | 594 | Number popped = stack.pop(); 595 | 596 | assertEquals(3, popped); 597 | assertEquals(2, stack.getSize()); 598 | assertEquals(2, stack.peek()); 599 | } 600 | 601 | @Test 602 | @Tag("score:1") 603 | @DisplayName("MyStack isEmpty() test1") 604 | public void shouldReturnIsEmpty() { 605 | MyStack stack = new MyStack(); 606 | assertTrue(stack.isEmpty()); 607 | } 608 | 609 | @Test 610 | @Tag("score:1") 611 | @DisplayName("MyStack clear() test1") 612 | public void shouldReturnClear() { 613 | MyStack stack = new MyStack(); 614 | 615 | stack.push(1); 616 | stack.push(2); 617 | stack.push(3); 618 | 619 | stack.clear(); 620 | 621 | assertTrue(stack.isEmpty()); 622 | assertEquals(0, stack.getSize()); 623 | } 624 | } 625 | 626 | 627 | class Part2Test { 628 | 629 | // ==================== POSITION CLASS TEST =================== // 630 | @Test 631 | @Tag("score:1") 632 | @DisplayName("Position move() test1") 633 | void positionMoveDir1() { 634 | Position pos = new Position(7, 5); 635 | pos.moveWest(); 636 | pos.moveNorth(); 637 | 638 | assertEquals(6, pos.getX()); 639 | assertEquals(4, pos.getY()); 640 | 641 | pos = new Position(7, 5); 642 | pos.moveEast(); 643 | pos.moveSouth(); 644 | 645 | assertEquals(8, pos.getX()); 646 | assertEquals(6, pos.getY()); 647 | } 648 | 649 | @Test 650 | @Tag("score:1") 651 | @DisplayName("Position reset() test1") 652 | void positionReset1() { 653 | Position pos = new Position(7, 5); 654 | 655 | pos.reset(6, 9); 656 | 657 | assertEquals(6, pos.getX()); 658 | assertEquals(9, pos.getY()); 659 | 660 | Position pos2 = new Position(9, 6); 661 | 662 | pos.reset(pos2); 663 | assertEquals(9, pos.getX()); 664 | assertEquals(6, pos.getY()); 665 | } 666 | 667 | @Test 668 | @Tag("score:1") 669 | @DisplayName("Position getDistance() test") 670 | void positionGetDistance() { 671 | Position pos = new Position(7, 5); 672 | Position pos2 = new Position(0, 5); 673 | 674 | assertEquals(7, Position.getDistance(pos, pos2)); 675 | } 676 | 677 | @Test 678 | @Tag("score:1") 679 | @DisplayName("Position equals() test1") 680 | void positionEqual1() { 681 | Position pos = new Position(7, 5); 682 | Position pos2 = new Position(9, 6); 683 | 684 | assertFalse(pos.equals(pos2)); 685 | 686 | pos2.reset(pos); 687 | 688 | assertTrue(pos.equals(pos2)); 689 | } 690 | 691 | // Testing Basic movement functionality 692 | @Test 693 | void posMove_1(){ 694 | Position p = new Position(0,0); 695 | p.moveNorth(); 696 | assertEquals(new Position(0, -1), p); 697 | p.moveEast(); 698 | assertEquals(new Position(1, -1), p); 699 | p.moveSouth(); 700 | assertEquals(new Position(1, 0), p); 701 | p.moveWest(); 702 | assertEquals(new Position(0, 0), p); 703 | } 704 | 705 | 706 | // ==================== TARGETQUEUE CLASS TEST =================== // 707 | @Test 708 | @Tag("score:1") 709 | @DisplayName("TargetQueue addTargets() test1") 710 | void tqAddTargets1() { 711 | TargetQueue test = new TargetQueue(); 712 | assertTrue(test.isEmpty()); 713 | 714 | test.addTargets("(7,5)"); 715 | assertFalse(test.isEmpty()); 716 | 717 | Position pos = new Position(7, 5); 718 | assertEquals(pos, test.dequeue()); 719 | } 720 | 721 | @Test 722 | @Tag("score:2") 723 | @DisplayName("TargetQueue addTargets() test2") 724 | void tqAddTargets2() { 725 | TargetQueue test = new TargetQueue(); 726 | 727 | test.addTargets("(7,5).(0,5).(2,3)"); 728 | 729 | Position pos = new Position(7, 5); 730 | Position pos2 = new Position(0, 5); 731 | Position pos3 = new Position(2, 3); 732 | 733 | assertFalse(test.isEmpty()); 734 | 735 | assertEquals(pos, test.dequeue()); 736 | assertEquals(pos2, test.dequeue()); 737 | assertEquals(pos3, test.dequeue()); 738 | 739 | } 740 | 741 | @Test 742 | @Tag("score:1") 743 | @DisplayName("TargetQueue addTargets() test3") 744 | void tqAddTargets3() { 745 | TargetQueue test = new TargetQueue(); 746 | 747 | assertThrows(IllegalArgumentException.class, 748 | () -> test.addTargets("(7,5)(0,5)")); 749 | } 750 | 751 | @Test 752 | @Tag("score:1") 753 | @DisplayName("TargetQueue addTargets() test4") 754 | void tqAddTargets4() { 755 | TargetQueue test = new TargetQueue(); 756 | 757 | assertThrows(IllegalArgumentException.class, 758 | () -> test.addTargets("(7,1).(0,)")); 759 | } 760 | 761 | @Test 762 | @Tag("score:5") 763 | @DisplayName("TargetQueue addTargets() GitHub Test 1 - Killua") 764 | void tqAddTargets5() { 765 | TargetQueue test = new TargetQueue(); 766 | 767 | test.addTargets("(1,2).(3,4).(5,6)."); 768 | 769 | Position pos = new Position(1, 2); 770 | Position pos2 = new Position(3, 4); 771 | Position pos3 = new Position(5, 6); 772 | 773 | assertFalse(test.isEmpty()); 774 | 775 | assertEquals(pos, test.dequeue()); 776 | assertEquals(pos2, test.dequeue()); 777 | assertEquals(pos3, test.dequeue()); 778 | 779 | // Should not have any error even with "." at the end 780 | // https://edstem.org/us/courses/32649/discussion/2716504 781 | 782 | } 783 | 784 | @Test 785 | @Tag("score:5") 786 | @DisplayName("TargetQueue addTargets() GitHub Test 2 - Killua") 787 | void tqAddTargets6() { 788 | TargetQueue test = new TargetQueue(); 789 | 790 | test.addTargets("."); 791 | 792 | assertTrue(test.isEmpty()); 793 | 794 | // Should not have any error even with just "." as the queue should just be empty 795 | // https://edstem.org/us/courses/32649/discussion/2716504 796 | } 797 | 798 | @Test 799 | @Tag("score:5") 800 | @DisplayName("TargetQueue addTargets() GitHub Test 3 - Killua") 801 | void tqAddTargets7() { 802 | TargetQueue test = new TargetQueue(); 803 | 804 | test.addTargets(".(1,2).(3,4)"); 805 | 806 | assertFalse(test.isEmpty()); 807 | 808 | Position pos = new Position(1, 2); 809 | Position pos2 = new Position(3, 4); 810 | 811 | assertEquals(pos, test.dequeue()); 812 | assertEquals(pos2, test.dequeue()); 813 | 814 | // Should not have any error even with "." in front 815 | // https://edstem.org/us/courses/32649/discussion/2716504 816 | } 817 | 818 | @Test 819 | @Tag("score:5") 820 | @DisplayName("TargetQueue addTargets() GitHub Test 4 - Killua") 821 | void tqAddTargets8() { 822 | TargetQueue test = new TargetQueue(); 823 | 824 | assertThrows(IllegalArgumentException.class, 825 | () -> test.addTargets("(1,2)..(3,4)")); 826 | 827 | // Should throw error since there is more than one period between each position 828 | // https://edstem.org/us/courses/32649/discussion/2716504 829 | } 830 | 831 | @Test 832 | @Tag("score:5") 833 | @DisplayName("TargetQueue addTargets() GitHub Test 5 - Killua") 834 | void tqAddTargets9() { 835 | TargetQueue test = new TargetQueue(); 836 | 837 | assertThrows(IllegalArgumentException.class, 838 | () -> test.addTargets("(1, 2).(3,4)")); 839 | 840 | // Should throw error when there is a space between characters 841 | // https://edstem.org/us/courses/32649/discussion/2754324 842 | } 843 | 844 | @Test 845 | @Tag("score:1") 846 | @DisplayName("TargetQueue clear() test") 847 | void tqClear() { 848 | TargetQueue test = new TargetQueue(); 849 | 850 | test.addTargets("(7,5)"); 851 | assertFalse(test.isEmpty()); 852 | 853 | test.clear(); 854 | assertTrue(test.isEmpty()); 855 | } 856 | 857 | @Test 858 | void tqAddTargets_1(){ 859 | TargetQueue test = new TargetQueue(); 860 | test.addTargets("."); 861 | assertTrue(test.isEmpty()); 862 | } 863 | 864 | // front and back period 865 | @Test 866 | void tqAddTargets_2(){ 867 | TargetQueue test = new TargetQueue(); 868 | test.addTargets(".(0,0)."); 869 | assertEquals(test.dequeue(), new Position(0,0)); 870 | assertTrue(test.isEmpty()); 871 | } 872 | 873 | @Test 874 | void tqAddTargets_3(){ 875 | TargetQueue test = new TargetQueue(); 876 | assertThrows(IllegalArgumentException.class, 877 | () -> test.addTargets("..(0,0).")); 878 | } 879 | 880 | // Just a space as input 881 | @Test 882 | void tqAddTargets_5(){ 883 | TargetQueue test = new TargetQueue(); 884 | assertThrows(IllegalArgumentException.class, 885 | () -> test.addTargets(" ")); 886 | } 887 | 888 | // Null input 889 | @Test 890 | void tqAddTargets_6(){ 891 | TargetQueue test = new TargetQueue(); 892 | assertThrows(NullPointerException.class, 893 | () -> test.addTargets(null)); 894 | } 895 | 896 | // Letter in coordinate 897 | @Test 898 | void tqAddTargets_7(){ 899 | TargetQueue test = new TargetQueue(); 900 | assertThrows(IllegalArgumentException.class, 901 | () -> test.addTargets("(0,0).(1,1).(0,a)")); 902 | } 903 | 904 | // Multi Digit Position Input 905 | @Test 906 | void tqAddTargets_8(){ 907 | TargetQueue test = new TargetQueue(); 908 | test.addTargets("(0,0).(100,9000)"); 909 | assertEquals(test.dequeue(), new Position(0,0)); 910 | assertEquals(test.dequeue(), new Position(100,9000)); 911 | assertTrue(test.isEmpty()); 912 | } 913 | 914 | // Mad periods 915 | @Test 916 | void tqAddTargets_9(){ 917 | TargetQueue test = new TargetQueue(); 918 | assertThrows(IllegalArgumentException.class, 919 | () -> test.addTargets(".....")); 920 | } 921 | 922 | // straight up no periods 923 | @Test 924 | void tqAddTargets_10(){ 925 | TargetQueue test = new TargetQueue(); 926 | test.addTargets("(0,0)"); 927 | assertEquals(test.dequeue(), new Position(0,0)); 928 | assertTrue(test.isEmpty()); 929 | } 930 | 931 | // missing second half 932 | @Test 933 | void tqAddTargets_11(){ 934 | TargetQueue test = new TargetQueue(); 935 | assertThrows(IllegalArgumentException.class, 936 | () -> test.addTargets("(1,")); 937 | } 938 | 939 | // missing second parenthesis 940 | @Test 941 | void tqAddTargets_12(){ 942 | TargetQueue test = new TargetQueue(); 943 | assertThrows(IllegalArgumentException.class, 944 | () -> test.addTargets("(1,2")); 945 | } 946 | 947 | // just one number 948 | @Test 949 | void tqAddTargets_13(){ 950 | TargetQueue test = new TargetQueue(); 951 | assertThrows(IllegalArgumentException.class, 952 | () -> test.addTargets("(12)")); 953 | } 954 | 955 | // wierd minus sign placement 956 | @Test 957 | void tqAddTargets_15(){ 958 | TargetQueue test = new TargetQueue(); 959 | assertThrows(IllegalArgumentException.class, 960 | () -> test.addTargets("(12,1-3)")); 961 | } 962 | 963 | // wierd minus sign placement pt2 964 | @Test 965 | void tqAddTargets_16(){ 966 | TargetQueue test = new TargetQueue(); 967 | assertThrows(IllegalArgumentException.class, 968 | () -> test.addTargets("(12,13-)")); 969 | } 970 | 971 | // float coordinates 972 | @Test 973 | void tqAddTargets_17(){ 974 | TargetQueue test = new TargetQueue(); 975 | assertThrows(IllegalArgumentException.class, 976 | () -> test.addTargets("(12,1.3)")); 977 | } 978 | 979 | // empty but properly formatted I suppose? 980 | @Test 981 | void tqAddTargets_18(){ 982 | TargetQueue test = new TargetQueue(); 983 | assertThrows(IllegalArgumentException.class, 984 | () -> test.addTargets("(,)")); 985 | } 986 | 987 | // properly formatted but spaces in between (in coordinate) 988 | @Test 989 | void tqAddTargets_19(){ 990 | TargetQueue test = new TargetQueue(); 991 | assertThrows(IllegalArgumentException.class, 992 | () -> test.addTargets("(1, 3)")); 993 | } 994 | 995 | // properly formatted but spaces in between (between coordinates) 996 | @Test 997 | void tqAddTargets_20(){ 998 | TargetQueue test = new TargetQueue(); 999 | assertThrows(IllegalArgumentException.class, 1000 | () -> test.addTargets("(1,3). (1,3)")); 1001 | } 1002 | 1003 | // properly formatted with leading 0 1004 | @Test 1005 | void tqAddTargets_21(){ 1006 | TargetQueue test = new TargetQueue(); 1007 | test.addTargets("(03,05)"); 1008 | assertEquals(test.dequeue(), new Position(3,5)); 1009 | assertTrue(test.isEmpty()); 1010 | } 1011 | 1012 | // multiple . 1013 | void tqAddTargets_22(){ 1014 | TargetQueue test = new TargetQueue(); 1015 | assertThrows(IllegalArgumentException.class, 1016 | () -> test.addTargets("(12,2)..(2,212133).")); 1017 | } 1018 | 1019 | // extra ( 1020 | void tqAddTargets_23(){ 1021 | TargetQueue test = new TargetQueue(); 1022 | assertThrows(IllegalArgumentException.class, 1023 | () -> test.addTargets("((12,2).(2,212133)")); 1024 | } 1025 | 1026 | // extra ) 1027 | void tqAddTargets_24(){ 1028 | TargetQueue test = new TargetQueue(); 1029 | assertThrows(IllegalArgumentException.class, 1030 | () -> test.addTargets("(12,2)).(2,212133)")); 1031 | } 1032 | 1033 | // extra () 1034 | void tqAddTargets_25(){ 1035 | TargetQueue test = new TargetQueue(); 1036 | assertThrows(IllegalArgumentException.class, 1037 | () -> test.addTargets("((12,2)).(2,212133)")); 1038 | } 1039 | 1040 | // extra ) at the end 1041 | void tqAddTargets_26(){ 1042 | TargetQueue test = new TargetQueue(); 1043 | assertThrows(IllegalArgumentException.class, 1044 | () -> test.addTargets("(12,2).((2,212133))")); 1045 | } 1046 | 1047 | //extra () wrapping string 1048 | void tqAddTargets_27(){ 1049 | TargetQueue test = new TargetQueue(); 1050 | assertThrows(IllegalArgumentException.class, 1051 | () -> test.addTargets("((12,2).(2,212133))")); 1052 | } 1053 | 1054 | // Testing clear function 1055 | @Test 1056 | void tqClear_1(){ 1057 | TargetQueue test1 = new TargetQueue(); 1058 | TargetQueue test2 = new TargetQueue(); 1059 | test1.addTargets("(0,0).(1,1).(0,3)"); 1060 | test1.clear(); 1061 | assertTrue(test1.isEmpty()); 1062 | assertEquals(test1, test2); 1063 | } 1064 | 1065 | // ==================== ACTIONQUEUE CLASS TEST =================== // 1066 | @Test 1067 | @Tag("score:1") 1068 | @DisplayName("ActionQueue loadFromEncodedString() test1") 1069 | void aqLoadFromEncodedString1() { 1070 | ActionQueue test = new ActionQueue(); 1071 | 1072 | test.loadFromEncodedString("3[E]"); // {East, East, East} 1073 | for (int i = 0; i < 3; i++) { 1074 | assertEquals(Direction.EAST, test.dequeue()); 1075 | } 1076 | } 1077 | 1078 | @Test 1079 | @Tag("score:1") 1080 | @DisplayName("ActionQueue loadFromEncodedString() test2") 1081 | void aqLoadFromEncodedString2() { 1082 | ActionQueue test = new ActionQueue(); 1083 | 1084 | test.loadFromEncodedString("3[N]2[S]1[W]"); // { North, North, North, South, South, West } 1085 | for (int i = 0; i < 6; i++) { 1086 | if (i < 3){ 1087 | assertEquals(Direction.NORTH, test.dequeue()); 1088 | } else if (i < 5) { 1089 | assertEquals(Direction.SOUTH, test.dequeue()); 1090 | } else { 1091 | assertEquals(Direction.WEST, test.dequeue()); 1092 | } 1093 | } 1094 | } 1095 | 1096 | @Test 1097 | @Tag("score:1") 1098 | @DisplayName("ActionQueue loadFromEncodedString() test3") 1099 | void aqLoadFromEncodedString3() { 1100 | ActionQueue test = new ActionQueue(); 1101 | 1102 | test.loadFromEncodedString("3[SW]"); 1103 | // {South, West, South, West, South, West} 1104 | 1105 | for (int i = 0; i < 3; i++) { 1106 | assertEquals(Direction.SOUTH, test.dequeue()); 1107 | assertEquals(Direction.WEST, test.dequeue()); 1108 | } 1109 | } 1110 | 1111 | @Test 1112 | @Tag("score:1") 1113 | @DisplayName("ActionQueue loadFromEncodedString() test4") 1114 | void aqLoadFromEncodedString4() { 1115 | ActionQueue test = new ActionQueue(); 1116 | 1117 | assertThrows(IllegalArgumentException.class, 1118 | () -> test.loadFromEncodedString("2S[W]")); 1119 | } 1120 | 1121 | @Test 1122 | @Tag("score:1") 1123 | @DisplayName("ActionQueue clear() test") 1124 | void aqClear() { 1125 | ActionQueue test = new ActionQueue(); 1126 | 1127 | test.loadFromEncodedString("3[E]"); 1128 | assertFalse(test.isEmpty()); 1129 | 1130 | test.clear(); 1131 | assertTrue(test.isEmpty()); 1132 | } 1133 | 1134 | // Case #1 on A2 Doc 1135 | @Test 1136 | void aqLoadFromEncodedString_1(){ 1137 | ActionQueue test = new ActionQueue(); 1138 | test.loadFromEncodedString("3[N]"); // NNN 1139 | 1140 | assertEquals(Direction.NORTH, test.dequeue()); 1141 | assertEquals(Direction.NORTH, test.dequeue()); 1142 | assertEquals(Direction.NORTH, test.dequeue()); 1143 | 1144 | assertTrue(test.isEmpty()); 1145 | } 1146 | 1147 | // Case #2 on A2 Doc 1148 | @Test 1149 | void aqLoadFromEncodedString_2(){ 1150 | ActionQueue test = new ActionQueue(); 1151 | test.loadFromEncodedString("3[NE]"); // NENENE 1152 | 1153 | assertEquals(Direction.NORTH, test.dequeue()); 1154 | assertEquals(Direction.EAST, test.dequeue()); 1155 | assertEquals(Direction.NORTH, test.dequeue()); 1156 | assertEquals(Direction.EAST, test.dequeue()); 1157 | assertEquals(Direction.NORTH, test.dequeue()); 1158 | assertEquals(Direction.EAST, test.dequeue()); 1159 | 1160 | assertTrue(test.isEmpty()); 1161 | } 1162 | 1163 | // Case #3 on A2 Doc 1164 | @Test 1165 | void aqLoadFromEncodedString_3(){ 1166 | ActionQueue test = new ActionQueue(); 1167 | test.loadFromEncodedString("3[2[N]2[E]]1[S]"); // NNEENNEENNEES 1168 | 1169 | assertEquals(Direction.NORTH, test.dequeue()); 1170 | assertEquals(Direction.NORTH, test.dequeue()); 1171 | assertEquals(Direction.EAST, test.dequeue()); 1172 | assertEquals(Direction.EAST, test.dequeue()); 1173 | assertEquals(Direction.NORTH, test.dequeue()); 1174 | assertEquals(Direction.NORTH, test.dequeue()); 1175 | assertEquals(Direction.EAST, test.dequeue()); 1176 | assertEquals(Direction.EAST, test.dequeue()); 1177 | assertEquals(Direction.NORTH, test.dequeue()); 1178 | assertEquals(Direction.NORTH, test.dequeue()); 1179 | assertEquals(Direction.EAST, test.dequeue()); 1180 | assertEquals(Direction.EAST, test.dequeue()); 1181 | assertEquals(Direction.SOUTH, test.dequeue()); 1182 | 1183 | assertTrue(test.isEmpty()); 1184 | } 1185 | 1186 | // Case #4 on A2 Doc 1187 | @Test 1188 | void aqLoadFromEncodedString_4(){ 1189 | ActionQueue test = new ActionQueue(); 1190 | assertThrows(IllegalArgumentException.class, 1191 | () -> test.loadFromEncodedString("2E[N]")); 1192 | } 1193 | 1194 | // Case #5 on A2 Doc 1195 | @Test 1196 | void aqLoadFromEncodedString_5(){ 1197 | ActionQueue test = new ActionQueue(); 1198 | assertThrows(IllegalArgumentException.class, 1199 | () -> test.loadFromEncodedString("EN]")); 1200 | } 1201 | 1202 | // Case #6 on A2 Doc 1203 | @Test 1204 | void aqLoadFromEncodedString_6(){ 1205 | ActionQueue test = new ActionQueue(); 1206 | assertThrows(IllegalArgumentException.class, 1207 | () -> test.loadFromEncodedString("E[EN]")); 1208 | } 1209 | 1210 | // Case #7 on A2 Doc 1211 | @Test 1212 | void aqLoadFromEncodedString_7(){ 1213 | ActionQueue test = new ActionQueue(); 1214 | assertThrows(IllegalArgumentException.class, 1215 | () -> test.loadFromEncodedString("A")); 1216 | } 1217 | 1218 | // No Brackets 1219 | @Test 1220 | void aqLoadFromEncodedString_8(){ 1221 | ActionQueue test = new ActionQueue(); 1222 | test.loadFromEncodedString("EN"); // NNN 1223 | 1224 | assertEquals(Direction.EAST, test.dequeue()); 1225 | assertEquals(Direction.NORTH, test.dequeue()); 1226 | 1227 | assertTrue(test.isEmpty()); 1228 | } 1229 | 1230 | // Wierd Space 1231 | @Test 1232 | void aqLoadFromEncodedString_9(){ 1233 | ActionQueue test = new ActionQueue(); 1234 | assertThrows(IllegalArgumentException.class, 1235 | () -> test.loadFromEncodedString("[ N]")); 1236 | } 1237 | 1238 | // k = 0 1239 | @Test 1240 | void aqLoadFromEncodedString_10(){ 1241 | ActionQueue test = new ActionQueue(); 1242 | test.loadFromEncodedString("0[ESSN]"); 1243 | 1244 | assertTrue(test.isEmpty()); 1245 | } 1246 | 1247 | // Empty Brackets 1248 | @Test 1249 | void aqLoadFromEncodedString_11(){ 1250 | ActionQueue test = new ActionQueue(); 1251 | assertThrows(IllegalArgumentException.class, 1252 | () -> test.loadFromEncodedString("3[]")); 1253 | } 1254 | 1255 | // Ed post 'using first inductive clause' 1256 | @Test 1257 | void aqLoadFromEncodedString_12(){ 1258 | ActionQueue test = new ActionQueue(); 1259 | test.loadFromEncodedString("NE5[N]"); // NENNNNN 1260 | 1261 | assertEquals(Direction.NORTH, test.dequeue()); 1262 | assertEquals(Direction.EAST, test.dequeue()); 1263 | assertEquals(Direction.NORTH, test.dequeue()); 1264 | assertEquals(Direction.NORTH, test.dequeue()); 1265 | assertEquals(Direction.NORTH, test.dequeue()); 1266 | assertEquals(Direction.NORTH, test.dequeue()); 1267 | assertEquals(Direction.NORTH, test.dequeue()); 1268 | 1269 | assertTrue(test.isEmpty()); 1270 | } 1271 | 1272 | // Ed post 'using first second clause' 1273 | @Test 1274 | void aqLoadFromEncodedString_13(){ 1275 | ActionQueue test = new ActionQueue(); 1276 | test.loadFromEncodedString("2[NE5[N]]"); // NENNNNNNENNNNN 1277 | 1278 | assertEquals(Direction.NORTH, test.dequeue()); 1279 | assertEquals(Direction.EAST, test.dequeue()); 1280 | assertEquals(Direction.NORTH, test.dequeue()); 1281 | assertEquals(Direction.NORTH, test.dequeue()); 1282 | assertEquals(Direction.NORTH, test.dequeue()); 1283 | assertEquals(Direction.NORTH, test.dequeue()); 1284 | assertEquals(Direction.NORTH, test.dequeue()); 1285 | assertEquals(Direction.NORTH, test.dequeue()); 1286 | assertEquals(Direction.EAST, test.dequeue()); 1287 | assertEquals(Direction.NORTH, test.dequeue()); 1288 | assertEquals(Direction.NORTH, test.dequeue()); 1289 | assertEquals(Direction.NORTH, test.dequeue()); 1290 | assertEquals(Direction.NORTH, test.dequeue()); 1291 | assertEquals(Direction.NORTH, test.dequeue()); 1292 | 1293 | assertTrue(test.isEmpty()); 1294 | } 1295 | 1296 | // Doubly nested 1297 | @Test 1298 | void aqLoadFromEncodedString_14(){ 1299 | ActionQueue test = new ActionQueue(); 1300 | test.loadFromEncodedString("2[3[2[N]2[E]]2[W]]"); // NNEENNEENNEEWWNNEENNEENNEEWW 1301 | 1302 | assertEquals(Direction.NORTH, test.dequeue()); 1303 | assertEquals(Direction.NORTH, test.dequeue()); 1304 | assertEquals(Direction.EAST, test.dequeue()); 1305 | assertEquals(Direction.EAST, test.dequeue()); 1306 | assertEquals(Direction.NORTH, test.dequeue()); 1307 | assertEquals(Direction.NORTH, test.dequeue()); 1308 | assertEquals(Direction.EAST, test.dequeue()); 1309 | assertEquals(Direction.EAST, test.dequeue()); 1310 | assertEquals(Direction.NORTH, test.dequeue()); 1311 | assertEquals(Direction.NORTH, test.dequeue()); 1312 | assertEquals(Direction.EAST, test.dequeue()); 1313 | assertEquals(Direction.EAST, test.dequeue()); 1314 | assertEquals(Direction.WEST, test.dequeue()); 1315 | assertEquals(Direction.WEST, test.dequeue()); 1316 | assertEquals(Direction.NORTH, test.dequeue()); 1317 | assertEquals(Direction.NORTH, test.dequeue()); 1318 | assertEquals(Direction.EAST, test.dequeue()); 1319 | assertEquals(Direction.EAST, test.dequeue()); 1320 | assertEquals(Direction.NORTH, test.dequeue()); 1321 | assertEquals(Direction.NORTH, test.dequeue()); 1322 | assertEquals(Direction.EAST, test.dequeue()); 1323 | assertEquals(Direction.EAST, test.dequeue()); 1324 | assertEquals(Direction.NORTH, test.dequeue()); 1325 | assertEquals(Direction.NORTH, test.dequeue()); 1326 | assertEquals(Direction.EAST, test.dequeue()); 1327 | assertEquals(Direction.EAST, test.dequeue()); 1328 | assertEquals(Direction.WEST, test.dequeue()); 1329 | assertEquals(Direction.WEST, test.dequeue()); 1330 | 1331 | assertTrue(test.isEmpty()); 1332 | } 1333 | 1334 | // Double bracket 1335 | @Test 1336 | void aqLoadFromEncodedString_15(){ 1337 | ActionQueue test = new ActionQueue(); 1338 | assertThrows(IllegalArgumentException.class, 1339 | () -> test.loadFromEncodedString("3[[N]]")); 1340 | } 1341 | 1342 | // Check negative K with action Queue 1343 | @Test 1344 | void aqLoadFromEncodedString_16(){ 1345 | ActionQueue test = new ActionQueue(); 1346 | assertThrows(IllegalArgumentException.class, 1347 | () -> test.loadFromEncodedString("-3[N]")); 1348 | } 1349 | 1350 | // Test clear 1351 | @Test 1352 | void aqClear_1(){ 1353 | ActionQueue test = new ActionQueue(); 1354 | test.loadFromEncodedString("EEEEENNNNNSSS"); 1355 | test.clear(); 1356 | assertTrue(test.isEmpty()); 1357 | } 1358 | } 1359 | 1360 | class Part3Test { 1361 | 1362 | // ==================== REGION CLASS TEST =================== // 1363 | @Test 1364 | @Tag("score:1") 1365 | @DisplayName("Region contains() test1") 1366 | void regionContainsTest1() { 1367 | Region region = new Region(0,0,5,5); 1368 | Position goodPos = new Position(2,4); 1369 | assertTrue(region.contains(goodPos)); 1370 | } 1371 | 1372 | // Strictly Positive Region 1373 | @Test 1374 | void regionContains_1(){ 1375 | Region r = new Region(1, 1, 11, 11); 1376 | 1377 | // Contains in bounds of x and y 1378 | assertTrue(r.contains(new Position(5, 7))); 1379 | 1380 | // Contains in bounds of x (y is over) 1381 | assertFalse(r.contains(new Position(6, 16))); 1382 | 1383 | // Contains in bounds of y (x is over) 1384 | assertFalse(r.contains(new Position(12, 7))); 1385 | 1386 | // Contains in bounds of x (y is under) 1387 | assertFalse(r.contains(new Position(5, 0))); 1388 | 1389 | // Contains in bounds of y (x is under) 1390 | assertFalse(r.contains(new Position(-14, 7))); 1391 | 1392 | // Contains x and y out of bounds (over) 1393 | assertFalse(r.contains(new Position(14, 27))); 1394 | 1395 | // Contains x and y out of bounds (under) 1396 | assertFalse(r.contains(new Position(-14, -7))); 1397 | } 1398 | 1399 | // Inclusive Test 1400 | @Test 1401 | void regionContains_2(){ 1402 | Region r = new Region(1, 1, 11, 11); 1403 | 1404 | // on x (min) 1405 | assertTrue(r.contains(new Position(1, 5))); 1406 | // on y (min) 1407 | assertTrue(r.contains(new Position(5, 1))); 1408 | // on x and y (min) 1409 | assertTrue(r.contains(new Position(1, 1))); 1410 | 1411 | // on x (max) 1412 | assertTrue(r.contains(new Position(11, 7))); 1413 | // on y (max) 1414 | assertTrue(r.contains(new Position(5, 11))); 1415 | // on x and y (max) 1416 | assertTrue(r.contains(new Position(11, 11))); 1417 | } 1418 | 1419 | // ==================== CATERPILLAR CLASS TEST =================== // 1420 | @Test 1421 | @Tag("score:1") 1422 | @DisplayName("Caterpillar constructor test1") 1423 | void caterpillarConstructorTest1() { 1424 | Caterpillar caterpillar = new Caterpillar(); 1425 | Position startPos = new Position(7, 7); 1426 | assertEquals(caterpillar.peekFirst(), startPos); 1427 | assertEquals(1, caterpillar.getSize()); 1428 | assertEquals(startPos, caterpillar.getHead()); 1429 | } 1430 | 1431 | @Test 1432 | @Tag("score:1") 1433 | @DisplayName("Caterpillar eat() test1") 1434 | void caterpillarEatTest1() { 1435 | Caterpillar caterpillar = new Caterpillar(); 1436 | Position adjPos = new Position(8, 7); 1437 | 1438 | caterpillar.eat(adjPos); 1439 | 1440 | assertEquals(adjPos, caterpillar.peekFirst()); 1441 | assertEquals(2, caterpillar.getSize()); 1442 | } 1443 | 1444 | @Test 1445 | @Tag("score:1") 1446 | @DisplayName("Caterpillar move() test1") 1447 | void caterpillarMoveTest1() { 1448 | Caterpillar caterpillar = new Caterpillar(); 1449 | Position adjPos = new Position(8, 7); 1450 | 1451 | caterpillar.move(adjPos); 1452 | 1453 | assertEquals(adjPos, caterpillar.peekFirst()); 1454 | assertEquals(1, caterpillar.getSize()); 1455 | } 1456 | 1457 | @Test 1458 | @Tag("score:1") 1459 | @DisplayName("Caterpillar selfCollision() test1") 1460 | void caterpillarSelfCollision1() { 1461 | Caterpillar caterpillar = new Caterpillar(); 1462 | assertFalse(caterpillar.selfCollision(new Position(7, 9))); 1463 | } 1464 | 1465 | // ==================== WORLD CLASS TEST =================== // 1466 | @Test // tests if all required private fields are not null when calling constructor 1467 | @Tag("score:1") 1468 | @DisplayName("World constructor test1") 1469 | void worldConstructorTest1() throws IllegalAccessException { 1470 | TargetQueue targetQueue = new TargetQueue(); 1471 | ActionQueue actionQueue = new ActionQueue(); 1472 | 1473 | String food = "(9,9)"; 1474 | targetQueue.addTargets(food); 1475 | 1476 | World world = new World(targetQueue, actionQueue); 1477 | 1478 | Field[] privateWorldFields = World.class.getDeclaredFields(); 1479 | for (Field privateField : privateWorldFields) { 1480 | privateField.setAccessible(true); 1481 | Object value = privateField.get(world); 1482 | assertNotNull(value); 1483 | } 1484 | } 1485 | 1486 | @Test 1487 | @Tag("score:1") 1488 | @DisplayName("World step() test1") 1489 | void worldStepTest1() { 1490 | TargetQueue targetQueue = new TargetQueue(); 1491 | ActionQueue actionQueue = new ActionQueue(); 1492 | 1493 | String food = "(7,10)"; 1494 | String direction = "2[N]"; 1495 | 1496 | targetQueue.addTargets(food); 1497 | actionQueue.loadFromEncodedString(direction); 1498 | 1499 | World world = new World(targetQueue, actionQueue); 1500 | 1501 | world.step(); // move 1 step N from (7,7) to (7,8) 1502 | 1503 | assertEquals(GameState.MOVE, world.getState()); 1504 | assertEquals(1, world.getCaterpillar().getSize()); 1505 | assertEquals(new Position(7, 6), world.getCaterpillar().getHead()); 1506 | } 1507 | 1508 | @Test 1509 | @Tag("score:1") 1510 | @DisplayName("World step() test2") 1511 | void worldStepTest2() { 1512 | TargetQueue targetQueue = new TargetQueue(); 1513 | ActionQueue actionQueue = new ActionQueue(); 1514 | 1515 | String food = "(5,9)"; 1516 | String direction = "10[E]"; 1517 | 1518 | actionQueue.loadFromEncodedString(direction); 1519 | targetQueue.addTargets(food); 1520 | World world = new World(targetQueue, actionQueue); 1521 | 1522 | //move 9 steps E, wall collision 1523 | for (int i = 0; i < 9; i++) { // move 9 steps E from (7,7) to (15,7) 1524 | world.step(); 1525 | } 1526 | 1527 | assertEquals(GameState.WALL_COLLISION, world.getState()); 1528 | assertEquals(new Position(15, 7), world.getCaterpillar().getHead()); 1529 | } 1530 | @Test 1531 | @Tag("score:2") 1532 | @DisplayName("World step() test3") 1533 | void worldStepTest3() throws Exception { 1534 | TargetQueue targetQueue = new TargetQueue(); 1535 | ActionQueue actionQueue = new ActionQueue(); 1536 | 1537 | String food = "(9,9).(14,7).(7,10)"; 1538 | String direction = "2[S]2[E]" ; 1539 | 1540 | targetQueue.addTargets(food); 1541 | actionQueue.loadFromEncodedString(direction); 1542 | 1543 | World world = new World(targetQueue, actionQueue); 1544 | 1545 | for (int i = 0; i < 4; i++) { // move 4 steps S from (7,7) to (7,3) 1546 | world.step(); 1547 | } 1548 | 1549 | assertEquals(GameState.EAT, world.getState()); 1550 | assertEquals(2, world.getCaterpillar().getSize()); 1551 | assertEquals(new Position(9, 9), world.getCaterpillar().getHead()); 1552 | assertFalse(targetQueue.isEmpty()); 1553 | assertTrue(actionQueue.isEmpty()); 1554 | 1555 | } 1556 | 1557 | @Test 1558 | @Tag("score:1") 1559 | @DisplayName("World getters test1") 1560 | void worldGetStateTest1() { 1561 | TargetQueue targetQueue = new TargetQueue(); 1562 | ActionQueue actionQueue = new ActionQueue(); 1563 | 1564 | String str_target_pos = "(9,9)"; 1565 | String str_encoded = "2[E]" ; 1566 | 1567 | actionQueue.loadFromEncodedString(str_encoded); 1568 | targetQueue.addTargets(str_target_pos); 1569 | 1570 | World world = new World(targetQueue, actionQueue); 1571 | 1572 | GameState state = GameState.MOVE; 1573 | assertEquals(world.getState(),state); 1574 | 1575 | Caterpillar caterpillar = new Caterpillar(); 1576 | assertEquals(world.getCaterpillar(), caterpillar); 1577 | 1578 | Position pos = new Position(9,9); 1579 | assertEquals(world.getFood(), pos); 1580 | } 1581 | @Test 1582 | @Tag("score:1") 1583 | @DisplayName("World isRunning() test1") 1584 | void worldIsRunningTest1() { 1585 | TargetQueue targetQueue = new TargetQueue(); 1586 | ActionQueue actionQueue = new ActionQueue(); 1587 | 1588 | String str_target_pos = "(9,9)"; 1589 | targetQueue.addTargets(str_target_pos); 1590 | World world = new World(targetQueue, actionQueue); 1591 | 1592 | world.step(); 1593 | 1594 | assertEquals(world.getState(), GameState.NO_MORE_ACTION); 1595 | assertFalse(world.isRunning()); 1596 | 1597 | } 1598 | } 1599 | 1600 | 1601 | -------------------------------------------------------------------------------- /Assignment3/Tester.java: -------------------------------------------------------------------------------- 1 | package assignment3; 2 | 3 | import org.junit.jupiter.api.DisplayName; 4 | import org.junit.jupiter.api.Tag; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import java.awt.*; 8 | import java.lang.reflect.Field; 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | import java.util.Random; 12 | 13 | import static org.junit.jupiter.api.Assertions.*; 14 | 15 | class Part1Test { // ======= 12 points ======= 16 | @Test 17 | @Tag("score:1") 18 | @DisplayName("Block constructor test1") 19 | void BlockConstructorTest1() throws NoSuchFieldException, IllegalAccessException { 20 | Block.gen = new Random(50); 21 | Block b = new Block(0, 2); 22 | 23 | Field childrenField = Block.class.getDeclaredField("children"); 24 | childrenField.setAccessible(true); 25 | 26 | Block[] children = (Block[]) childrenField.get(b); 27 | 28 | assertEquals(4, children.length); 29 | 30 | Block[] urChildren = (Block[]) childrenField.get(children[0]); 31 | Block[] ulChildren = (Block[]) childrenField.get(children[1]); 32 | Block[] llChildren = (Block[]) childrenField.get(children[2]); 33 | Block[] lrChildren = (Block[]) childrenField.get(children[3]); 34 | 35 | assertEquals(4, urChildren.length); 36 | assertEquals(0, ulChildren.length); 37 | assertEquals(0, llChildren.length); 38 | assertEquals(4, lrChildren.length); 39 | 40 | } 41 | 42 | @Test 43 | @Tag("score:3") 44 | @DisplayName("Block constructor test2") 45 | void BlockConstructorTest2() throws NoSuchFieldException, IllegalAccessException { 46 | Block.gen = new Random(4); 47 | Block b = new Block(0, 2); 48 | 49 | Field childrenField = Block.class.getDeclaredField("children"); 50 | Field colorField = Block.class.getDeclaredField("color"); 51 | childrenField.setAccessible(true); 52 | colorField.setAccessible(true); 53 | 54 | Block[] children = (Block[]) childrenField.get(b); 55 | 56 | ArrayList expectedColor = new ArrayList<>(); 57 | expectedColor.add(GameColors.YELLOW); 58 | expectedColor.add(null); 59 | expectedColor.add(GameColors.YELLOW); 60 | expectedColor.add(GameColors.RED); 61 | 62 | ArrayList actualColor = new ArrayList<>(); 63 | 64 | for (Block child : children) { 65 | actualColor.add((Color) colorField.get(child)); 66 | } 67 | 68 | assertEquals(expectedColor, actualColor); // checking if the colors at level 1 are correct 69 | 70 | Block[] ulChildren = (Block[]) childrenField.get(children[1]); 71 | 72 | assertEquals(4, ulChildren.length); 73 | 74 | List expectedColorUL = List.of(GameColors.GREEN, GameColors.RED, GameColors.GREEN, GameColors.RED); 75 | List actualColorUL = new ArrayList<>(); 76 | 77 | for (Block child : ulChildren) { 78 | actualColorUL.add((Color) colorField.get(child)); 79 | } 80 | 81 | assertEquals(expectedColorUL, actualColorUL); // checking if the colors at level 2 are correct for the upper left child 82 | } 83 | 84 | 85 | // Testing for the case were lvl > maxDepth, as per the TA's answer on Ed 86 | // https://edstem.org/us/courses/32649/discussion/2880926 87 | @Test 88 | @DisplayName("Block constructor test3") 89 | void BlockConstructorTest3() { 90 | assertThrows(IllegalArgumentException.class, () -> new Block(3, 1)); 91 | } 92 | 93 | @Test 94 | @Tag("score:3") 95 | @DisplayName("Block updateSizeAndPosition() test1") 96 | void UpdateSizeAndPositionTest1() throws NoSuchFieldException, IllegalAccessException { 97 | Block[] children = new Block[4]; 98 | children[0] = new Block(0, 0, 0, 1, 2, GameColors.YELLOW, new Block[0]); 99 | children[1] = new Block(0, 0, 0, 1, 2, GameColors.RED, new Block[0]); 100 | children[2] = new Block(0, 0, 0, 1, 2, GameColors.GREEN, new Block[0]); 101 | children[3] = new Block(0, 0, 0, 1, 2, GameColors.RED, new Block[0]); 102 | Block b = new Block(0, 0, 0, 0, 2, null, children); 103 | 104 | b.updateSizeAndPosition(16, 0, 0); 105 | 106 | Field childrenField = Block.class.getDeclaredField("children"); 107 | Field sizeField = Block.class.getDeclaredField("size"); 108 | Field xcoordField = Block.class.getDeclaredField("xCoord"); 109 | Field ycoordField = Block.class.getDeclaredField("yCoord"); 110 | 111 | childrenField.setAccessible(true); 112 | sizeField.setAccessible(true); 113 | xcoordField.setAccessible(true); 114 | ycoordField.setAccessible(true); 115 | 116 | assertEquals(16, (int) sizeField.get(b)); 117 | assertEquals(0, (int) xcoordField.get(b)); 118 | assertEquals(0, (int) ycoordField.get(b)); 119 | 120 | 121 | ArrayList actualSize = new ArrayList<>(); 122 | ArrayList Coords = new ArrayList<>(); 123 | 124 | for (Block child : (Block[]) childrenField.get(b)) { 125 | actualSize.add((int) sizeField.get(child)); 126 | Coords.add((int) xcoordField.get(child)); 127 | Coords.add((int) ycoordField.get(child)); 128 | } 129 | 130 | List expectedSize = List.of(8, 8, 8, 8); 131 | List expectedCoords = List.of(8, 0, 0, 0, 0, 8, 8, 8); // UL x, UL y, UR x, UR y, LL x, LL y, LR x, LR y 132 | 133 | assertEquals(expectedSize, actualSize); 134 | assertEquals(expectedCoords, Coords); 135 | } 136 | 137 | @Test 138 | @Tag("score:1") 139 | @DisplayName("Block updateSizeAndPosition() test2") 140 | void UpdateSizeAndPositionTest2() { 141 | Block b = new Block(0, 0, 0, 0, 2, null, new Block[0]); 142 | 143 | assertThrows(IllegalArgumentException.class, () -> b.updateSizeAndPosition(-1, 0, 0)); 144 | assertThrows(IllegalArgumentException.class, () -> b.updateSizeAndPosition(3, 0, 0)); 145 | } 146 | 147 | @Test 148 | @DisplayName("Block updateSizeAndPosition() test3") 149 | void UpdateSizeAndPositionTest3() { 150 | Block b = new Block(0, 0, 0, 0, 0, null, new Block[0]); 151 | 152 | assertThrows(IllegalArgumentException.class, () -> b.updateSizeAndPosition(0, 0, 0)); 153 | } 154 | 155 | @Test 156 | @Tag("score:2") 157 | @DisplayName("Block getBlocksToDraw() test1") 158 | void GetBlocksToDrawTest1() { 159 | Block.gen = new Random(4); 160 | Block b = new Block(0, 2); 161 | b.updateSizeAndPosition(16, 0, 0); 162 | 163 | ArrayList blocksToDraw = b.getBlocksToDraw(); 164 | 165 | assertEquals(14, blocksToDraw.size()); 166 | 167 | int frameCount = 0; 168 | int blockCount = 0; 169 | 170 | for (BlockToDraw btd : blocksToDraw) { 171 | if (btd.getColor() == GameColors.FRAME_COLOR) { 172 | frameCount++; 173 | } else if (btd.getStrokeThickness() == 0) { 174 | blockCount++; 175 | } 176 | } 177 | 178 | assertEquals(7, frameCount); 179 | assertEquals(7, blockCount); 180 | } 181 | 182 | @Test 183 | @Tag("score:2") 184 | @DisplayName("Block getBlocksToDraw() test2") 185 | void GetBlocksToDrawTest2() { 186 | Block[] children = new Block[0]; 187 | Block b = new Block(0, 0, 16, 0, 2, GameColors.YELLOW, children); 188 | 189 | ArrayList blocksToDraw = b.getBlocksToDraw(); 190 | assertEquals(2, blocksToDraw.size()); 191 | 192 | for (BlockToDraw btd : blocksToDraw) { 193 | boolean frame = btd.getStrokeThickness() == 0 && btd.getColor() == GameColors.YELLOW; 194 | boolean block = btd.getStrokeThickness() == 3 && btd.getColor() == GameColors.FRAME_COLOR; 195 | assertTrue(frame || block); 196 | } 197 | } 198 | } 199 | 200 | class Part2Test { // ========= 12 points ========= 201 | @Test 202 | @Tag("score:2") 203 | @DisplayName("Block getSelectedBlock() test1") 204 | void getSelectedBlock1() { 205 | Block b = new Block(0, 0, 0, 0, 2, null, new Block[0]); 206 | 207 | assertThrows(IllegalArgumentException.class, () -> b.getSelectedBlock(2, 15, 4)); 208 | assertThrows(IllegalArgumentException.class, () -> b.getSelectedBlock(15, 2, -1)); 209 | } 210 | 211 | @Test 212 | @Tag("score:2") 213 | @DisplayName("Block getSelectedBlock() test2") 214 | void getSelectedBlock2() throws NoSuchFieldException, IllegalAccessException { 215 | Block[] children = new Block[4]; 216 | children[0] = new Block(1, 0, 1, 1, 1, GameColors.YELLOW, new Block[0]); 217 | children[1] = new Block(0, 0, 1, 1, 1, GameColors.RED, new Block[0]); 218 | children[2] = new Block(0, 1, 1, 1, 1, GameColors.GREEN, new Block[0]); 219 | children[3] = new Block(1, 1, 1, 1, 1, GameColors.BLUE, new Block[0]); 220 | 221 | Block b = new Block(0, 0, 2, 0, 1, null, children); 222 | 223 | Field xCoordField = Block.class.getDeclaredField("xCoord"); 224 | Field yCoordField = Block.class.getDeclaredField("yCoord"); 225 | Field colorField = Block.class.getDeclaredField("color"); 226 | 227 | xCoordField.setAccessible(true); 228 | yCoordField.setAccessible(true); 229 | colorField.setAccessible(true); 230 | 231 | Block res = b.getSelectedBlock(0, 0, 1); 232 | 233 | assertEquals(0, (int) xCoordField.get(res)); 234 | assertEquals(0, (int) yCoordField.get(res)); 235 | assertEquals(GameColors.RED, colorField.get(res)); 236 | } 237 | 238 | @Test 239 | @Tag("score:2") 240 | @DisplayName("Block getSelectedBlock() test3") 241 | void getSelectedBlock3() throws NoSuchFieldException, IllegalAccessException { 242 | Block.gen = new Random(4); 243 | Block b = new Block(0, 3); 244 | b.updateSizeAndPosition(16, 0, 0); 245 | 246 | Block res = b.getSelectedBlock(9, 10, 2); 247 | 248 | Field xCoordField = Block.class.getDeclaredField("xCoord"); 249 | Field yCoordField = Block.class.getDeclaredField("yCoord"); 250 | Field colorField = Block.class.getDeclaredField("color"); 251 | Field childrenField = Block.class.getDeclaredField("children"); 252 | 253 | xCoordField.setAccessible(true); 254 | yCoordField.setAccessible(true); 255 | colorField.setAccessible(true); 256 | childrenField.setAccessible(true); 257 | 258 | assertEquals(8, (int) xCoordField.get(res)); 259 | assertEquals(8, (int) yCoordField.get(res)); 260 | assertNull(colorField.get(res)); 261 | assertEquals(4, ((Block[]) childrenField.get(res)).length); 262 | 263 | List colors = List.of(GameColors.BLUE, GameColors.YELLOW, GameColors.GREEN, GameColors.YELLOW); 264 | 265 | Block[] children = (Block[]) childrenField.get(res); 266 | 267 | for (int i = 0; i < 4; i++) { 268 | Block child = children[i]; 269 | assertEquals(colors.get(i), colorField.get(child)); 270 | } 271 | } 272 | 273 | @Test 274 | @Tag("score:2") 275 | @DisplayName("Block getSelectedBlock() test4") 276 | void getSelectedBlock4() throws NoSuchFieldException, IllegalAccessException { 277 | Block.gen = new Random(4); 278 | Block b = new Block(0, 3); 279 | b.updateSizeAndPosition(16, 0, 0); 280 | 281 | Block res = b.getSelectedBlock(9, 1, 2); 282 | 283 | Field xCoordField = Block.class.getDeclaredField("xCoord"); 284 | Field yCoordField = Block.class.getDeclaredField("yCoord"); 285 | Field colorField = Block.class.getDeclaredField("color"); 286 | Field childrenField = Block.class.getDeclaredField("children"); 287 | 288 | xCoordField.setAccessible(true); 289 | yCoordField.setAccessible(true); 290 | colorField.setAccessible(true); 291 | childrenField.setAccessible(true); 292 | 293 | assertEquals(8, (int) xCoordField.get(res)); 294 | assertEquals(0, (int) yCoordField.get(res)); 295 | assertEquals(GameColors.YELLOW, colorField.get(res)); 296 | assertEquals(0, ((Block[]) childrenField.get(res)).length); 297 | } 298 | 299 | @Test 300 | @Tag("score:1") 301 | @DisplayName("Block reflect() test1") 302 | void reflect1() { 303 | Block b = new Block(0, 0, 0, 0, 2, null, new Block[0]); 304 | 305 | assertThrows(IllegalArgumentException.class, () -> b.reflect(2)); 306 | assertThrows(IllegalArgumentException.class, () -> b.reflect(-1)); 307 | } 308 | 309 | @Test 310 | @Tag("score:1") 311 | @DisplayName("Block reflect() test2") 312 | void reflect2() throws NoSuchFieldException, IllegalAccessException { 313 | Block[] children = new Block[4]; 314 | children[0] = new Block(1, 0, 1, 1, 1, GameColors.YELLOW, new Block[0]); // UR 315 | children[1] = new Block(0, 0, 1, 1, 1, GameColors.RED, new Block[0]); // UL 316 | children[2] = new Block(0, 1, 1, 1, 1, GameColors.GREEN, new Block[0]); // LL 317 | children[3] = new Block(1, 1, 1, 1, 1, GameColors.BLUE, new Block[0]); // LR 318 | Block b = new Block(0, 0, 2, 0, 1, null, children); 319 | 320 | b.reflect(0); // reflect horizontally 321 | 322 | Field childrenField = Block.class.getDeclaredField("children"); 323 | Field colorField = Block.class.getDeclaredField("color"); 324 | childrenField.setAccessible(true); 325 | colorField.setAccessible(true); 326 | 327 | Block[] childrenLevel1 = (Block[]) childrenField.get(b); 328 | 329 | List expected = List.of(GameColors.BLUE, GameColors.GREEN, GameColors.RED, GameColors.YELLOW); 330 | List actual = new ArrayList<>(); 331 | 332 | for (Block child : childrenLevel1) { 333 | actual.add((Color) colorField.get(child)); 334 | } 335 | 336 | assertEquals(expected, actual); 337 | 338 | } 339 | 340 | @Test 341 | @DisplayName("Block reflect() test3") 342 | void reflect3() throws NoSuchFieldException, IllegalAccessException { 343 | Block[] children = new Block[4]; 344 | children[0] = new Block(1, 0, 1, 1, 1, GameColors.YELLOW, new Block[0]); // UR 345 | children[1] = new Block(0, 0, 1, 1, 1, GameColors.RED, new Block[0]); // UL 346 | children[2] = new Block(0, 1, 1, 1, 1, GameColors.GREEN, new Block[0]); // LL 347 | children[3] = new Block(1, 1, 1, 1, 1, GameColors.BLUE, new Block[0]); // LR 348 | Block b = new Block(0, 0, 2, 0, 1, null, children); 349 | 350 | b.reflect(1); // reflect vertically 351 | 352 | Field childrenField = Block.class.getDeclaredField("children"); 353 | Field colorField = Block.class.getDeclaredField("color"); 354 | childrenField.setAccessible(true); 355 | colorField.setAccessible(true); 356 | 357 | Block[] childrenLevel1 = (Block[]) childrenField.get(b); 358 | 359 | List expected = List.of(GameColors.RED, GameColors.YELLOW, GameColors.BLUE, GameColors.GREEN); 360 | List actual = new ArrayList<>(); 361 | 362 | for (Block child : childrenLevel1) { 363 | actual.add((Color) colorField.get(child)); 364 | } 365 | 366 | assertEquals(expected, actual); 367 | 368 | } 369 | 370 | 371 | @Test 372 | @Tag("score:1") 373 | @DisplayName("Block rotate() test1") 374 | void rotate1() { 375 | Block b = new Block(); 376 | assertThrows(IllegalArgumentException.class, () -> b.rotate(2)); 377 | assertThrows(IllegalArgumentException.class, () -> b.rotate(-1)); 378 | } 379 | 380 | @Test 381 | @Tag("score:1") 382 | @DisplayName("Block rotate() test2") 383 | void rotate2() throws NoSuchFieldException, IllegalAccessException { 384 | Block[] children = new Block[4]; 385 | children[0] = new Block(1, 0, 1, 1, 1, GameColors.GREEN, new Block[0]); // UR 386 | children[1] = new Block(0, 0, 1, 1, 1, GameColors.BLUE, new Block[0]); // UL 387 | children[2] = new Block(0, 1, 1, 1, 1, GameColors.RED, new Block[0]); // LL 388 | children[3] = new Block(1, 1, 1, 1, 1, GameColors.BLUE, new Block[0]); // LR 389 | 390 | Block b = new Block(0, 0, 2, 0, 1, null, children); 391 | 392 | b.rotate(1); // rotate clockwise 393 | 394 | Field childrenField = Block.class.getDeclaredField("children"); 395 | Field colorField = Block.class.getDeclaredField("color"); 396 | childrenField.setAccessible(true); 397 | colorField.setAccessible(true); 398 | 399 | Block[] childrenLevel1 = (Block[]) childrenField.get(b); 400 | 401 | List expected = List.of(GameColors.BLUE, GameColors.RED, GameColors.BLUE, GameColors.GREEN); 402 | 403 | List actual = new ArrayList<>(); 404 | for (Block child : childrenLevel1) { 405 | actual.add((Color) colorField.get(child)); 406 | } 407 | 408 | assertEquals(expected, actual); 409 | } 410 | 411 | @Test 412 | @DisplayName("Block rotate() test3") 413 | void rotate3() throws NoSuchFieldException, IllegalAccessException { 414 | Block[] children = new Block[4]; 415 | children[0] = new Block(1, 0, 1, 1, 1, GameColors.GREEN, new Block[0]); // UR 416 | children[1] = new Block(0, 0, 1, 1, 1, GameColors.BLUE, new Block[0]); // UL 417 | children[2] = new Block(0, 1, 1, 1, 1, GameColors.RED, new Block[0]); // LL 418 | children[3] = new Block(1, 1, 1, 1, 1, GameColors.BLUE, new Block[0]); // LR 419 | 420 | Block b = new Block(0, 0, 2, 0, 1, null, children); 421 | 422 | b.rotate(0); // rotate counter-clockwise 423 | 424 | Field childrenField = Block.class.getDeclaredField("children"); 425 | Field colorField = Block.class.getDeclaredField("color"); 426 | childrenField.setAccessible(true); 427 | colorField.setAccessible(true); 428 | 429 | Block[] childrenLevel1 = (Block[]) childrenField.get(b); 430 | 431 | List expected = List.of(GameColors.BLUE, GameColors.GREEN, GameColors.BLUE, GameColors.RED); 432 | 433 | List actual = new ArrayList<>(); 434 | for (Block child : childrenLevel1) { 435 | actual.add((Color) colorField.get(child)); 436 | } 437 | 438 | assertEquals(expected, actual); 439 | 440 | } 441 | 442 | @Test 443 | @Tag("score:1") 444 | @DisplayName("Block smash() test1") 445 | void smash1() { 446 | Block b = new Block(); 447 | 448 | assertFalse(b.smash()); 449 | 450 | Block[] children = new Block[4]; 451 | children[0] = new Block(1, 0, 1, 1, 1, GameColors.YELLOW, new Block[0]); // UR 452 | children[1] = new Block(0, 0, 1, 1, 1, GameColors.BLUE, new Block[0]); // UL 453 | children[2] = new Block(0, 1, 1, 1, 1, GameColors.GREEN, new Block[0]); // LL 454 | children[3] = new Block(1, 1, 1, 1, 1, GameColors.BLUE, new Block[0]); // LR 455 | 456 | b = new Block(0, 0, 2, 1, 2, null, children); 457 | 458 | assertTrue(b.smash()); 459 | } 460 | 461 | @Test 462 | @Tag("score:1") 463 | @DisplayName("Block smash() test2") 464 | void smash2() throws NoSuchFieldException, IllegalAccessException { 465 | Block.gen = new Random(0); 466 | Block b = new Block(1, 2); 467 | b.updateSizeAndPosition(4, 0, 0); 468 | 469 | b.smash(); 470 | 471 | Field childrenField = Block.class.getDeclaredField("children"); 472 | Field colorField = Block.class.getDeclaredField("color"); 473 | childrenField.setAccessible(true); 474 | colorField.setAccessible(true); 475 | 476 | Block[] childrenLevel1 = (Block[]) childrenField.get(b); 477 | 478 | List expected = List.of(GameColors.BLUE, GameColors.RED, GameColors.BLUE, GameColors.YELLOW); 479 | List actual = new ArrayList<>(); 480 | 481 | for (Block child : childrenLevel1) { 482 | actual.add((Color) colorField.get(child)); 483 | } 484 | 485 | assertEquals(expected, actual); 486 | } 487 | 488 | // smash root level 489 | @Test 490 | @DisplayName("Block smash() test3") 491 | void smash3() { 492 | Block.gen = new Random(0); 493 | Block b = new Block(0, 4); 494 | b.updateSizeAndPosition(16, 0, 0); 495 | assertFalse(b.smash()); 496 | } 497 | 498 | // smash leaf 499 | @Test 500 | @DisplayName("Block smash() test4") 501 | void smash4() { 502 | Block.gen = new Random(0); 503 | Block b = new Block(4, 4); 504 | b.updateSizeAndPosition(16, 0, 0); 505 | assertFalse(b.smash()); 506 | } 507 | 508 | } 509 | 510 | class Part3Test { // ======== 16 points ======== 511 | 512 | @Test // same as the one in the pdf 513 | @Tag("score:2") 514 | @DisplayName("Block flatten() test1") 515 | void Blockflatten1() { 516 | Block.gen = new Random(2); 517 | Block b = new Block(0, 2); 518 | b.updateSizeAndPosition(16, 0, 0); 519 | 520 | Color[][] c = b.flatten(); 521 | 522 | Color[][] expected = new Color[][]{ 523 | {GameColors.RED, GameColors.RED, GameColors.GREEN, GameColors.GREEN}, 524 | {GameColors.RED, GameColors.RED, GameColors.GREEN, GameColors.GREEN}, 525 | {GameColors.YELLOW, GameColors.YELLOW, GameColors.RED, GameColors.BLUE}, 526 | {GameColors.YELLOW, GameColors.YELLOW, GameColors.YELLOW, GameColors.BLUE} 527 | }; 528 | 529 | for (int i = 0; i < 4; i++) { 530 | for (int j = 0; j < 4; j++) { 531 | assertEquals(expected[i][j], c[i][j]); 532 | } 533 | } 534 | } 535 | 536 | @Test 537 | @Tag("score:1") 538 | @DisplayName("Block flatten() test2") 539 | void Blockflatten2() { 540 | Block[] children = new Block[4]; 541 | children[0] = new Block(8, 0, 8, 1, 1, GameColors.BLUE, new Block[0]); 542 | children[1] = new Block(0, 0, 8, 1, 1, GameColors.YELLOW, new Block[0]); 543 | children[2] = new Block(0, 8, 8, 1, 1, GameColors.RED, new Block[0]); 544 | children[3] = new Block(8, 8, 8, 1, 1, GameColors.GREEN, new Block[0]); 545 | 546 | Block b = new Block(0, 0, 16, 0, 1, null, children); 547 | 548 | Color[][] c = b.flatten(); 549 | 550 | Color[][] expected = new Color[][]{ 551 | {GameColors.YELLOW, GameColors.BLUE}, 552 | {GameColors.RED, GameColors.GREEN} 553 | }; 554 | 555 | for (int i = 0; i < 2; i++) { 556 | for (int j = 0; j < 2; j++) { 557 | assertEquals(expected[i][j], c[i][j]); 558 | } 559 | } 560 | } 561 | 562 | @Test 563 | @Tag("score:3") 564 | @DisplayName("Block flatten() test3") 565 | void Blockflatten3() { 566 | Block[] children = new Block[4]; 567 | Block[] llChildren = new Block[4]; 568 | 569 | llChildren[0] = new Block(4, 8, 4, 2, 2, GameColors.RED, new Block[0]); 570 | llChildren[1] = new Block(0, 8, 4, 2, 2, GameColors.GREEN, new Block[0]); 571 | llChildren[2] = new Block(0, 12, 4, 2, 2, GameColors.GREEN, new Block[0]); 572 | llChildren[3] = new Block(4, 12, 4, 2, 2, GameColors.YELLOW, new Block[0]); 573 | 574 | children[0] = new Block(8, 0, 8, 1, 2, GameColors.RED, new Block[0]); 575 | children[1] = new Block(0, 0, 8, 1, 2, GameColors.BLUE, new Block[0]); 576 | children[2] = new Block(0, 8, 8, 1, 2, null, llChildren); 577 | children[3] = new Block(8, 8, 8, 1, 2, GameColors.YELLOW, new Block[0]); 578 | 579 | Block b = new Block(0, 0, 16, 0, 2, null, children); 580 | 581 | Color[][] c = b.flatten(); 582 | 583 | Color[][] expected = new Color[][]{ 584 | {GameColors.BLUE, GameColors.BLUE, GameColors.RED, GameColors.RED}, 585 | {GameColors.BLUE, GameColors.BLUE, GameColors.RED, GameColors.RED}, 586 | {GameColors.GREEN, GameColors.RED, GameColors.YELLOW, GameColors.YELLOW}, 587 | {GameColors.GREEN, GameColors.YELLOW, GameColors.YELLOW, GameColors.YELLOW} 588 | }; 589 | 590 | for (int i = 0; i < 4; i++) { 591 | for (int j = 0; j < 4; j++) { 592 | assertEquals(expected[i][j], c[i][j]); 593 | } 594 | } 595 | } 596 | 597 | // test for if flattened board is square 598 | @Test 599 | @DisplayName("Block flatten() test4") 600 | void Blockflatten4() { 601 | Block b = new Block(0, 10); 602 | b.updateSizeAndPosition(1024, 0, 0); 603 | Color[][] board = b.flatten(); 604 | assertEquals(board.length, board[0].length); 605 | assertEquals(1024, board.length); 606 | } 607 | 608 | // test for if flattened board is minimized to unit-cells 609 | @Test 610 | @DisplayName("Block flatten() test5") 611 | void Blockflatten5() { 612 | Block b = new Block(0, 10); 613 | b.updateSizeAndPosition(2048, 0, 0); 614 | Color[][] board = b.flatten(); 615 | assertEquals(board.length, board[0].length); 616 | assertEquals(1024, board.length); 617 | } 618 | 619 | @Test 620 | @DisplayName("Block flatten() test6") 621 | void Blockflatten6() { 622 | Block b = new Block(0, 0, 1, 0, 0, GameColors.RED, new Block[0]); 623 | b.updateSizeAndPosition(1, 0, 0); 624 | Color[][] board = b.flatten(); 625 | Color[][] expected = {{GameColors.RED}}; 626 | 627 | for (int i = 0; i < 1; i++) { 628 | for (int j = 0; j < 1; j++) { 629 | assertEquals(expected[i][j], board[i][j]); 630 | } 631 | } 632 | 633 | } 634 | 635 | @Test 636 | @Tag("score:1") 637 | @DisplayName("PerimeterGoal score() test1") 638 | void PGscore1() { 639 | Block[] children = new Block[4]; 640 | 641 | children[0] = new Block(8, 0, 8, 1, 2, GameColors.GREEN, new Block[0]); 642 | children[1] = new Block(0, 0, 8, 1, 2, GameColors.BLUE, new Block[0]); 643 | children[2] = new Block(0, 8, 8, 1, 2, GameColors.RED, new Block[0]); 644 | children[3] = new Block(8, 8, 8, 1, 2, GameColors.YELLOW, new Block[0]); 645 | 646 | Block b = new Block(0, 0, 16, 0, 2, null, children); 647 | 648 | PerimeterGoal p = new PerimeterGoal(GameColors.RED); 649 | assertEquals(4, p.score(b)); 650 | } 651 | 652 | @Test 653 | @Tag("score:1") 654 | @DisplayName("PerimeterGoal score() test2") 655 | void PGscore2() { 656 | Block.gen = new Random(8); 657 | Block b = new Block(0, 2); 658 | b.updateSizeAndPosition(16, 0, 0); 659 | 660 | PerimeterGoal p = new PerimeterGoal(GameColors.YELLOW); 661 | assertEquals(3, p.score(b)); 662 | } 663 | 664 | @Test 665 | @DisplayName("PerimeterGoal score() test3") 666 | void PGscore3() { 667 | Block.gen = new Random(10); 668 | Block b = new Block(0, 1); 669 | b.updateSizeAndPosition(16, 0, 0); 670 | 671 | PerimeterGoal p = new PerimeterGoal(GameColors.GREEN); 672 | assertEquals(4, p.score(b)); 673 | } 674 | 675 | @Test 676 | @DisplayName("PerimeterGoal score() test4") 677 | void PGscore4() { 678 | Block.gen = new Random(20); 679 | Block b = new Block(0, 1); 680 | b.updateSizeAndPosition(48, 0, 0); 681 | 682 | PerimeterGoal p = new PerimeterGoal(GameColors.YELLOW); 683 | assertEquals(4, p.score(b)); 684 | } 685 | 686 | @Test 687 | @DisplayName("PerimeterGoal score() test4") 688 | void PGscore5() { 689 | Block.gen = new Random(20); 690 | Block b = new Block(0, 1); 691 | b.updateSizeAndPosition(48, 0, 0); 692 | 693 | PerimeterGoal p = new PerimeterGoal(GameColors.GREEN); 694 | assertEquals(0, p.score(b)); 695 | } 696 | 697 | @Test 698 | @Tag("score:2") 699 | @DisplayName("BlobGoal undiscoveredBlobSize() test1") 700 | void BGBlobSize1() { 701 | BlobGoal g = new BlobGoal(GameColors.BLUE); 702 | 703 | Color[][] c = new Color[][]{ 704 | {GameColors.YELLOW, GameColors.BLUE}, 705 | {GameColors.RED, GameColors.RED} 706 | }; 707 | 708 | assertEquals(0, g.undiscoveredBlobSize(0, 0, c, new boolean[2][2])); 709 | } 710 | 711 | @Test 712 | @Tag("score:2") 713 | @DisplayName("BlobGoal undiscoveredBlobSize() test2") 714 | void BGBlobSize2() { 715 | BlobGoal g = new BlobGoal(GameColors.RED); 716 | 717 | Color[][] c = new Color[][]{ 718 | {GameColors.BLUE, GameColors.RED, GameColors.GREEN}, 719 | {GameColors.RED, GameColors.YELLOW, GameColors.RED}, 720 | {GameColors.RED, GameColors.YELLOW, GameColors.GREEN}, 721 | {GameColors.RED, GameColors.RED, GameColors.YELLOW} 722 | }; 723 | 724 | assertEquals(1, g.undiscoveredBlobSize(0, 1, c, new boolean[4][3])); 725 | } 726 | 727 | @Test 728 | @Tag("score:2") 729 | @DisplayName("BlobGoal undiscoveredBlobSize() test3") 730 | void BGBlobSize3() { 731 | Block.gen = new Random(8); 732 | Block b = new Block(0, 2); 733 | b.updateSizeAndPosition(16, 0, 0); 734 | 735 | BlobGoal g = new BlobGoal(GameColors.YELLOW); 736 | assertEquals(2, g.undiscoveredBlobSize(1, 1, b.flatten(), new boolean[4][4])); 737 | } 738 | 739 | @Test 740 | @Tag("score:1") 741 | @DisplayName("BlobGoal score() test1") 742 | void BGscore1() { 743 | Block[] children = new Block[4]; 744 | Block[] urChildren = new Block[4]; 745 | 746 | urChildren[0] = new Block(12, 0, 4, 2, 2, GameColors.GREEN, new Block[0]); 747 | urChildren[1] = new Block(8, 0, 4, 2, 2, GameColors.BLUE, new Block[0]); 748 | urChildren[2] = new Block(8, 4, 4, 2, 2, GameColors.RED, new Block[0]); 749 | urChildren[3] = new Block(12, 4, 4, 2, 2, GameColors.YELLOW, new Block[0]); 750 | 751 | children[0] = new Block(8, 0, 8, 1, 2, null, urChildren); 752 | children[1] = new Block(0, 0, 8, 1, 2, GameColors.BLUE, new Block[0]); 753 | children[2] = new Block(0, 8, 8, 1, 2, GameColors.RED, new Block[0]); 754 | children[3] = new Block(8, 8, 8, 1, 2, GameColors.YELLOW, new Block[0]); 755 | 756 | Block b = new Block(0, 0, 16, 0, 2, null, children); 757 | 758 | BlobGoal g = new BlobGoal(GameColors.BLUE); 759 | assertEquals(5, g.score(b)); 760 | } 761 | 762 | @Test 763 | @Tag("score:1") 764 | @DisplayName("BlobGoal score() test2") 765 | void BGscore2() { 766 | Block.gen = new Random(500); 767 | Block b = new Block(0, 3); 768 | b.updateSizeAndPosition(16, 0, 0); 769 | 770 | BlobGoal g = new BlobGoal(GameColors.RED); 771 | assertEquals(18, g.score(b)); 772 | } 773 | 774 | @Test 775 | @DisplayName("BlobGoal score() test3") 776 | void BGscore3() { 777 | Block.gen = new Random(10); 778 | Block b = new Block(0, 1); 779 | b.updateSizeAndPosition(16, 0, 0); 780 | 781 | BlobGoal bg = new BlobGoal(GameColors.GREEN); 782 | assertEquals(2, bg.score(b)); 783 | } 784 | 785 | @Test 786 | @DisplayName("BlobGoal score() test4") 787 | void BGscore4() { 788 | Block.gen = new Random(20); 789 | Block b = new Block(0, 1); 790 | b.updateSizeAndPosition(16, 0, 0); 791 | 792 | BlobGoal bg = new BlobGoal(GameColors.RED); 793 | assertEquals(1, bg.score(b)); 794 | } 795 | 796 | @Test 797 | @DisplayName("BlobGoal score() test5") 798 | void BGscore5() { 799 | Block.gen = new Random(20); 800 | Block b = new Block(0, 1); 801 | b.updateSizeAndPosition(16, 0, 0); 802 | 803 | BlobGoal bg = new BlobGoal(GameColors.YELLOW); 804 | assertEquals(2, bg.score(b)); 805 | } 806 | } 807 | -------------------------------------------------------------------------------- /FinalProject/StudentTests.java: -------------------------------------------------------------------------------- 1 | package finalproject; 2 | 3 | import javafx.util.Pair; 4 | import org.junit.jupiter.api.DisplayName; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import java.util.*; 8 | import java.util.function.Supplier; 9 | import java.time.*; 10 | 11 | import static org.junit.jupiter.api.Assertions.*; 12 | 13 | class HashTableTests { 14 | // default constructor; 15 | @Test 16 | @DisplayName("Constructor test 1") 17 | void constructorTest1() { 18 | MyHashTable table = new MyHashTable<>(); 19 | assertEquals(0, table.size()); 20 | assertTrue(table.isEmpty()); 21 | assertEquals(16, table.numBuckets()); 22 | assertNotNull(table.getBuckets()); 23 | } 24 | 25 | // constructor with capacity; 26 | @Test 27 | @DisplayName("Constructor test 2") 28 | void constructorTest2() { 29 | MyHashTable table = new MyHashTable<>(999); 30 | assertEquals(0, table.size()); 31 | assertTrue(table.isEmpty()); 32 | assertEquals(999, table.numBuckets()); 33 | assertNotNull(table.getBuckets()); 34 | } 35 | 36 | // normal put 37 | @Test 38 | @DisplayName("Put test 1") 39 | void putTest1() { 40 | MyHashTable table = new MyHashTable<>(); 41 | int key = 10; 42 | int val = 20; 43 | 44 | int placeAt = table.hashFunction(key); 45 | ArrayList>> buckets = table.getBuckets(); 46 | 47 | Object output = table.put(key, val); 48 | 49 | assertNotNull(buckets.get(placeAt)); 50 | assertNotNull(buckets.get(placeAt).peek()); 51 | 52 | assertEquals(key, buckets.get(placeAt).peek().getKey()); 53 | assertEquals(val, buckets.get(placeAt).peek().getValue()); 54 | 55 | assertNull(output); 56 | } 57 | 58 | // replacing a value in an existing key 59 | @Test 60 | @DisplayName("Put test 2") 61 | void putTest2() { 62 | MyHashTable table = new MyHashTable<>(); 63 | int key = 10; 64 | int val = 20; 65 | int newVal = 30; 66 | 67 | int placeAt = table.hashFunction(key); 68 | ArrayList>> buckets = table.getBuckets(); 69 | 70 | table.put(key, val); 71 | Object output = table.put(key, newVal); 72 | 73 | assertNotNull(buckets.get(placeAt)); 74 | assertNotNull(buckets.get(placeAt).peek()); 75 | 76 | assertInstanceOf(MyPair.class, buckets.get(placeAt).peek()); 77 | 78 | assertEquals(key, buckets.get(placeAt).peek().getKey()); 79 | assertEquals(newVal, buckets.get(placeAt).peek().getValue()); 80 | 81 | // check if it returns old value 82 | assertEquals(20, output); 83 | } 84 | 85 | // check for resize 86 | @Test 87 | @DisplayName("Put test 3") 88 | void putTest3() { 89 | MyHashTable table = new MyHashTable<>(2); 90 | int key1 = 10; 91 | int val1 = 20; 92 | 93 | int key2 = 3; 94 | int val2 = 5; 95 | 96 | assertEquals(2, table.numBuckets()); 97 | 98 | table.put(key1, val1); 99 | table.put(key2, val2); 100 | 101 | assertEquals(4, table.numBuckets()); 102 | assertEquals(2, table.size()); 103 | } 104 | 105 | //same hash value 106 | @Test 107 | @DisplayName("Put test 4") 108 | void putTest4() { 109 | MyHashTable table = new MyHashTable<>(); 110 | int key1 = 1; 111 | int val1 = 20; 112 | int key2 = 17; 113 | int val2 = 200; 114 | 115 | int placeAt1 = table.hashFunction(key1); 116 | int placeAt2 = table.hashFunction(key2); 117 | 118 | ArrayList>> buckets = table.getBuckets(); 119 | 120 | Object output1 = table.put(key1, val1); 121 | Object output2 = table.put(key2, val2); 122 | 123 | assertNotNull(buckets.get(placeAt1)); 124 | assertNotNull(buckets.get(placeAt1).peek()); 125 | assertNotNull(buckets.get(placeAt2)); 126 | assertNotNull(buckets.get(placeAt2).peek()); 127 | 128 | assertEquals(key1, buckets.get(placeAt1).peek().getKey()); 129 | assertEquals(val1, buckets.get(placeAt1).peek().getValue()); 130 | assertEquals(key2, buckets.get(placeAt2).peekLast().getKey()); 131 | assertEquals(val2, buckets.get(placeAt2).peekLast().getValue()); 132 | 133 | assertNull(output1); 134 | assertNull(output2); 135 | } 136 | 137 | // normal get operations 138 | @Test 139 | @DisplayName("Get test 1") 140 | void getTest1() { 141 | MyHashTable table = new MyHashTable<>(); 142 | int key = 10; 143 | int val = 20; 144 | 145 | table.put(10, 20); 146 | 147 | assertEquals(20, table.get(10)); 148 | } 149 | 150 | @Test 151 | @DisplayName("Get test 2") 152 | void getTest2() { 153 | MyHashTable table = new MyHashTable<>(); 154 | int key = 10; 155 | int val = 20; 156 | 157 | table.put(10, 20); 158 | 159 | assertNull(table.get(3)); 160 | } 161 | 162 | // Overwrite the value and get with the key 163 | @Test 164 | @DisplayName("Get test 3") 165 | void getTest3() { 166 | MyHashTable table = new MyHashTable<>(); 167 | int key = 10; 168 | int val1 = 20; 169 | int val2 = 200; 170 | 171 | table.put(key, val1); 172 | table.put(key, val2); 173 | 174 | assertEquals(200, table.get(10)); 175 | } 176 | 177 | @Test 178 | @DisplayName("Remove test 1") 179 | void removeTest1() { 180 | MyHashTable table = new MyHashTable<>(); 181 | int key = 10; 182 | int val = 20; 183 | 184 | table.put(10, 20); 185 | Object output = table.remove(10); 186 | 187 | assertEquals(20, output); 188 | assertNull(table.get(10)); 189 | assertFalse( 190 | table 191 | .getBuckets() 192 | .stream() 193 | .anyMatch(x -> x 194 | .stream() 195 | .anyMatch(y -> y 196 | .getKey() 197 | .equals(key))) 198 | ); 199 | } 200 | 201 | @Test 202 | @DisplayName("Remove test 2") 203 | void removeTest2() { 204 | MyHashTable table = new MyHashTable<>(); 205 | int key = 10; 206 | int val = 20; 207 | 208 | table.put(10, 20); 209 | Object output = table.remove(0); 210 | 211 | assertNull(output); 212 | assertNotNull(table.get(10)); 213 | assertTrue( 214 | table 215 | .getBuckets() 216 | .stream() 217 | .anyMatch(x -> x 218 | .stream() 219 | .anyMatch(y -> y 220 | .getKey() 221 | .equals(key))) 222 | ); 223 | } 224 | 225 | @Test 226 | @DisplayName("getKeySet test 1") 227 | void getKeySetTest1() { 228 | MyHashTable table = new MyHashTable<>(); 229 | table.put(1, 2); 230 | table.put(10, 20); 231 | table.put(100, 200); 232 | 233 | ArrayList expected = new ArrayList(); 234 | expected.add(1); 235 | expected.add(10); 236 | expected.add(100); 237 | 238 | ArrayList keys = table.getKeySet(); 239 | Collections.sort(keys); 240 | 241 | assertEquals(expected, keys); 242 | } 243 | 244 | // no duplicates 245 | @Test 246 | @DisplayName("getValuesSet test 1") 247 | void getValuesSetTest1() { 248 | MyHashTable table = new MyHashTable<>(); 249 | table.put(1, 2); 250 | table.put(10, 20); 251 | table.put(100, 200); 252 | 253 | ArrayList expected = new ArrayList(); 254 | expected.add(2); 255 | expected.add(20); 256 | expected.add(200); 257 | 258 | ArrayList vals = table.getValueSet(); 259 | Collections.sort(vals); 260 | 261 | assertEquals(expected, vals); 262 | } 263 | 264 | @Test 265 | @DisplayName("getValuesSet test 2") 266 | void getValuesSetTest2() { 267 | MyHashTable table = new MyHashTable<>(); 268 | table.put(1, 2); 269 | table.put(10, 20); 270 | table.put(100, 200); 271 | table.put(3, 200); 272 | 273 | ArrayList expected = new ArrayList(); 274 | expected.add(2); 275 | expected.add(20); 276 | expected.add(200); 277 | 278 | ArrayList vals = table.getValueSet(); 279 | Collections.sort(vals); 280 | 281 | assertEquals(expected, vals); 282 | } 283 | 284 | @Test 285 | @DisplayName("getEntries test 1") 286 | void getEntriesTest1() { 287 | MyHashTable table = new MyHashTable<>(); 288 | table.put(1, 2); 289 | table.put(10, 20); 290 | table.put(100, 200); 291 | table.put(3, 200); 292 | 293 | ArrayList expectedKeys = new ArrayList(); 294 | expectedKeys.add(1); 295 | expectedKeys.add(3); 296 | expectedKeys.add(10); 297 | expectedKeys.add(100); 298 | 299 | ArrayList expectedVals = new ArrayList(); 300 | expectedVals.add(2); 301 | expectedVals.add(20); 302 | expectedVals.add(200); 303 | expectedVals.add(200); 304 | 305 | ArrayList> entries = table.getEntries(); 306 | 307 | assertEquals(entries.stream().map(MyPair::getKey).sorted().toList(), expectedKeys); 308 | assertEquals(entries.stream().map(MyPair::getValue).sorted().toList(), expectedVals); 309 | } 310 | 311 | // check if rehash doubles table size 312 | @Test 313 | @DisplayName("rehash test 1") 314 | void rehashTest1() { 315 | MyHashTable table = new MyHashTable<>(); 316 | 317 | assertEquals(16, table.numBuckets()); 318 | 319 | table.rehash(); 320 | 321 | assertEquals(32, table.numBuckets()); 322 | } 323 | 324 | // check if rehash disperses the entries 325 | @Test 326 | @DisplayName("rehash test 2") 327 | void rehashTest2() { 328 | MyHashTable table = new MyHashTable<>(); 329 | 330 | assertEquals(16, table.numBuckets()); 331 | 332 | table.put(1, 10); 333 | table.put(2, 20); 334 | table.put(3, 30); 335 | table.put(4, 40); 336 | 337 | table.rehash(); 338 | assertEquals(32, table.numBuckets()); 339 | 340 | // if properly rehashed, the hashmap should be able to get values after resizing 341 | assertEquals(10, table.get(1)); 342 | assertEquals(20, table.get(2)); 343 | assertEquals(30, table.get(3)); 344 | assertEquals(40, table.get(4)); 345 | } 346 | 347 | // rehashing index test 348 | @Test 349 | @DisplayName("rehash test 3") 350 | void rehashTest3() { 351 | MyHashTable table = new MyHashTable<>(); 352 | 353 | assertEquals(16, table.numBuckets()); 354 | 355 | table.put(1, 10); 356 | table.put(16, 20); 357 | table.put(17, 30); 358 | table.put(32, 40); 359 | 360 | ArrayList>> buckets1 = table.getBuckets(); 361 | assertEquals(2, buckets1.get(0).size()); 362 | assertEquals(2, buckets1.get(1).size()); 363 | 364 | table.rehash(); 365 | assertEquals(32, table.numBuckets()); 366 | 367 | // if properly rehashed, the index of elements in the hashmap should be properly reorganized 368 | ArrayList>> buckets2 = table.getBuckets(); 369 | assertEquals(1, buckets2.get(0).size()); 370 | assertEquals(1, buckets2.get(1).size()); 371 | assertEquals(1, buckets2.get(16).size()); 372 | assertEquals(1, buckets2.get(17).size()); 373 | } 374 | 375 | //check if rehash handled keys with same hashvalues 376 | @Test 377 | @DisplayName("rehash test 4") 378 | void rehashTest4() { 379 | MyHashTable tester = new MyHashTable(3); 380 | 381 | tester.put(3, "key 3"); 382 | tester.put(9, "key 9"); 383 | 384 | tester.rehash(); 385 | 386 | ArrayList>> buckets = tester.getBuckets(); 387 | assertEquals(2, tester.size()); 388 | assertEquals(6, tester.numBuckets()); 389 | assertEquals(2, buckets.get(3).size()); 390 | } 391 | 392 | // standard iteration 393 | @Test 394 | @DisplayName("iterator test 1") 395 | void iteratorTest1() { 396 | MyHashTable table = new MyHashTable<>(); 397 | table.put(1, 10); 398 | table.put(2, 20); 399 | table.put(3, 30); 400 | 401 | ArrayList keys = new ArrayList<>(); 402 | ArrayList vals = new ArrayList<>(); 403 | for (MyPair item : table) { 404 | keys.add(item.getKey()); 405 | vals.add(item.getValue()); 406 | } 407 | 408 | ArrayList expectedKeys = new ArrayList<>(); 409 | expectedKeys.add(1); 410 | expectedKeys.add(2); 411 | expectedKeys.add(3); 412 | 413 | ArrayList expectedVals = new ArrayList<>(); 414 | expectedVals.add(10); 415 | expectedVals.add(20); 416 | expectedVals.add(30); 417 | 418 | assertEquals(expectedKeys, keys); 419 | assertEquals(expectedVals, vals); 420 | } 421 | 422 | // check hasNext() returns 423 | @Test 424 | @DisplayName("iterator test 2") 425 | void iteratorTest2() { 426 | MyHashTable table = new MyHashTable<>(); 427 | table.put(1, 10); 428 | Iterator> i = table.iterator(); 429 | assertTrue(i.hasNext()); 430 | i.next(); 431 | assertFalse(i.hasNext()); 432 | } 433 | 434 | // check hasNext() returns 435 | @Test 436 | @DisplayName("iterator test 3") 437 | void iteratorTest3() { 438 | MyHashTable table = new MyHashTable<>(); 439 | table.put(1, 10); 440 | table.put(17,100); 441 | table.put(4, 20); 442 | Iterator> i = table.iterator(); 443 | assertTrue(i.hasNext()); 444 | assertEquals(1, Objects.requireNonNull(i.next()).getKey()); 445 | assertTrue(i.hasNext()); 446 | assertEquals(17, Objects.requireNonNull(i.next()).getKey()); 447 | assertTrue(i.hasNext()); 448 | assertEquals(4, Objects.requireNonNull(i.next()).getKey()); 449 | assertFalse(i.hasNext()); 450 | } 451 | } 452 | 453 | // For tests below, if you are using a different filepath than the one given to the parser, 454 | // make sure to change it to your filepath in parserInit() 455 | // the tests below use the sample csv for speed 456 | class DataAnalyzerTests { 457 | Parser p = new Parser("/RateMyProf_Data_Gendered_Sample.csv"); 458 | 459 | // normal operation 460 | @Test 461 | @DisplayName("RatingDistributionByProf Test 1") 462 | void ratingDistributionByProfTest1() { 463 | p.read(); 464 | DataAnalyzer analyzer = new RatingDistributionByProf(p); 465 | MyHashTable output = analyzer.getDistByKeyword("rebecca tsosie"); 466 | 467 | assertEquals(0, output.get("1")); 468 | assertEquals(1, output.get("2")); 469 | assertEquals(1, output.get("3")); 470 | assertEquals(2, output.get("4")); 471 | assertEquals(3, output.get("5")); 472 | } 473 | 474 | // weird untrimmed and irregular cases keyword 475 | @Test 476 | @DisplayName("RatingDistributionByProf Test 2") 477 | void ratingDistributionByProfTest3() { 478 | p.read(); 479 | DataAnalyzer analyzer = new RatingDistributionByProf(p); 480 | MyHashTable output1 = analyzer.getDistByKeyword("rebecca tsosie"); 481 | MyHashTable output2 = analyzer.getDistByKeyword(" Rebecca tsosIe "); 482 | 483 | assertEquals(output1.get("1"), output2.get("1")); 484 | assertEquals(output1.get("2"), output2.get("2")); 485 | assertEquals(output1.get("3"), output2.get("3")); 486 | assertEquals(output1.get("4"), output2.get("4")); 487 | assertEquals(output1.get("5"), output2.get("5")); 488 | 489 | assertEquals(0, output2.get("1")); 490 | assertEquals(1, output2.get("2")); 491 | assertEquals(1, output2.get("3")); 492 | assertEquals(2, output2.get("4")); 493 | assertEquals(3, output2.get("5")); 494 | } 495 | 496 | // null testing 497 | @Test 498 | @DisplayName("RatingDistributionByProf Test 3") 499 | void ratingDistributionByProfTest4() { 500 | p.read(); 501 | DataAnalyzer analyzer = new RatingDistributionByProf(p); 502 | MyHashTable output1 = analyzer.getDistByKeyword("Invalid"); 503 | 504 | assertNull(output1); 505 | } 506 | 507 | // normal test from original minitester 508 | @Test 509 | @DisplayName("RatingDistributionByProf Test 4") 510 | void ratingDistributionByProfTest5() { 511 | p.read(); 512 | RatingDistributionByProf ratingDistributionByProf = new RatingDistributionByProf(p); 513 | MyHashTable dist = ratingDistributionByProf.getDistByKeyword("soazig le bihan"); 514 | assertEquals(1, dist.get("1")); 515 | assertEquals(0, dist.get("2")); 516 | assertEquals(1, dist.get("3")); 517 | assertEquals(0, dist.get("4")); 518 | assertEquals(2, dist.get("5")); 519 | } 520 | 521 | // normal operation, same example as pdf 522 | @Test 523 | @DisplayName("RatingCountPerProf Test 1") 524 | void ratingCountPerProfTest1() { 525 | p.read(); 526 | DataAnalyzer analyzer = new RatingDistributionBySchool(p); 527 | MyHashTable output = analyzer.getDistByKeyword("arizona state university"); 528 | assertEquals(4, output.get("adam chodorow\n3.88")); 529 | assertEquals(3, output.get("alan matheson\n4.5")); 530 | } 531 | 532 | // weird untrimmed keyword 533 | @Test 534 | @DisplayName("RatingCountPerProf Test 2") 535 | void ratingCountPerProfTest2() { 536 | p.read(); 537 | DataAnalyzer analyzer = new RatingDistributionBySchool(p); 538 | MyHashTable output1 = analyzer.getDistByKeyword("arizona state university"); 539 | MyHashTable output2 = analyzer.getDistByKeyword(" Arizona State University "); 540 | 541 | ArrayList k1 = output1.getKeySet(); 542 | ArrayList k2 = output2.getKeySet(); 543 | ArrayList v1 = output1.getValueSet(); 544 | ArrayList v2 = output2.getValueSet(); 545 | 546 | assertEquals(k1, k2); 547 | assertEquals(v1, v2); 548 | } 549 | 550 | // round the average rating to two decimal number 551 | @Test 552 | @DisplayName("RatingCountPerProf Test 3") 553 | void ratingCountPerProfTest3() { 554 | Parser parser = new Parser(""); 555 | 556 | HashMap fields = new HashMap<>(); 557 | fields.put("professor_name", 0); 558 | fields.put("school_name", 1); 559 | fields.put("department_name", 2); 560 | fields.put("post_date", 3); 561 | fields.put("student_star", 4); 562 | fields.put("student_difficult", 5); 563 | fields.put("comments", 6); 564 | fields.put("gender", 7); 565 | 566 | parser.fields = fields; 567 | String[] prof1 = {"Diana Oqimachi", "Long Beach City College", "Counseling department", "08/26/2014", "5", "1", "Diana is a great professor and counselor!I took the class because I need extra units but I learned so much. She\'s very helpful and willing to answer any questions.It was like having a counseling appointment for 6 weeks.Now,I make my appointments with her.She as motivated as if it was her very first time teaching or counseling.We need more like her!", "F"}; 568 | String[] prof2 = {"Diana Oqimachi", "Long Beach City College", "Counseling department", "04/28/2013", "5", "1", "Super easy, funny, enthusiastic and helpful!!", "F"}; 569 | String[] prof3 = {"Diana Oqimachi", "Long Beach City College", "Counseling department", "04/28/2013", "4.5", "1", "She gives an extremely helpful and very fun class environment. I recommend this class for any freshmen who don\\'t quite understand the concept of GPA\\'s or regulations in LBCC.", "F"}; 570 | String[] prof4 = {"Diana Oqimachi", "Long Beach City College", "Counseling department", "04/28/2013", "4", "1", "She gives an extremely helpful and very fun class environment. I recommend this class for any freshmen who don\\'t quite understand the concept of GPA\\'s or regulations in LBCC.", "F"}; 571 | 572 | parser.data.add(prof1); 573 | parser.data.add(prof2); 574 | parser.data.add(prof3); 575 | parser.data.add(prof4); 576 | 577 | DataAnalyzer analyzer = new RatingDistributionBySchool(parser); 578 | MyHashTable output1 = analyzer.getDistByKeyword("Long Beach City College"); 579 | 580 | ArrayList k1 = output1.getKeySet(); 581 | 582 | ArrayList expect = new ArrayList<>(); 583 | expect.add("diana oqimachi" + "\n" + "4.63"); 584 | 585 | assertEquals(expect, k1); 586 | } 587 | 588 | // null testing 589 | @Test 590 | @DisplayName("RatingCountPerProf Test 4") 591 | void ratingCountPerProfTest4() { 592 | p.read(); 593 | DataAnalyzer analyzer = new RatingDistributionBySchool(p); 594 | MyHashTable output1 = analyzer.getDistByKeyword("Invalid"); 595 | 596 | assertNull(output1); 597 | } 598 | 599 | // getting specific prof in school (from minitester) 600 | @Test 601 | @DisplayName("RatingCountPerProf Test 5") 602 | void ratingCountPerProfTest5() { 603 | p.read(); 604 | RatingDistributionBySchool ratingDistributionBySchool = new RatingDistributionBySchool(p); 605 | MyHashTable dist = ratingDistributionBySchool 606 | .getDistByKeyword("pennsylvania college of technology"); 607 | String key2 = "david burke" + "\n" + "4.33"; 608 | 609 | assertEquals(6, dist.get(key2)); 610 | } 611 | 612 | // Correct gender output, F -> W 613 | @Test 614 | @DisplayName("GenderByKeyword Test 1") 615 | void genderByKeywordTest1() { 616 | p.read(); 617 | DataAnalyzer analyzer = new GenderByKeyword(p); 618 | MyHashTable output = analyzer.getDistByKeyword("caring"); 619 | assertNotNull(output.get("M")); 620 | assertNotNull(output.get("F")); 621 | assertNotNull(output.get("X")); 622 | } 623 | 624 | // normal operation, same example as pdf 625 | @Test 626 | @DisplayName("GenderByKeyword Test 2") 627 | void genderByKeywordTest2() { 628 | p.read(); 629 | DataAnalyzer analyzer = new GenderByKeyword(p); 630 | MyHashTable output1 = analyzer.getDistByKeyword("caring"); 631 | MyHashTable output2 = analyzer.getDistByKeyword("smart"); 632 | 633 | assertEquals(9, output1.get("F")); 634 | assertEquals(4, output1.get("M")); 635 | assertEquals(0, output1.get("X")); 636 | 637 | assertEquals(6, output2.get("F")); 638 | assertEquals(14, output2.get("M")); 639 | assertEquals(0, output2.get("X")); 640 | } 641 | 642 | @Test 643 | @DisplayName("GenderByKeyword Test 3") 644 | void genderByKeywordTest3() { 645 | p.read(); 646 | DataAnalyzer analyzer = new GenderByKeyword(p); 647 | MyHashTable output1 = analyzer.getDistByKeyword("caring"); 648 | MyHashTable output2 = analyzer.getDistByKeyword(" CaRiNg"); 649 | 650 | ArrayList k1 = output1.getKeySet(); 651 | ArrayList k2 = output2.getKeySet(); 652 | ArrayList v1 = output1.getValueSet(); 653 | ArrayList v2 = output2.getValueSet(); 654 | Collections.sort(k1); 655 | Collections.sort(k2); 656 | Collections.sort(v1); 657 | Collections.sort(v2); 658 | 659 | assertEquals(k1, k2); 660 | assertEquals(v1, v2); 661 | } 662 | 663 | // null testing 664 | @Test 665 | @DisplayName("GenderByKeyword Test 4") 666 | void genderByKeywordTest4() { 667 | p.read(); 668 | DataAnalyzer analyzer = new GenderByKeyword(p); 669 | 670 | MyHashTable output = analyzer.getDistByKeyword("invalidInput"); 671 | assertNull(output); 672 | } 673 | 674 | @Test 675 | @DisplayName("GenderByKeyword Test 5") 676 | void genderByKeywordTest5() { 677 | p.read(); 678 | DataAnalyzer analyzer = new GenderByKeyword(p); 679 | MyHashTable output1 = analyzer.getDistByKeyword(" KiND "); 680 | MyHashTable output2 = analyzer.getDistByKeyword(" humor "); 681 | 682 | assertEquals(15, output1.get("M")); 683 | 684 | assertEquals(8, output1.get("F")); 685 | 686 | assertEquals(1, output1.get("X")); 687 | 688 | assertEquals(3, output2.get("M")); 689 | 690 | assertEquals(7, output2.get("F")); 691 | 692 | assertEquals(0, output2.get("X")); 693 | } 694 | 695 | @Test 696 | @DisplayName("RatingByKeyword Test 1") 697 | void ratingByKeywordTest1() { 698 | p.read(); 699 | DataAnalyzer analyzer = new RatingByKeyword(p); 700 | MyHashTable output1 = analyzer.getDistByKeyword("fun"); 701 | assertNotNull(output1.get("1")); 702 | assertNotNull(output1.get("2")); 703 | assertNotNull(output1.get("3")); 704 | assertNotNull(output1.get("4")); 705 | assertNotNull(output1.get("5")); 706 | } 707 | 708 | @Test 709 | @DisplayName("RatingByKeyword Test 2") 710 | void ratingByKeywordTest2() { 711 | p.read(); 712 | DataAnalyzer analyzer = new RatingByKeyword(p); 713 | MyHashTable output1 = analyzer.getDistByKeyword("fun"); 714 | MyHashTable output2 = analyzer.getDistByKeyword("FuN "); 715 | 716 | ArrayList k1 = output1.getKeySet(); 717 | ArrayList k2 = output2.getKeySet(); 718 | ArrayList v1 = output1.getValueSet(); 719 | ArrayList v2 = output2.getValueSet(); 720 | Collections.sort(k1); 721 | Collections.sort(k2); 722 | Collections.sort(v1); 723 | Collections.sort(v2); 724 | 725 | assertEquals(k1, k2); 726 | assertEquals(v1, v2); 727 | } 728 | 729 | // check for abundant words in one sentence 730 | @Test 731 | @DisplayName("RatingByKeyword Test 3") 732 | void ratingByKeywordTest3() { 733 | Parser parser1 = new Parser(""); 734 | Parser parser2 = new Parser(""); 735 | ArrayList dataArray1 = new ArrayList<>(); 736 | ArrayList dataArray2 = new ArrayList<>(); 737 | 738 | HashMap fields = new HashMap<>(); 739 | fields.put("professor_name", 0); 740 | fields.put("school_name", 1); 741 | fields.put("department_name", 2); 742 | fields.put("post_date", 3); 743 | fields.put("student_star", 4); 744 | fields.put("student_difficult", 5); 745 | fields.put("comments", 6); 746 | fields.put("gender", 7); 747 | 748 | parser1.fields = fields; 749 | parser2.fields = fields; 750 | 751 | String[] arr1 = {"Diana Oqimachi", "Long Beach City College", "Counseling department", "08/26/2014", "5", "1", "fun"}; 752 | String[] arr2 = {"Diana Oqimachi", "Long Beach City College", "Counseling department", "08/26/2014", "5", "1", "Fun fun fun!"}; 753 | dataArray1.add(arr1); 754 | dataArray2.add(arr2); 755 | 756 | parser1.data = dataArray1; 757 | parser2.data = dataArray2; 758 | 759 | DataAnalyzer analyzer1 = new RatingByKeyword(parser1); 760 | DataAnalyzer analyzer2 = new RatingByKeyword(parser2); 761 | 762 | MyHashTable output1 = analyzer1.getDistByKeyword("fun"); 763 | MyHashTable output2 = analyzer2.getDistByKeyword("fun"); 764 | 765 | ArrayList k1 = output1.getKeySet(); 766 | ArrayList k2 = output2.getKeySet(); 767 | ArrayList v1 = output1.getValueSet(); 768 | ArrayList v2 = output2.getValueSet(); 769 | 770 | Collections.sort(k1); 771 | Collections.sort(k2); 772 | Collections.sort(v1); 773 | Collections.sort(v1); 774 | 775 | assertEquals(k1, k2); 776 | assertEquals(v1, v2); 777 | } 778 | 779 | // null testing 780 | @Test 781 | @DisplayName("RatingByKeyword Test 4") 782 | void ratingByKeywordTest4() { 783 | p.read(); 784 | DataAnalyzer analyzer = new RatingByKeyword(p); 785 | MyHashTable output1 = analyzer.getDistByKeyword("InVaLIdInPut"); 786 | assertNull(output1); 787 | } 788 | 789 | @Test 790 | @DisplayName("RatingByKeyword Test 5") 791 | void ratingByKeywordTest5() { 792 | p.read(); 793 | RatingByKeyword ratingByKeyword = new RatingByKeyword(p); 794 | MyHashTable dist = ratingByKeyword.getDistByKeyword("terrible"); 795 | 796 | assertEquals(12, dist.get("1")); 797 | assertEquals(6, dist.get("2")); 798 | assertEquals(3, dist.get("3")); 799 | assertEquals(0, dist.get("4")); 800 | assertEquals(0, dist.get("5")); 801 | } 802 | 803 | @Test 804 | @DisplayName("RatingByGender Test 1") 805 | void ratingByGenderTest1() { 806 | p.read(); 807 | DataAnalyzer analyzer = new RatingByGender(p); 808 | MyHashTable output1 = analyzer.getDistByKeyword("F, difficulty"); 809 | assertNotNull(output1.get("1")); 810 | assertNotNull(output1.get("2")); 811 | assertNotNull(output1.get("3")); 812 | assertNotNull(output1.get("4")); 813 | assertNotNull(output1.get("5")); 814 | 815 | } 816 | 817 | @Test 818 | @DisplayName("RatingByGender Test 2") 819 | void ratingByGenderTest2() { 820 | p.read(); 821 | DataAnalyzer analyzer = new RatingByGender(p); 822 | MyHashTable output1 = analyzer.getDistByKeyword("M, Quality "); 823 | MyHashTable output2 = analyzer.getDistByKeyword("M, quality"); 824 | 825 | ArrayList k1 = output1.getKeySet(); 826 | ArrayList k2 = output2.getKeySet(); 827 | ArrayList v1 = output1.getValueSet(); 828 | ArrayList v2 = output2.getValueSet(); 829 | Collections.sort(k1); 830 | Collections.sort(k2); 831 | Collections.sort(v1); 832 | Collections.sort(v2); 833 | 834 | assertEquals(k1, k2); 835 | assertEquals(v1, v2); 836 | } 837 | 838 | //test for invalid input strings 839 | @Test 840 | @DisplayName("RatingByGender Test 3") 841 | void ratingByGenderTest3() { 842 | p.read(); 843 | DataAnalyzer analyzer = new RatingByGender(p); 844 | 845 | //See Ed post #1628 & #1741 for edge case clarification 846 | MyHashTable output1 = analyzer.getDistByKeyword("M, difficulty"); 847 | MyHashTable output2 = analyzer.getDistByKeyword("F , difficulty"); 848 | MyHashTable output3 = analyzer.getDistByKeyword("F , difficulty"); 849 | MyHashTable output4 = analyzer.getDistByKeyword("M, in valid"); 850 | MyHashTable output5 = analyzer.getDistByKeyword("x, quality"); 851 | MyHashTable output6 = analyzer.getDistByKeyword("m, dIfFiCULTY"); 852 | 853 | assertNull(output1); 854 | assertNull(output2); 855 | assertNull(output3); 856 | assertNull(output4); 857 | assertNull(output5); 858 | assertNotNull(output6); 859 | } 860 | 861 | @Test 862 | @DisplayName("RatingByGender Test 4") 863 | void ratingByGenderTest4() { 864 | p.read(); 865 | DataAnalyzer analyzer = new RatingByGender(p); 866 | MyHashTable output1 = analyzer.getDistByKeyword("F, quality"); 867 | assertEquals(62,output1.get("1")); 868 | assertEquals(42,output1.get("2")); 869 | assertEquals(44,output1.get("3")); 870 | assertEquals(83,output1.get("4")); 871 | assertEquals(198,output1.get("5")); 872 | } 873 | 874 | @Test 875 | @DisplayName("RatingByGender Test 5") 876 | void ratingByGenderTest5() { 877 | p.read(); 878 | RatingByGender ratingByGender = new RatingByGender(p); 879 | MyHashTable dist = ratingByGender.getDistByKeyword("F,difficulty"); 880 | 881 | assertEquals(83, dist.get("1")); 882 | assertEquals(90, dist.get("2")); 883 | assertEquals(126, dist.get("3")); 884 | assertEquals(73, dist.get("4")); 885 | assertEquals(57, dist.get("5")); 886 | } 887 | } 888 | 889 | // The below tests do not assert any outputs, 890 | // they time the runtime of each of your functions 891 | // with different number of entries and print out the time taken 892 | // You should observe with your own due diligence whether the increase in runtime is 893 | // your desired time complexity: 894 | // constant O(1) for getDistByKeyword 895 | // linear by bucket numbers O(m) for instantiation 896 | // You can plot it in your preferred data visualization method for some visuals 897 | 898 | // NOTE: 899 | // This method of stress testing is naive and not production-level 900 | // due to factors such different computer hardwares, compiler optimizations, and more 901 | // Please only use the data as a mere reference. 902 | class TimeTests { 903 | // parser with no data 904 | private final String[] NAMES = new String[] 905 | {"Sam", 906 | "Rebecca", 907 | "Albert", 908 | "Eric", 909 | "Ludwig", 910 | "XQC", 911 | "Hasan"}; 912 | private final String[] UNI_WORDS = new String[] 913 | {"Wild", 914 | "Chicken", 915 | "Omega", 916 | "Theta", 917 | "Polytechnic", 918 | "Kappa"}; 919 | private final String[] DEPARTMENTS = new String[] 920 | {"Mathematics", 921 | "Philosophy", 922 | "Physics", 923 | "Statistics", 924 | "Computer Science", 925 | "Linguistic"}; 926 | private final String[] DAY = new String[] 927 | {"01", "02", "03", "04", "05", "06", "07", "08", "09", 928 | "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", 929 | "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", 930 | "30", "31"}; 931 | private final String[] MONTH = new String[] 932 | {"01", "02", "03", "04", "05", "06", "07", "08", "09", 933 | "10", "11", "12"}; 934 | private final String[] YEAR = new String[]{"1999", "2000", "2001", "2002", "2003"}; 935 | private final String[] RATINGS = new String[]{"1", "2", "3", "4", "5"}; 936 | 937 | Parser p = new Parser("/RateMyProf_Data_Gendered.csv"); 938 | 939 | private void parserInitFields() { 940 | p.fields = new HashMap<>(); 941 | p.fields.put("professor_name", 0); 942 | p.fields.put("school_name", 1); 943 | p.fields.put("department_name", 2); 944 | p.fields.put("post_date", 3); 945 | p.fields.put("student_star", 4); 946 | p.fields.put("student_difficult", 5); 947 | p.fields.put("comments", 6); 948 | p.fields.put("gender", 7); 949 | } 950 | 951 | private final int[] NUMBERS_OF_ENTRIES 952 | = new int[]{100, 1000, 5000, 10000, 30000, 50000, 70000, 90000, 110000}; 953 | 954 | // fill the parser with randomly generated dummy data 955 | private void parserInitData(int numberOfEntries) { 956 | p.data = new ArrayList<>(); 957 | 958 | for (int i = 0; i < numberOfEntries; i++) { 959 | java.util.Random random = new java.util.Random(); 960 | String name = NAMES[random.nextInt(NAMES.length)]; 961 | String uni1 = UNI_WORDS[random.nextInt(UNI_WORDS.length)]; 962 | String uni2 = UNI_WORDS[random.nextInt(UNI_WORDS.length)]; 963 | String department = DEPARTMENTS[random.nextInt(DEPARTMENTS.length)]; 964 | String day = DAY[random.nextInt(DAY.length)]; 965 | String month = MONTH[random.nextInt(MONTH.length)]; 966 | String year = YEAR[random.nextInt(YEAR.length)]; 967 | String quality = RATINGS[random.nextInt(RATINGS.length)]; 968 | String difficulty = RATINGS[random.nextInt(RATINGS.length)]; 969 | 970 | p.data.add(new String[] 971 | {name, 972 | String.format("%s %S University", uni1, uni2), 973 | department + " department", 974 | String.format("%s/%s/%s", month, day, year), 975 | quality, difficulty, 976 | "Lorem Ipsum Most amazing instructor! She makes class fun and interesting! Cares about her students and wants nothing more than to see you succeed!", "F"}); 977 | } 978 | } 979 | 980 | private Pair extract(Supplier constructor) { 981 | Instant start = Instant.now(); 982 | DataAnalyzer analyzer = constructor.get(); 983 | Instant finish = Instant.now(); 984 | 985 | long time = Duration.between(start, finish).toMillis(); 986 | return new Pair<>(analyzer, time); 987 | } 988 | 989 | //returns time taken 990 | private long query(DataAnalyzer analyzer, String keyword) { 991 | Instant start = Instant.now(); 992 | analyzer.getDistByKeyword(keyword); 993 | Instant finish = Instant.now(); 994 | 995 | return Duration.between(start, finish).toMillis(); 996 | } 997 | 998 | @Test 999 | @DisplayName("Time for RatingDistributionByProf") 1000 | void ratingDistributionByProfTime() { 1001 | parserInitFields(); 1002 | Supplier constructor = () -> new RatingDistributionByProf(p); 1003 | String keyword = "Sam"; 1004 | 1005 | for (int num : NUMBERS_OF_ENTRIES) { 1006 | parserInitData(num); 1007 | Pair extracted = extract(constructor); 1008 | DataAnalyzer analyzer = extracted.getKey(); 1009 | Object time = extracted.getValue(); 1010 | 1011 | System.out.println("The time taken to extract information for " + num + " entries is " + time + "ms"); 1012 | System.out.println("The time taken to query a keyword for " + num + " entries is " + query(analyzer, keyword) + "ms"); 1013 | System.out.println(); 1014 | } 1015 | } 1016 | 1017 | @Test 1018 | @DisplayName("Time for RatingDistributionBySchool") 1019 | void ratingDistributionBySchoolTime() { 1020 | parserInitFields(); 1021 | Supplier constructor = () -> new RatingDistributionBySchool(p); 1022 | String keyword = "Wild Chicken University"; 1023 | 1024 | for (int num : NUMBERS_OF_ENTRIES) { 1025 | parserInitData(num); 1026 | 1027 | Pair extracted = extract(constructor); 1028 | DataAnalyzer analyzer = extracted.getKey(); 1029 | Object time = extracted.getValue(); 1030 | 1031 | System.out.println("The time taken to extract information for " + num + " entries is " + time + "ms"); 1032 | System.out.println("The time taken to query a keyword for " + num + " entries is " + query(analyzer, keyword) + "ms"); 1033 | System.out.println(); 1034 | } 1035 | } 1036 | 1037 | @Test 1038 | @DisplayName("Time for RatingByGender") 1039 | void ratingByGenderTime() { 1040 | parserInitFields(); 1041 | Supplier constructor = () -> new RatingByGender(p); 1042 | String keyword = "F, difficulty"; 1043 | 1044 | for (int num : NUMBERS_OF_ENTRIES) { 1045 | parserInitData(num); 1046 | 1047 | Pair extracted = extract(constructor); 1048 | DataAnalyzer analyzer = extracted.getKey(); 1049 | Object time = extracted.getValue(); 1050 | 1051 | System.out.println("The time taken to extract information for " + num + " entries is " + time + "ms"); 1052 | System.out.println("The time taken to query a keyword for " + num + " entries is " + query(analyzer, keyword) + "ms"); 1053 | System.out.println(); 1054 | } 1055 | } 1056 | 1057 | @Test 1058 | @DisplayName("Time for GenderByKeyword") 1059 | void genderByKeywordTime() { 1060 | parserInitFields(); 1061 | Supplier constructor = () -> new GenderByKeyword(p); 1062 | String keyword = "fun"; 1063 | 1064 | for (int num : NUMBERS_OF_ENTRIES) { 1065 | parserInitData(num); 1066 | 1067 | Pair extracted = extract(constructor); 1068 | DataAnalyzer analyzer = extracted.getKey(); 1069 | Object time = extracted.getValue(); 1070 | 1071 | System.out.println("The time taken to extract information for " + num + " entries is " + time + "ms"); 1072 | System.out.println("The time taken to query a keyword for " + num + " entries is " + query(analyzer, keyword) + "ms"); 1073 | System.out.println(); 1074 | } 1075 | } 1076 | 1077 | @Test 1078 | @DisplayName("Time for RatingByKeyword") 1079 | void ratingByKeyword() { 1080 | parserInitFields(); 1081 | Supplier constructor = () -> new RatingByKeyword(p); 1082 | String keyword = "fun"; 1083 | 1084 | for (int num : NUMBERS_OF_ENTRIES) { 1085 | parserInitData(num); 1086 | 1087 | Pair extracted = extract(constructor); 1088 | DataAnalyzer analyzer = extracted.getKey(); 1089 | Object time = extracted.getValue(); 1090 | 1091 | System.out.println("The time taken to extract information for " + num + " entries is " + time + "ms"); 1092 | System.out.println("The time taken to query a keyword for " + num + " entries is " + query(analyzer, keyword) + "ms"); 1093 | System.out.println(); 1094 | } 1095 | } 1096 | } 1097 | 1098 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # COMP 250 Testers [+51 tests for Final Project] 2 | 3 | This is a repository for COMP 250 testers! 4 | 5 | ## Errors at code execution 6 | 7 | If you encounter any errors while running the code, create a new Issue:[ click here to create a new issue](https://github.com/ssantoshp/Empyrial/issues/new/choose). One of the contributors will try to help you! 8 | 9 | ## How to contribute 10 | 11 | This section aims to simplify and guide the way beginners make their first contribution. If you are looking to make your first contribution, follow the steps below. 12 | 13 | _If you're not comfortable with command line, [here are tutorials using GUI tools.](#tutorials-using-other-tools)_ 14 | 15 | fork this repository 16 | 17 | #### If you don't have git on your machine, [install it](https://docs.github.com/en/get-started/quickstart/set-up-git). 18 | 19 | ## Fork this repository 20 | 21 | Fork this repository by clicking on the fork button on the top of this page. 22 | This will create a copy of this repository in your account. 23 | 24 | ## Clone the repository 25 | 26 | clone this repository 27 | 28 | Now clone the forked repository to your machine. Go to your GitHub account, open the forked repository, click on the code button and then click the _copy to clipboard_ icon. 29 | 30 | Open a terminal and run the following git command: 31 | 32 | ``` 33 | git clone "url you just copied" 34 | ``` 35 | 36 | where "url you just copied" (without the quotation marks) is the url to this repository (your fork of this project). See the previous steps to obtain the url. 37 | 38 | copy URL to clipboard 39 | 40 | For example: 41 | 42 | ``` 43 | git clone https://github.com/this-is-you/first-contributions.git 44 | ``` 45 | 46 | where `this-is-you` is your GitHub username. Here you're copying the contents of the first-contributions repository on GitHub to your computer. 47 | 48 | ## Create a branch 49 | 50 | Change to the repository directory on your computer (if you are not already there): 51 | 52 | ``` 53 | cd first-contributions 54 | ``` 55 | 56 | Now create a branch using the `git switch` command: 57 | 58 | ``` 59 | git switch -c your-new-branch-name 60 | ``` 61 | 62 | For example: 63 | 64 | ``` 65 | git switch -c add-alonzo-church 66 | ``` 67 | 68 | ## Make necessary changes and commit those changes 69 | 70 | Now open `Contributors.md` file in a text editor, add your name to it. Don't add it at the beginning or end of the file. Put it anywhere in between. Now, save the file. 71 | 72 | git status 73 | 74 | If you go to the project directory and execute the command `git status`, you'll see there are changes. 75 | 76 | Add those changes to the branch you just created using the `git add` command: 77 | 78 | ``` 79 | git add Contributors.md 80 | ``` 81 | 82 | Now commit those changes using the `git commit` command: 83 | 84 | ``` 85 | git commit -m "Add your-name to Contributors list" 86 | ``` 87 | 88 | replacing `your-name` with your name. 89 | 90 | ## Push changes to GitHub 91 | 92 | Push your changes using the command `git push`: 93 | 94 | ``` 95 | git push -u origin your-branch-name 96 | ``` 97 | 98 | replacing `your-branch-name` with the name of the branch you created earlier. 99 | 100 |
101 | If you get any errors while pushing, click here: 102 | 103 | - ### Authentication Error 104 |
remote: Support for password authentication was removed on August 13, 2021. Please use a personal access token instead.
105 |   remote: Please see https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ for more information.
106 |   fatal: Authentication failed for 'https://github.com//first-contributions.git/'
107 | Go to [GitHub's tutorial](https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account) on generating and configuring an SSH key to your account. 108 | 109 |
110 | 111 | ## Submit your changes for review 112 | 113 | If you go to your repository on GitHub, you'll see a `Compare & pull request` button. Click on that button. 114 | 115 | create a pull request 116 | 117 | Now submit the pull request. 118 | 119 | submit pull request 120 | 121 | Soon I'll be merging all your changes into the main branch of this project. You will get a notification email once the changes have been merged. 122 | 123 | ### [Additional material](additional-material/git_workflow_scenarios/additional-material.md) 124 | --------------------------------------------------------------------------------