Пример #1
0
        public void TestSortByMultipleFields(int workersCount)
        {
            string[] records = new string[]
            {
                "2. e",
                "1. c",
                "2. a",
                "1. a",
                "2. d",

                "3. b",
                "3. a",
                "2. e",
                "3. c",
                "2. a",

                "1. a",
                "2. d",
                "1. b",
                "3. a",
                "1. e",

                "1. c",
                "2. a",
                "1. a",
                "3. d",

                "5. a",
                "7. a",
                "9. a",
                "4. a",
                "6. a",
            };

            int            sizeOfRecord = System.Runtime.InteropServices.Marshal.SizeOf <Record>() + 2;
            int            bufSize      = sizeOfRecord * 6 * workersCount;
            TempStreams    tempStreams  = new TempStreams();
            BigTableSorter tableSorter  = new BigTableSorter(tempStreams, bufSize, workersCount);

            MemoryStream input  = StringsToStream(records);
            MemoryStream output = new MemoryStream();

            tableSorter.Sort(input, new int[] { 1, 0 }, output);

            string[] expectedRecords = new string[]
            {
                "1. a",
                "1. a",
                "1. a",
                "2. a",
                "2. a",
                "2. a",
                "3. a",
                "3. a",
                "4. a",
                "5. a",
                "6. a",
                "7. a",
                "9. a",

                "1. b",
                "3. b",

                "1. c",
                "1. c",
                "3. c",

                "2. d",
                "2. d",
                "3. d",

                "1. e",
                "2. e",
                "2. e",
            };
            string expectedRecordsStr = string.Join("\r\n", expectedRecords) + "\r\n";

            output.Position = 0;
            string result = new StreamReader(output).ReadToEnd();

            Assert.Equal(expectedRecordsStr, result);
        }
Пример #2
0
        public void TestSortMultipleBlocks(int workersCount)
        {
            string[] records = new string[]
            {
                "2. e",
                "1. c",
                "2. a",
                "1. a",
                "1. d",

                "1. b",
                "3. a",
                "2. e",
                "1. c",
                "2. a",

                "1. a",
                "1. d",
                "1. b",
                "3. a",
                "2. e",

                "1. c",
                "2. a",
                "1. a",
                "1. d",
            };

            int            sizeOfRecord = System.Runtime.InteropServices.Marshal.SizeOf <Record>() + 2;
            int            bufSize      = sizeOfRecord * 5 * workersCount;
            TempStreams    tempStreams  = new TempStreams();
            BigTableSorter tableSorter  = new BigTableSorter(tempStreams, bufSize, workersCount);

            MemoryStream input  = StringsToStream(records);
            MemoryStream output = new MemoryStream();

            tableSorter.Sort(input, 1, output);

            Assert.Equal(4, tempStreams.Blocks[0].Count);

            string[][] expectedBlockRecords = new string[][]
            {
                new string[]
                {
                    "2. a",
                    "1. a",
                    "1. c",
                    "1. d",
                    "2. e",
                },

                new string[]
                {
                    "3. a",
                    "2. a",
                    "1. b",
                    "1. c",
                    "2. e",
                },

                new string[]
                {
                    "1. a",
                    "3. a",
                    "1. b",
                    "1. d",
                    "2. e",
                },

                new string[]
                {
                    "2. a",
                    "1. a",
                    "1. c",
                    "1. d",
                }
            };

            for (int i = 0; i < expectedBlockRecords.Length; i++)
            {
                Assert.Equal(expectedBlockRecords[i], BytesToStrings(tempStreams.Blocks[0][i]));
            }
            string[] expectedRecords = new string[]
            {
                "2. a",
                "1. a",
                "3. a",
                "2. a",
                "2. a",

                "1. a",
                "1. a",
                "3. a",
                "1. b",
                "1. b",

                "1. c",
                "1. c",
                "1. c",
                "1. d",
                "1. d",

                "1. d",
                "2. e",
                "2. e",
                "2. e",
            };
            string expectedRecordsStr = string.Join("\r\n", expectedRecords) + "\r\n";

            output.Position = 0;
            string result = new StreamReader(output).ReadToEnd();

            Assert.Equal(expectedRecordsStr, result);
        }
Пример #3
0
        public void TestSortSingleBlock(int workersCount)
        {
            string[] records = new string[]
            {
                "2. e",
                "1. c",
                "2. a",
                "1. a",
                "1. d",
                "1. b",
                "3. a",
                "2. e",
                "1. c",
                "2. a",
                "1. a",
                "1. d",
                "1. b",
                "3. a",
                "2. e",
                "1. c",
                "2. a",
                "1. a",
                "1. d",
            };

            int            sizeOfRecord = System.Runtime.InteropServices.Marshal.SizeOf <Record>() + 2;
            int            bufSize      = sizeOfRecord * records.Length * workersCount;
            TempStreams    tempStreams  = new TempStreams();
            BigTableSorter tableSorter  = new BigTableSorter(tempStreams, bufSize, workersCount);

            MemoryStream input  = StringsToStream(records);
            MemoryStream output = new MemoryStream();

            tableSorter.Sort(input, 1, output);

            Assert.Empty(tempStreams.Blocks);

            string[] expectedRecords = new string[]
            {
                "2. a",
                "2. a",
                "1. a",
                "2. a",
                "3. a",
                "1. a",
                "1. a",
                "3. a",
                "1. b",
                "1. b",
                "1. c",
                "1. c",
                "1. c",
                "1. d",
                "1. d",
                "1. d",
                "2. e",
                "2. e",
                "2. e",
            };
            string expectedRecordsStr = string.Join("\r\n", expectedRecords) + "\r\n";

            output.Position = 0;
            string result = new StreamReader(output).ReadToEnd();

            Assert.Equal(expectedRecordsStr, result);
        }