From 94af7f5f2f1704334b0c617bc69e19389215b1ee Mon Sep 17 00:00:00 2001 From: Iurii Tatishchev Date: Mon, 26 Aug 2024 19:05:44 -0700 Subject: [PATCH] Assignment 2 init - Find Max and Min --- labMaxMin/.gitignore | 184 ++++++++++++++++++ labMaxMin/.idea/.gitignore | 8 + .../inspectionProfiles/Project_Default.xml | 6 + labMaxMin/.idea/misc.xml | 6 + labMaxMin/.idea/modules.xml | 8 + labMaxMin/.idea/vcs.xml | 6 + labMaxMin/Assignment.java | 37 ++++ labMaxMin/TestAll.java | 103 ++++++++++ labMaxMin/inputs/test01.txt | 2 + labMaxMin/inputs/test02.txt | 3 + labMaxMin/inputs/test03.txt | 4 + labMaxMin/inputs/test04.txt | 5 + labMaxMin/inputs/test05.txt | 6 + labMaxMin/labMaxMin.iml | 11 ++ labMaxMin/outputs/test01.txt | 1 + labMaxMin/outputs/test02.txt | 1 + labMaxMin/outputs/test03.txt | 1 + labMaxMin/outputs/test04.txt | 1 + labMaxMin/outputs/test05.txt | 1 + 19 files changed, 394 insertions(+) create mode 100644 labMaxMin/.gitignore create mode 100644 labMaxMin/.idea/.gitignore create mode 100644 labMaxMin/.idea/inspectionProfiles/Project_Default.xml create mode 100644 labMaxMin/.idea/misc.xml create mode 100644 labMaxMin/.idea/modules.xml create mode 100644 labMaxMin/.idea/vcs.xml create mode 100644 labMaxMin/Assignment.java create mode 100644 labMaxMin/TestAll.java create mode 100644 labMaxMin/inputs/test01.txt create mode 100644 labMaxMin/inputs/test02.txt create mode 100644 labMaxMin/inputs/test03.txt create mode 100644 labMaxMin/inputs/test04.txt create mode 100644 labMaxMin/inputs/test05.txt create mode 100644 labMaxMin/labMaxMin.iml create mode 100644 labMaxMin/outputs/test01.txt create mode 100644 labMaxMin/outputs/test02.txt create mode 100644 labMaxMin/outputs/test03.txt create mode 100644 labMaxMin/outputs/test04.txt create mode 100644 labMaxMin/outputs/test05.txt diff --git a/labMaxMin/.gitignore b/labMaxMin/.gitignore new file mode 100644 index 0000000..7a3d0fb --- /dev/null +++ b/labMaxMin/.gitignore @@ -0,0 +1,184 @@ +### Intellij template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### Java template +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +replay_pid* + +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + diff --git a/labMaxMin/.idea/.gitignore b/labMaxMin/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/labMaxMin/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/labMaxMin/.idea/inspectionProfiles/Project_Default.xml b/labMaxMin/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..03d9549 --- /dev/null +++ b/labMaxMin/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/labMaxMin/.idea/misc.xml b/labMaxMin/.idea/misc.xml new file mode 100644 index 0000000..1b2d693 --- /dev/null +++ b/labMaxMin/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/labMaxMin/.idea/modules.xml b/labMaxMin/.idea/modules.xml new file mode 100644 index 0000000..861e7e2 --- /dev/null +++ b/labMaxMin/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/labMaxMin/.idea/vcs.xml b/labMaxMin/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/labMaxMin/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/labMaxMin/Assignment.java b/labMaxMin/Assignment.java new file mode 100644 index 0000000..126c9cc --- /dev/null +++ b/labMaxMin/Assignment.java @@ -0,0 +1,37 @@ +package labMaxMin; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +public class Assignment { + public static int[] getMaxAndMin(int[] sequence) { + // Fill in here + return new int[] { 0, 0 }; + } + + public static void run(String input_path) { + int[] sequence; + int arraySize = 1; + + try (BufferedReader br = new BufferedReader(new FileReader(input_path))) { + // Get the size of the sequence + arraySize = Integer.parseInt(br.readLine()); + sequence = new int[arraySize]; + + // Read the sequence + for (int i = 0; i < arraySize; i++) { + sequence[i] = Integer.parseInt(br.readLine()); + } + + // Get max and min values + int[] maxAndMin = getMaxAndMin(sequence); + + // Output + System.out.println(maxAndMin[0] + ";" + maxAndMin[1]); + + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/labMaxMin/TestAll.java b/labMaxMin/TestAll.java new file mode 100644 index 0000000..b762a2c --- /dev/null +++ b/labMaxMin/TestAll.java @@ -0,0 +1,103 @@ +package labMaxMin; + +import java.io.File; +import java.net.URL; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.PrintStream; +import java.io.ByteArrayOutputStream; + +public class TestAll { + public static void main(String[] args) { + processInputFiles(); + } + + public static void processInputFiles() { + try { + // Get the inputs and outputs directories + URL input_url = TestAll.class.getResource("inputs"); + URL output_url = TestAll.class.getResource("outputs"); + if (input_url == null) { + System.out.println("Cannot find folder \"inputs\""); + return; + } + if (output_url == null) { + System.out.println("Cannot find folder \"outputs\""); + return; + } + + // Get the names of all the files in either directory + File input_folder = new File(input_url.getPath()); + File[] input_files = input_folder.listFiles(); + File output_folder = new File(output_url.getPath()); + if (input_files == null) { + System.out.printf("No files in folder: %s\n", input_folder.getPath()); + return; + } + // No need to check if output files exist, can still run inputs + + // Capture the print statements from the assignment into printstream + ByteArrayOutputStream captured_bytestream = new ByteArrayOutputStream(); + PrintStream captured_printstream = new PrintStream(captured_bytestream); + // Replace System.out with a local variable to capture prints + PrintStream stdout = System.out; + System.setOut(captured_printstream); + Integer total_tests = 0, passed_tests = 0; + + for (File input_file : input_files) { + if (!input_file.isFile()) { + continue; + } + // Print which test is currently being run + stdout.println("Processing file: " + input_file.getName()); + // Run the assignment on this input file + Assignment.run(input_file.getAbsolutePath()); + // Ensure all the output from assignment goes to local variable + System.out.flush(); + // Get the output from assignment into a String + String assignment_output = captured_bytestream.toString(); + captured_bytestream.reset(); + // Get the file contents for comparison + File output_file = new File(output_folder, input_file.getName()); + // Check if a corresponding output file exists + if (!output_file.exists()) { + // Print the output to the console without grading + stdout.println("No corresponding output, result shown:"); + stdout.println(assignment_output); + continue; + } + // Read the contents of the corresponding output file + total_tests++; + BufferedReader buffered_reader = new BufferedReader(new FileReader(output_file.getAbsolutePath())); + StringBuilder string_builder = new StringBuilder(); + String line; + while ((line = buffered_reader.readLine()) != null) { + string_builder.append(line).append(System.lineSeparator()); + } + buffered_reader.close(); + + String correct_output = string_builder.toString(); + if (assignment_output.equals(correct_output)) { + // This test passes + passed_tests++; + stdout.println("Test Pass"); + } else { + // This test does not pass + stdout.println("Test Fail:"); + stdout.println("Expected:"); + stdout.println(correct_output); + stdout.println("Actual:"); + stdout.println(assignment_output); + } + + // Indicate this test is done + stdout.println("====="); + } + stdout.printf("Result: %d/%d Tests Passed\n", passed_tests, total_tests); + System.setOut(stdout); + + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/labMaxMin/inputs/test01.txt b/labMaxMin/inputs/test01.txt new file mode 100644 index 0000000..6ed281c --- /dev/null +++ b/labMaxMin/inputs/test01.txt @@ -0,0 +1,2 @@ +1 +1 diff --git a/labMaxMin/inputs/test02.txt b/labMaxMin/inputs/test02.txt new file mode 100644 index 0000000..5357a4a --- /dev/null +++ b/labMaxMin/inputs/test02.txt @@ -0,0 +1,3 @@ +2 +1 +2 diff --git a/labMaxMin/inputs/test03.txt b/labMaxMin/inputs/test03.txt new file mode 100644 index 0000000..9873ee0 --- /dev/null +++ b/labMaxMin/inputs/test03.txt @@ -0,0 +1,4 @@ +3 +1 +2 +3 diff --git a/labMaxMin/inputs/test04.txt b/labMaxMin/inputs/test04.txt new file mode 100644 index 0000000..4934959 --- /dev/null +++ b/labMaxMin/inputs/test04.txt @@ -0,0 +1,5 @@ +4 +1 +2 +3 +4 diff --git a/labMaxMin/inputs/test05.txt b/labMaxMin/inputs/test05.txt new file mode 100644 index 0000000..74fc6f9 --- /dev/null +++ b/labMaxMin/inputs/test05.txt @@ -0,0 +1,6 @@ +5 +1 +2 +3 +4 +5 diff --git a/labMaxMin/labMaxMin.iml b/labMaxMin/labMaxMin.iml new file mode 100644 index 0000000..bd627fe --- /dev/null +++ b/labMaxMin/labMaxMin.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/labMaxMin/outputs/test01.txt b/labMaxMin/outputs/test01.txt new file mode 100644 index 0000000..fea3825 --- /dev/null +++ b/labMaxMin/outputs/test01.txt @@ -0,0 +1 @@ +1;1 \ No newline at end of file diff --git a/labMaxMin/outputs/test02.txt b/labMaxMin/outputs/test02.txt new file mode 100644 index 0000000..fe821a1 --- /dev/null +++ b/labMaxMin/outputs/test02.txt @@ -0,0 +1 @@ +2;1 \ No newline at end of file diff --git a/labMaxMin/outputs/test03.txt b/labMaxMin/outputs/test03.txt new file mode 100644 index 0000000..dfb65e1 --- /dev/null +++ b/labMaxMin/outputs/test03.txt @@ -0,0 +1 @@ +3;1 \ No newline at end of file diff --git a/labMaxMin/outputs/test04.txt b/labMaxMin/outputs/test04.txt new file mode 100644 index 0000000..9966f7d --- /dev/null +++ b/labMaxMin/outputs/test04.txt @@ -0,0 +1 @@ +4;1 \ No newline at end of file diff --git a/labMaxMin/outputs/test05.txt b/labMaxMin/outputs/test05.txt new file mode 100644 index 0000000..6885eda --- /dev/null +++ b/labMaxMin/outputs/test05.txt @@ -0,0 +1 @@ +5;1 \ No newline at end of file