001package algs11;
002import java.util.Arrays;
003import stdlib.*;
004
005public class PlaygroundMerge {
006        /* Merge a and b into a sorted result */
007        public static double[] merge (double[] a, double[] b) {
008                return null; //TODO: fix this
009        }
010
011        /* A main function for testing */
012        public static void main (String[] args) {  
013                //Trace.drawStepsOfMethod ("merge");
014                //Trace.run ();
015                testMerge ("11 13 15", "11 15", "13");
016                testMerge ("11 13 15", "13", "11 15");
017                testMerge ("11 12", "11", "12");
018                testMerge ("11 12", "12", "11");
019                testMerge ("11", "11", "");
020                testMerge ("11", "", "11");
021                testMerge ("", "", "");
022
023                // this is an example of random testing:
024                for (int numTests = 10; numTests>0; numTests--) {
025                        double[] a = ArrayGenerator.doubleRandom (StdRandom.uniform (10), 1000);
026                        double[] b = ArrayGenerator.doubleRandom (StdRandom.uniform (10), 1000);
027                        Arrays.sort (a);
028                        Arrays.sort (b);
029                        double[] result = new double[a.length+b.length];
030                        System.arraycopy (a, 0, result, 0, a.length);
031                        System.arraycopy (b, 0, result, a.length, b.length);
032                        Arrays.sort (result);
033                        testMerge (result, a, b);
034                }
035                StdOut.println ("Finished tests");
036        }
037        
038        private static void testMerge (double[] expected, double[] a, double[] b) {
039                double[] actual = merge (a, b);
040                if (!Arrays.equals (expected, actual)) {
041                        StdOut.format ("Failed merge([%s],[%s]): Expecting (%s) Actual (%s)\n", Arrays.toString (a), Arrays.toString (b), Arrays.toString (expected), Arrays.toString (actual));
042                }
043        }
044        private static void testMerge (String expected, String aList, String bList) {
045                double[] a = doublesFromString (aList);
046                double[] b = doublesFromString (bList);
047                double[] actual = merge (a, b);
048                if (!Arrays.equals (a, doublesFromString (aList))) {
049                        StdOut.format ("Failed merge([%s]): Array modified\n", aList);
050                }
051                if (!Arrays.equals (b, doublesFromString (bList))) {
052                        StdOut.format ("Failed merge([%s]): Array modified\n", bList);
053                }
054                double[] aExpected = doublesFromString (expected);
055                if (!Arrays.equals (aExpected, actual)) {
056                        StdOut.format ("Failed merge([%s],[%s]): Expecting (%s) Actual (%s)\n", aList, bList, Arrays.toString (aExpected), Arrays.toString (actual));
057                }
058        }
059
060        /* A utility function to create an array of doubles from a string. */
061        // The string should include a list of numbers, separated by single spaces.
062        private static double[] doublesFromString (String s) {
063                if ("".equals (s)) return new double[0]; // empty array is a special case
064                String[] nums = s.split (" ");
065                double[] result = new double[nums.length];
066                for (int i = nums.length - 1; i >= 0; i--) {
067                        try {
068                                result[i] = Double.parseDouble (nums[i]);
069                        } catch (NumberFormatException e) {
070                                throw new IllegalArgumentException (String.format ("Bad argument \"%s\": could not parse \"%s\" as a double", s, nums[i]));
071                        }
072                }
073                return result;
074        }
075}