Skip to content

Commit

Permalink
added read from file class and android tests for him
Browse files Browse the repository at this point in the history
  • Loading branch information
pmokeev committed Aug 3, 2021
1 parent d4ad8f3 commit ac6b2d0
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.googleresearch.capturesync;

import android.content.Context;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class ReadFromFileTests {
Context unitTestContext;

@Before
public void initContext() {
unitTestContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
unitTestContext = ApplicationProvider.getApplicationContext();
}

@Test
public void readFromFile_CoordinatesArray_isCorrect() {
double[][] correctArray = { { (double)1.8405248, (double)-1.7660676, (double)0.49335796 }, { (double)1.8783984, (double)-1.8546431, (double)0.5410055 }, { (double)1.8924483, (double)-1.896182, (double)0.5593314 } };
String namefile = "ForUnitTestFile1.csv";

Assert.assertArrayEquals(correctArray, ReadFromFile.ReadFromCSV(namefile, unitTestContext).first);
}

@Test
public void readFromFile_TimesArray_isCorrect() {
double[] correctArray = { (double)691166767933511L / 1e9, (double)691166769907511L / 1e9, (double)691166771907511L / 1e9 };
String namefile = "ForUnitTestFile1.csv";

Assert.assertArrayEquals(correctArray, ReadFromFile.ReadFromCSV(namefile, unitTestContext).second, 0.1);
}

@Test
public void readFromFile_IncorrectCSV() {
double[] correctArray = new double[0];
String namefile = "ForUnitTestFile2.csv";

Assert.assertArrayEquals(correctArray, ReadFromFile.ReadFromCSV(namefile, unitTestContext).second, 0.1);
}

@Test
public void readFromFile_EmptyFile() {
double[] correctArray = new double[0];
String namefile = "ForUnitTestFile3.csv";

Assert.assertArrayEquals(correctArray, ReadFromFile.ReadFromCSV(namefile, unitTestContext).second, 0.1);
}
}
63 changes: 63 additions & 0 deletions app/src/main/java/com/googleresearch/capturesync/ReadFromFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.googleresearch.capturesync;

import android.content.Context;
import android.util.Pair;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ReadFromFile {
private static int CountOfRows(String namefile, Context context) {
int counter = 0;

try {
InputStream inputStream = context.getAssets().open(namefile);
for (int symbol = 0; symbol != -1; symbol = inputStream.read()) {
counter += symbol == '\n' ? 1 : 0;
}
} catch (IOException exception) {
System.out.println("Error Reading The File.");
exception.printStackTrace();
}

return counter;
}

public static Pair<double[][], double[]> ReadFromCSV(String namefile, Context context) {
double[][] coordinatesArray = new double[0][0];
double[] timeArray = new double[0];

try {
InputStream inputStream = context.getAssets().open(namefile);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
int countOfRows = CountOfRows(namefile, context);
coordinatesArray = new double[countOfRows][3];
timeArray = new double[countOfRows];
int index = 0;

while(bufferedReader.ready()) {
String[] line = bufferedReader.readLine().split(",");
double[] tempCoordinatesArray = { Double.parseDouble(line[0]), Double.parseDouble(line[1]), Double.parseDouble(line[2])};
coordinatesArray[index] = tempCoordinatesArray;
timeArray[index] = Double.parseDouble(line[3]) / 1e9;

index++;
}
} catch (FileNotFoundException exception) {
System.out.println("File Not Found.");
exception.printStackTrace();
} catch (IOException exception) {
System.out.println("Error Reading The File.");
exception.printStackTrace();
} catch (ArrayIndexOutOfBoundsException exception) {
System.out.println("CSV incorrect");
exception.printStackTrace();
return new Pair<>(new double[0][0], new double[0]);
}

return new Pair<>(coordinatesArray, timeArray);
}
}

0 comments on commit ac6b2d0

Please sign in to comment.