Skip to content

Commit

Permalink
fix unit test and some edge cases in generating roads.mmd (thx unit t…
Browse files Browse the repository at this point in the history
…ests). Also add settings for tests
  • Loading branch information
Abelkrijgtalles committed Dec 16, 2024
1 parent 06b756f commit ef14fbe
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class RoadData {
DO NOT DELETE THIS FILE!!!
This rest of this file may look like gibberish, but it's not. This stores all the road data for Mojang Maps.
If you delete this file, you'll delete all your Mojang Maps data and essentially start from scratch.
Even editing this file, or this message, will brick Mojang Maps.
---
""";
private final static Path FILE_PATH = Path.of(MojangMaps.loaderInfo.getConfig().getDataDirectory().toString(), "roads.mmd");
Expand Down Expand Up @@ -116,7 +117,7 @@ public void generateRoadData(List<Road> roads) {
MojangMaps.loaderInfo.getConfig().getDataDirectory().toFile().mkdirs();
}

if (FILE_PATH.toFile().exists()) {
if (!FILE_PATH.toFile().exists()) {
FILE_PATH.toFile().createNewFile();
}

Expand Down Expand Up @@ -219,9 +220,13 @@ public List<Road> readRoadData() {
switch (files[i]) {

case 0x06:
if (i + 2 > files.length - 1) continue;
if (files[i + 2] != 0x07) continue;
version = files[i + 1];
case 0x07:
compressedArray = Arrays.copyOfRange(files, i + 1, files.length);
if (files[i - 2] != 0x06) continue;
compressedArray = Arrays.copyOfRange(files, MESSAGE.getBytes(StandardCharsets.UTF_8).length + 3, files.length);
break;

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* nl.abelkrijgtalles.mojangmaps.mojang_maps.fabric.test
* Copyright (C) 2024 Abel van Hulst/Abelkrijgtalles/Abelpro678
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package nl.abelkrijgtalles.mojangmaps;

public class TestSettings {
// please edit the values if needed

// How many times to repeat a RepeatedTest
public final static int REPEATED_TEST_COUNT = 100;

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Random;
import net.minecraft.core.Position;
import net.minecraft.world.phys.Vec3;
import nl.abelkrijgtalles.mojangmaps.TestSettings;
import nl.abelkrijgtalles.mojangmaps.common.model.Road;
import nl.abelkrijgtalles.mojangmaps.fabric.MojangMapsFabric;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -33,19 +34,20 @@

public class RoadDataTest {

@RepeatedTest(5)
@RepeatedTest(TestSettings.REPEATED_TEST_COUNT)
void testGeneratingRoadData(RepetitionInfo repetitionInfo) {

List<Road> roads = new ArrayList<>();
Random rand = new Random();
Faker faker = new Faker();
RoadData roadData = new RoadData();
int numberOfRoadsAndWaypointsPerRoad = repetitionInfo.getCurrentRepetition();

for (int i = 0; i < repetitionInfo.getCurrentRepetition(); i++) {
for (int i = 0; i < numberOfRoadsAndWaypointsPerRoad; i++) {

List<Position> waypoints = new ArrayList<>();

for (int j = 0; j < repetitionInfo.getCurrentRepetition(); j++) {
for (int j = 0; j < numberOfRoadsAndWaypointsPerRoad; j++) {

waypoints.add(new Vec3(rand.nextDouble(1000), rand.nextDouble(256), rand.nextDouble(1000)));

Expand All @@ -56,7 +58,32 @@ void testGeneratingRoadData(RepetitionInfo repetitionInfo) {
}

roadData.generateRoadData(roads);
assertEquals(roads, roadData.readRoadData());

// doing this cause junit does weird
List<Road> readRoads = roadData.readRoadData();
for (int i = 0; i < roads.size(); i++) {

Road road = roads.get(i);
Road readRoad = readRoads.get(i);

assertEquals(road.getName(), readRoad.getName());
assertEquals(road.getWorldName(), readRoad.getWorldName());

List<Position> roadWaypoints = road.getWaypoints();
List<Position> readRoadWaypoints = readRoad.getWaypoints();

for (int j = 0; j < roadWaypoints.size(); j++) {

Position position = roadWaypoints.get(j);
Position readPosition = readRoadWaypoints.get(j);

assertEquals((int) position.x(), (int) readPosition.x());
assertEquals((int) position.y(), (int) readPosition.y());
assertEquals((int) position.z(), (int) readPosition.z());

}

}

}

Expand Down

0 comments on commit ef14fbe

Please sign in to comment.