Skip to content

Commit

Permalink
Removed bug in DataFileReader with a single comment character
Browse files Browse the repository at this point in the history
  • Loading branch information
heySourabh committed Aug 11, 2023
1 parent 4e0f216 commit 29cefcb
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 32 deletions.
9 changes: 7 additions & 2 deletions src/main/io/DataFileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ public void close() {
private String nextLine() {
String nextLine = fileScanner.nextLine().trim();

if (commentStr != null)
nextLine = nextLine.split(commentStr)[0].trim();
if (commentStr != null) {
if (nextLine.equals(commentStr)) {
nextLine = "";
} else {
nextLine = nextLine.split(commentStr)[0].trim();
}
}

// Since blank lines are ignored
if (nextLine.length() == 0)
Expand Down
94 changes: 64 additions & 30 deletions test/main/io/DataFileReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,28 @@ public void readWithComments() throws IOException {
File tempFile = new File("test/test_data/dataFileTest.cfd");

try (FileWriter writer = new FileWriter(tempFile)) {
String textWithComments = "% Comment line\n" +
"int_param1=56\n" +
"IntParam2 = 85\n" +
"DoubleParam1 = 2452 % This a double param\n" +
"str1 = Just a plain line\n" +
"987 34 76\n" +
"45 72 25 98 41 23 14 57 92 12 % int array\n" +
"line=empty";
String textWithComments = """
% Comment line
int_param1=56
IntParam2 = 85
DoubleParam1 = 2452 % This a double param
str1 = Just a plain line
987 34 76
45 72 25 98 41 23 14 57 92 12 % int array
line=empty""";
writer.write(textWithComments);
}
DataFileReader fileReader = new DataFileReader(tempFile, "%");
try (DataFileReader fileReader = new DataFileReader(tempFile, "%")) {

assertEquals(56, fileReader.readIntParameter("int_param1"));
assertEquals(85, fileReader.readIntParameter("IntParam2"));
assertEquals(2452, fileReader.readDoubleParameter("DoubleParam1"), 1e-15);
assertEquals("Just a plain line", fileReader.readParameter("str1"));
assertEquals(0, new Point(987, 34, 76).distance(fileReader.readXYZ()), 1e-15);
assertArrayEquals(new int[]{45, 72, 25, 98, 41, 23, 14, 57, 92, 12}, fileReader.readIntArray());
assertEquals(56, fileReader.readIntParameter("int_param1"));
assertEquals(85, fileReader.readIntParameter("IntParam2"));
assertEquals(2452, fileReader.readDoubleParameter("DoubleParam1"), 1e-15);
assertEquals("Just a plain line", fileReader.readParameter("str1"));
assertEquals(0, new Point(987, 34, 76).distance(fileReader.readXYZ()), 1e-15);
assertArrayEquals(new int[]{45, 72, 25, 98, 41, 23, 14, 57, 92, 12}, fileReader.readIntArray());

assertThrows(IllegalArgumentException.class, () -> fileReader.readParameter("Dummy")); // line=empty
assertThrows(IllegalArgumentException.class, () -> fileReader.readParameter("Dummy")); // line=empty
}

if (!tempFile.delete()) System.out.println("Unable to delete temporary file: " + tempFile);
}
Expand All @@ -47,22 +49,54 @@ public void readWithoutComments() throws IOException {
File tempFile = new File("test/test_data/dataFileTest.cfd");

try (FileWriter writer = new FileWriter(tempFile)) {
String textWithoutComments = "int_param1=56\n" +
"IntParam2 = 85\n" +
"DoubleParam1 = 2452 \n" +
"str1 = Just a plain line\n" +
"987 34 76\n" +
"45 72 25 98 41 23 14 57 92 12\n";
String textWithoutComments = """
int_param1=56
IntParam2 = 85
DoubleParam1 = 2452\s
str1 = Just a plain line
987 34 76
45 72 25 98 41 23 14 57 92 12
""";
writer.write(textWithoutComments);
}
DataFileReader fileReader = new DataFileReader(tempFile);

assertEquals(56, fileReader.readIntParameter("int_param1"));
assertEquals(85, fileReader.readIntParameter("IntParam2"));
assertEquals(2452, fileReader.readDoubleParameter("DoubleParam1"), 1e-15);
assertEquals("Just a plain line", fileReader.readParameter("str1"));
assertEquals(0, new Point(987, 34, 76).distance(fileReader.readXYZ()), 1e-15);
assertArrayEquals(new int[]{45, 72, 25, 98, 41, 23, 14, 57, 92, 12}, fileReader.readIntArray());
try (DataFileReader fileReader = new DataFileReader(tempFile)) {

assertEquals(56, fileReader.readIntParameter("int_param1"));
assertEquals(85, fileReader.readIntParameter("IntParam2"));
assertEquals(2452, fileReader.readDoubleParameter("DoubleParam1"), 1e-15);
assertEquals("Just a plain line", fileReader.readParameter("str1"));
assertEquals(0, new Point(987, 34, 76).distance(fileReader.readXYZ()), 1e-15);
assertArrayEquals(new int[]{45, 72, 25, 98, 41, 23, 14, 57, 92, 12}, fileReader.readIntArray());
}

if (!tempFile.delete()) System.out.println("Unable to delete temporary file: " + tempFile);
}

@Test
public void file_with_only_comment_on_a_line() throws IOException {
File tempFile = new File("test/test_data/dataFileTest.cfd");

try (FileWriter writer = new FileWriter(tempFile)) {
String textWithoutComments = """
%
int_param1=56
IntParam2 = 85
DoubleParam1= 2452\s
str1 = Just a plain line
987 34 76
45 72 25 98 41 23 14 57 92 12
""";
writer.write(textWithoutComments);
}
try (DataFileReader fileReader = new DataFileReader(tempFile, "%")) {

assertEquals(56, fileReader.readIntParameter("int_param1"));
assertEquals(85, fileReader.readIntParameter("IntParam2"));
assertEquals(2452, fileReader.readDoubleParameter("DoubleParam1"), 1e-15);
assertEquals("Just a plain line", fileReader.readParameter("str1"));
assertEquals(0, new Point(987, 34, 76).distance(fileReader.readXYZ()), 1e-15);
assertArrayEquals(new int[]{45, 72, 25, 98, 41, 23, 14, 57, 92, 12}, fileReader.readIntArray());
}

if (!tempFile.delete()) System.out.println("Unable to delete temporary file: " + tempFile);
}
Expand Down

0 comments on commit 29cefcb

Please sign in to comment.