-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added read from file class and android tests for him
- Loading branch information
Showing
2 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
app/src/androidTest/java/com/googleresearch/capturesync/ReadFromFileTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
63
app/src/main/java/com/googleresearch/capturesync/ReadFromFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |