From a7f3ebfe50a61818bfdf7b0a95e757a3a9cdf84b Mon Sep 17 00:00:00 2001 From: M-H9 Date: Mon, 13 Jan 2025 22:58:28 +0100 Subject: [PATCH] Converted IMPL_M_1 to Generic --- .../powersort/MSort/PowerSortT.java | 89 +++++++++++++++++++ .../powersort/MSort/PowerSortTest.java | 44 +++++++++ .../powersort/msort/PowerSortTest.java | 37 -------- 3 files changed, 133 insertions(+), 37 deletions(-) create mode 100644 app/src/test/java/de/uni_marburg/powersort/MSort/PowerSortT.java create mode 100644 app/src/test/java/de/uni_marburg/powersort/MSort/PowerSortTest.java delete mode 100644 app/src/test/java/de/uni_marburg/powersort/msort/PowerSortTest.java diff --git a/app/src/test/java/de/uni_marburg/powersort/MSort/PowerSortT.java b/app/src/test/java/de/uni_marburg/powersort/MSort/PowerSortT.java new file mode 100644 index 0000000..8a0bd76 --- /dev/null +++ b/app/src/test/java/de/uni_marburg/powersort/MSort/PowerSortT.java @@ -0,0 +1,89 @@ +package de.uni_marburg.powersort.MSort; + +import static de.uni_marburg.powersort.MSort.IMPL_M_1.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public class PowerSortT { + + public static void main(String[] args) { + testFillWithAscRunsHighToLow(); + testMerge(); + testMergeInplace(); + testExtendRun(); + testPower(); + testPowerFast(); + testMergeTopmost2(); + testPowerSort(); + } + + // Test for fillWithAscRunsHighToLow + public static void testFillWithAscRunsHighToLow() { + List A = new ArrayList<>(Collections.nCopies(10, 0)); + int[] runLengths = {2, 3, 5}; + int runLenFactor = 1; + fillWithAscRunsHighToLow(A, runLengths, runLenFactor); + System.out.println("Test fillWithAscRunsHighToLow: " + A); + } + + // Test for merge + public static void testMerge() { + List run1 = new ArrayList<>(Arrays.asList(1, 4, 6)); + List run2 = new ArrayList<>(Arrays.asList(2, 3, 5)); + List result = merge(run1, run2); + System.out.println("Test merge: " + result); + } + + // Test for mergeInplace + public static void testMergeInplace() { + List A = new ArrayList<>(Arrays.asList(1, 4, 6, 2, 3, 5)); + mergeInplace(A, 0, 3, 6); + System.out.println("Test mergeInplace: " + A); + } + + // Test for extendRun + public static void testExtendRun() { + List A = new ArrayList<>(Arrays.asList(1, 2, 3, 6, 5, 4)); + int endIndex = extendRun(A, 0); + System.out.println("Test extendRun (from 0): " + endIndex); + System.out.println("Modified List: " + A); + } + + // Test for power + public static void testPower() { + int[] run1 = {0, 3}; + int[] run2 = {3, 3}; + int n = 6; + int powerValue = power(run1, run2, n); + System.out.println("Test power: " + powerValue); + } + + // Test for powerFast + public static void testPowerFast() { + int[] run1 = {0, 3}; + int[] run2 = {3, 3}; + int n = 6; + int powerFastValue = powerFast(run1, run2, n); + System.out.println("Test powerFast: " + powerFastValue); + } + + // Test for mergeTopmost2 + public static void testMergeTopmost2() { + List A = new ArrayList<>(Arrays.asList(1, 3, 5, 2, 4, 6)); + List runs = new ArrayList<>(); + runs.add(new int[]{0, 3, 1}); + runs.add(new int[]{3, 3, 1}); + mergeTopmost2(A, runs); + System.out.println("Test mergeTopmost2: " + A); + } + + // Test for powerSort + public static void testPowerSort() { + List A = new ArrayList<>(Arrays.asList(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)); + powerSort(A); + System.out.println("Test powerSort: " + A); + } + } diff --git a/app/src/test/java/de/uni_marburg/powersort/MSort/PowerSortTest.java b/app/src/test/java/de/uni_marburg/powersort/MSort/PowerSortTest.java new file mode 100644 index 0000000..1d1bb78 --- /dev/null +++ b/app/src/test/java/de/uni_marburg/powersort/MSort/PowerSortTest.java @@ -0,0 +1,44 @@ +package de.uni_marburg.powersort.MSort; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import org.junit.jupiter.api.Test; + +import static de.uni_marburg.powersort.MSort.IMPL_M_1.MERGE_COST; +import static de.uni_marburg.powersort.MSort.IMPL_M_1.fillWithAscRunsHighToLow; +import static de.uni_marburg.powersort.MSort.IMPL_M_1.powerSort; + +class PowerSortTest { + @Test + public void testWithOneInputList() { + // List list = new ArrayList<>(List.of(5, 2, 8, 12, 1, 6)); + // extendRun(list, 0); +//System.out.println(list); + // example from slides he wrote this + int[] runs = {5, 3, 3, 14, 1, 2}; + runs = new int[]{9, 16, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7}; + + List a = new ArrayList<>(IntStream.range(0, Arrays.stream(runs).sum()).boxed().collect(Collectors.toList())); + + System.out.println(); + fillWithAscRunsHighToLow(a, runs, 1); + MERGE_COST = 0; + System.out.println("Sorting with Powersort:"); + powerSort(a); + System.out.println("Merge cost: " + MERGE_COST); + } + + @Test + public void testWithFinnInputList() { + List numbers = List.of(new Integer[] {24, 25, 26, 27, 28, 21, 22, 23, 18, 19, 20, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 3, 1, 2}); + + powerSort(numbers); + System.out.println("Result: "); + System.out.println(new ArrayList<>(List.of(numbers))); + + } +} diff --git a/app/src/test/java/de/uni_marburg/powersort/msort/PowerSortTest.java b/app/src/test/java/de/uni_marburg/powersort/msort/PowerSortTest.java deleted file mode 100644 index dc2bfa5..0000000 --- a/app/src/test/java/de/uni_marburg/powersort/msort/PowerSortTest.java +++ /dev/null @@ -1,37 +0,0 @@ -package de.uni_marburg.powersort.msort; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.IntStream; - -import org.junit.jupiter.api.Test; - -import static de.uni_marburg.powersort.msort.IMPL_M_1.MERGE_COST; -import static de.uni_marburg.powersort.msort.IMPL_M_1.extendRun; -import static de.uni_marburg.powersort.msort.IMPL_M_1.fillWithAscRunsHighToLow; -import static de.uni_marburg.powersort.msort.IMPL_M_1.powerSort; - -class PowerSortTest { - @Test - - public void testWithOneInputList() { - List list = new ArrayList<>(List.of(5, 2, 8, 12, 1, 6)); - extendRun(list, 0); - System.out.println(list); - // example from slides he wrote this - int[] runs = {5, 3, 3, 14, 1, 2}; - // runs = new int[]{9, 16, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7}; - - List a = new ArrayList<>(IntStream.range(0, Arrays.stream(runs).sum()).boxed().collect(Collectors.toList())); - - System.out.println(); - fillWithAscRunsHighToLow(a, runs, 1); - MERGE_COST = 0; - System.out.println("Sorting with Powersort:"); - powerSort(a); - System.out.println("Merge cost: " + MERGE_COST); - } - - } \ No newline at end of file