From ac6b2d016ce2b9a66350c6c328dfac931d401a2b Mon Sep 17 00:00:00 2001 From: pmokeev Date: Tue, 3 Aug 2021 16:58:25 +0300 Subject: [PATCH] added read from file class and android tests for him --- .../capturesync/ReadFromFileTests.java | 52 +++++++++++++++ .../capturesync/ReadFromFile.java | 63 +++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 app/src/androidTest/java/com/googleresearch/capturesync/ReadFromFileTests.java create mode 100644 app/src/main/java/com/googleresearch/capturesync/ReadFromFile.java diff --git a/app/src/androidTest/java/com/googleresearch/capturesync/ReadFromFileTests.java b/app/src/androidTest/java/com/googleresearch/capturesync/ReadFromFileTests.java new file mode 100644 index 0000000..9e2324c --- /dev/null +++ b/app/src/androidTest/java/com/googleresearch/capturesync/ReadFromFileTests.java @@ -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); + } +} diff --git a/app/src/main/java/com/googleresearch/capturesync/ReadFromFile.java b/app/src/main/java/com/googleresearch/capturesync/ReadFromFile.java new file mode 100644 index 0000000..9399c17 --- /dev/null +++ b/app/src/main/java/com/googleresearch/capturesync/ReadFromFile.java @@ -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 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); + } +}