public void EncodingCancellationTest()
        {
            WritableMemoryStream outputStream = new WritableMemoryStream();

            platform.WriteFileFunc = _ => outputStream;

            fileSystem.EnumFileSystemEntriesFunc = _ => {
                return(new[] {
                    new FileSystemEntry(FileSystemEntryType.Directory, "dir", @"C:\dir\"),
                    new FileSystemEntry(FileSystemEntryType.File, "f1.dat", @"C:\dir\f1.dat"),
                    new FileSystemEntry(FileSystemEntryType.File, "f2.dat", @"C:\dir\f2.dat"),
                });
            };
            using (CancellationTokenSource cts = new CancellationTokenSource()) {
                int fileRequest = 0;
                platform.ReadFileFunc = x => {
                    if (++fileRequest == 4)
                    {
                        cts.Cancel();
                    }
                    switch (x)
                    {
                    case @"C:\dir\f1.dat": return(new MemoryStream(new byte[200 * 1024]));

                    case @"C:\dir\f2.dat": return(new MemoryStream(new byte[300 * 1024]));

                    default: throw new NotImplementedException();
                    }
                };
                Assert.ThrowsAsync <OperationCanceledException>(async() => await service.EncodeAsync(@"C:\dir\", @"C:\outputFile.dat", cts.Token, null));
                Assert.AreEqual(32855, outputStream.Length);
            }
        }
        public async Task EncodingStatisticsTest2()
        {
            WritableMemoryStream outputStream = new WritableMemoryStream();

            platform.WriteFileFunc = _ => outputStream;

            fileSystem.EnumFileSystemEntriesFunc = _ => {
                return(new[] {
                    new FileSystemEntry(FileSystemEntryType.Directory, "dir", @"C:\dir\"),
                    new FileSystemEntry(FileSystemEntryType.File, "f1.dat", @"C:\dir\f1.dat"),
                    new FileSystemEntry(FileSystemEntryType.File, "f2.dat", @"C:\dir\f2.dat"),
                });
            };
            platform.ReadFileFunc = x => {
                switch (x)
                {
                case @"C:\dir\f1.dat": return(new MemoryStream(new byte[200 * 1024]));

                case @"C:\dir\f2.dat": return(new MemoryStream(new byte[300 * 1024]));

                default: throw new NotImplementedException();
                }
            };

            EncodingStatistics statistics = await service.EncodeAsync(@"C:\dir\", @"C:\outputFile.dat", CancellationToken.None, null);

            Assert.IsNotNull(statistics);
            Assert.AreEqual(500 * 1024, statistics.InputSize);
            Assert.AreEqual(64095, statistics.OutputSize);
        }
Esempio n. 3
0
        public async Task EncodingStatisticsTest()
        {
            TestIFileSelectorService fileSelector = new TestIFileSelectorService();

            fileSelector.FilePath         = @"path";
            viewModel.FileSelectorService = fileSelector;
            viewModel.Path = @"C:\dir\";

            TestIInputDataService inputDataService = new TestIInputDataService();

            inputDataService.InputCommand = InputCommand.Encode;
            viewModel.InputDataService    = inputDataService;

            TestIFileSystemService fileSystem = new TestIFileSystemService();

            fileSystem.EnumFileSystemEntriesFunc = _ => {
                return(new[] {
                    new FileSystemEntry(FileSystemEntryType.Directory, "dir", @"C:\dir\"),
                    new FileSystemEntry(FileSystemEntryType.File, "f1.dat", @"C:\dir\f1.dat"),
                    new FileSystemEntry(FileSystemEntryType.File, "f2.dat", @"C:\dir\f2.dat"),
                });
            };

            WritableMemoryStream outputStream = new WritableMemoryStream();
            TestIPlatformService platform     = new TestIPlatformService();

            platform.WriteFileFunc = _ => outputStream;

            platform.ReadFileFunc = x => {
                switch (x)
                {
                case @"C:\dir\f1.dat": return(new MemoryStream(new byte[200 * 1024]));

                case @"C:\dir\f2.dat": return(new MemoryStream(new byte[300 * 1024]));

                default: throw new NotImplementedException();
                }
            };
            viewModel.EncodingService = new DefaultHuffmanEncodingService(fileSystem, platform, new DefaultStreamBuilder());

            await viewModel.Run();

            window.UpdateLayout();

            TextBlock inputSizeTextBlock  = window.EncodingResultControl.GetContentTemplateControl <TextBlock>("InputSizeTextBlock");
            TextBlock outputSizeTextBlock = window.EncodingResultControl.GetContentTemplateControl <TextBlock>("OutputSizeTextBlock");
            TextBlock saveFactorTextBlock = window.EncodingResultControl.GetContentTemplateControl <TextBlock>("SaveFactorTextBlock");

            Assert.AreEqual("Input Size: 512 Kb", inputSizeTextBlock.Text);
            Assert.AreEqual("Output Size: 64.1 Kb", outputSizeTextBlock.Text);
            Assert.AreEqual("Save Factor: 87.48%", saveFactorTextBlock.Text);
        }
Esempio n. 4
0
        public async Task EncodingCancellationTest1()
        {
            TestIFileSelectorService fileSelector = new TestIFileSelectorService();

            fileSelector.FilePath         = @"path";
            viewModel.FileSelectorService = fileSelector;
            viewModel.Path = @"C:\dir\";

            TestIInputDataService inputDataService = new TestIInputDataService();

            inputDataService.InputCommand = InputCommand.Encode;
            viewModel.InputDataService    = inputDataService;

            TestIFileSystemService fileSystem = new TestIFileSystemService();

            fileSystem.EnumFileSystemEntriesFunc = _ => {
                return(new[] {
                    new FileSystemEntry(FileSystemEntryType.Directory, "dir", @"C:\dir\"),
                    new FileSystemEntry(FileSystemEntryType.File, "f1.dat", @"C:\dir\f1.dat"),
                    new FileSystemEntry(FileSystemEntryType.File, "f2.dat", @"C:\dir\f2.dat"),
                });
            };

            WritableMemoryStream outputStream = new WritableMemoryStream();
            TestIPlatformService platform     = new TestIPlatformService();

            platform.WriteFileFunc = _ => outputStream;

            int fileRequest = 0;

            platform.ReadFileFunc = x => {
                if (++fileRequest == 4)
                {
                    viewModel.Cancel();
                }
                switch (x)
                {
                case @"C:\dir\f1.dat": return(new MemoryStream(new byte[200 * 1024]));

                case @"C:\dir\f2.dat": return(new MemoryStream(new byte[300 * 1024]));

                default: throw new NotImplementedException();
                }
            };
            viewModel.EncodingService = new DefaultHuffmanEncodingService(fileSystem, platform, new DefaultStreamBuilder());

            await viewModel.Run();

            Assert.AreEqual(32855, outputStream.Length);
        }
Esempio n. 5
0
        public async Task EncodingCancellationTest2()
        {
            TestIFolderSelectorService folderSelector = new TestIFolderSelectorService();

            folderSelector.FolderPath       = @"C:\folder\";
            viewModel.FolderSelectorService = folderSelector;
            viewModel.Path = @"C:\dir\";

            TestIInputDataService inputDataService = new TestIInputDataService();

            inputDataService.InputCommand = InputCommand.Decode;
            viewModel.InputDataService    = inputDataService;

            WritableMemoryStream outputStream = new WritableMemoryStream();
            TestIPlatformService platform     = new TestIPlatformService();

            platform.WriteFileFunc = x => {
                if (string.Equals(x, @"C:\folder\dir\f2.dat", StringComparison.OrdinalIgnoreCase))
                {
                    viewModel.Cancel();
                }
                return(outputStream);
            };
            platform.ReadFileFunc = x => new BufferBuilder()
                                    .AddByte(0x2)
                                    .AddInt(0x9)
                                    .AddByte(0x0)
                                    .AddLong(0x1)
                                    .AddByte(0x1)
                                    .AddInt(0x6)
                                    .AddString("dir")
                                    .AddInt(2)
                                    .AddByte(0x0)
                                    .AddInt(0xC)
                                    .AddString("f1.dat")
                                    .AddLong(200 * 1024 * 8) // 200Kb
                                    .AddByte(0x0, 200 * 1024)
                                    .AddByte(0x0)
                                    .AddInt(0xC)
                                    .AddString("f2.dat")
                                    .AddLong(300 * 1024 * 8) // 300Kb
                                    .AddByte(0x0, 300 * 1024).GetStream();

            viewModel.DecodingService = new DefaultHuffmanDecodingService(platform, new DefaultStreamParser());
            await viewModel.Run();

            Assert.AreEqual(256 * 1024 * 8, outputStream.Length);
        }
        public async Task EncodingStatisticsTest1()
        {
            fileSystem.EnumFileSystemEntriesFunc = _ => {
                return(new[] {
                    new FileSystemEntry(FileSystemEntryType.File, "file.dat", @"C:\dir\file.dat"),
                });
            };
            WritableMemoryStream outputStream = new WritableMemoryStream();

            platform.WriteFileFunc = _ => outputStream;
            platform.ReadFileFunc  = x => new MemoryStream(new byte[0]);

            EncodingStatistics statistics = await service.EncodeAsync(@"C:\dir\", @"C:\outputFile.dat", CancellationToken.None, null);

            Assert.IsNotNull(statistics);
            Assert.AreEqual(0, statistics.InputSize);
            Assert.AreEqual(42, statistics.OutputSize);
        }
        public void DecodingCancellationTest()
        {
            platform.ReadFileFunc = x => new BufferBuilder()
                                    .AddByte(0x2)
                                    .AddInt(0x9)
                                    .AddByte(0x0)
                                    .AddLong(0x1)
                                    .AddByte(0x1)
                                    .AddInt(0x6)
                                    .AddString("dir")
                                    .AddInt(2)
                                    .AddByte(0x0)
                                    .AddInt(0xC)
                                    .AddString("f1.dat")
                                    .AddLong(200 * 1024 * 8) // 200Kb
                                    .AddByte(0x0, 200 * 1024)
                                    .AddByte(0x0)
                                    .AddInt(0xC)
                                    .AddString("f2.dat")
                                    .AddLong(300 * 1024 * 8) // 300Kb
                                    .AddByte(0x0, 300 * 1024).GetStream();

            const string         f2           = @"C:\Root\dir\f2.dat";
            WritableMemoryStream outputStream = new WritableMemoryStream();

            using (CancellationTokenSource cts = new CancellationTokenSource()) {
                platform.WriteFileFunc += x => {
                    if (string.Equals(x, f2, StringComparison.OrdinalIgnoreCase))
                    {
                        cts.Cancel();
                    }
                    return(outputStream);
                };
                Assert.ThrowsAsync <OperationCanceledException>(async() => await service.DecodeAsync(@"C:\InputFile.dat", @"C:\Root\", cts.Token, null));
                Assert.AreEqual(256 * 1024 * 8, outputStream.Length);
            }
        }