public RandomTextInputStream(Int64 length, System.Text.Encoding encoding)
     : base()
 {
     _desiredLength = length;
     _rtg           = new RandomTextGenerator();
     _encoding      = encoding;
     _randomText    = new byte[_chunks][];
     _rnd           = new System.Random();
 }
Пример #2
0
        internal static void CreateAndFillFileText(string filename, Int64 size, System.Action <Int64> update)
        {
            Int64 bytesRemaining = size;

            if (size > 128 * 1024)
            {
                RandomTextGenerator rtg = new RandomTextGenerator();

                // fill the file with text data, selecting large blocks at a time
                using (StreamWriter sw = File.CreateText(filename))
                {
                    do
                    {
                        string generatedText = rtg.Generate(32 * 1024);
                        sw.Write(generatedText);
                        sw.Write("\n\n");
                        bytesRemaining -= (generatedText.Length + 2);

                        if (update != null)
                        {
                            update(size - bytesRemaining);
                        }
                    } while (bytesRemaining > 0);
                }
            }
            else
            {
                // fill the file with text data, selecting one word at a time
                using (StreamWriter sw = File.CreateText(filename))
                {
                    do
                    {
                        // pick a word at random
                        string selectedWord = LoremIpsumWords[_rnd.Next(LoremIpsumWords.Length)];
                        if (bytesRemaining < selectedWord.Length + 1)
                        {
                            sw.Write(selectedWord.Substring(0, (int)bytesRemaining));
                            bytesRemaining = 0;
                        }
                        else
                        {
                            sw.Write(selectedWord);
                            sw.Write(" ");
                            bytesRemaining -= (selectedWord.Length + 1);
                        }
                        if (update != null)
                        {
                            update(size - bytesRemaining);
                        }
                    } while (bytesRemaining > 0);
                    sw.Close();
                }
            }
        }
Пример #3
0
        public RandomTextInputStream(Int64 length, System.Text.Encoding encoding)
            : base()
        {
            _desiredLength = length;
            _rtg           = new RandomTextGenerator();
            _encoding      = encoding;
            _nblocks       = 0;
            _p             = 0;
            _isDisposed    = false;
            _randomText    = new string[2];
            _needData      = new AutoResetEvent(false);
            _haveData      = new AutoResetEvent(false);
            _producerDone  = new ManualResetEvent(false);

            ThreadPool.QueueUserWorkItem(new WaitCallback(_Producer));
        }
Пример #4
0
        public void SortedSave()
        {
            var rtg = new RandomTextGenerator();

            WriteDelegate writer = (name, stream) =>
                {
                    byte[] buffer = System.Text.Encoding.ASCII.GetBytes(rtg.Generate(_rnd.Next(2000) + 200));
                    stream.Write(buffer, 0, buffer.Length);
                };

            int numEntries = _rnd.Next(256) + 48;

            // Two trials, one with sorted output, and the other with non-sorted output.
            for (int m = 0; m < 2; m++)
            {
                string zipFileToCreate = Path.Combine(TopLevelDir,
                                                      String.Format("SortedSave-{0}.zip", m));
                using (var zip = new ZipFile())
                {
                    for (int i = 0; i < numEntries; i++)
                    {
                        // I need the randomness in the first part, to force the sort.
                        string filename = String.Format("{0}-{1:000}.txt",
                                                        TestUtilities.GenerateRandomAsciiString(6), i);
                        zip.AddEntry(filename, writer);
                    }

                    zip.SortEntriesBeforeSaving = (m == 1);
                    zip.Save(zipFileToCreate);
                }

                using (var zip = FileSystemZip.Read(zipFileToCreate))
                {
                    bool sorted = true;
                    for (int i = 0; i < zip.Entries.Count - 1 && sorted; i++)
                    {
                        for (int j = i; j < zip.Entries.Count && sorted; j++)
                        {
                            if (String.Compare(zip[i].FileName, zip[j].FileName, StringComparison.OrdinalIgnoreCase) > 0)
                            {
                                sorted = false;
                            }
                        }
                    }

                    Assert.IsTrue((((m == 1) && sorted) || ((m == 0) && !sorted)),
                        "Unexpected sort results");
                }
            }
        }
Пример #5
0
        internal static void CreateAndFillFileText(string filename,
                                                   Int64 size,
                                                   Action <Int64> update)
        {
            Int64 bytesRemaining = size;

            if (size > 128 * 1024)
            {
                var rnd = new System.Random();
                RandomTextGenerator rtg = new RandomTextGenerator();
                int chunkSize           = 48 * 1024;
                int variationSize       = 2 * 1024;
                var newLinePair         = System.Text.Encoding.ASCII.GetBytes("\n\n");
                int nCycles             = 0;
                // fill the file with text data, selecting large blocks at a time
                var fodder = new byte[32][];
                using (var fs = File.Create(filename))
                {
                    do
                    {
                        int n = rnd.Next(fodder.Length);
                        if (fodder[n] == null)
                        {
                            string generatedText = rtg.Generate(chunkSize);
                            fodder[n] = System.Text.Encoding.ASCII.GetBytes(generatedText);
                        }

                        var bytes = fodder[n];
                        int len   = bytes.Length - rnd.Next(variationSize);
                        fs.Write(bytes, 0, len);
                        bytesRemaining -= len;
                        fs.Write(newLinePair, 0, newLinePair.Length);
                        bytesRemaining -= newLinePair.Length;
                        nCycles++;
                        if ((nCycles % 1024) == 0)
                        {
                            if (update != null)
                            {
                                update(size - bytesRemaining);
                            }
                        }
                    } while (bytesRemaining > 0);
                }
            }
            else
            {
                // fill the file with text data, selecting one word at a time
                using (StreamWriter sw = File.CreateText(filename))
                {
                    do
                    {
                        // pick a word at random
                        string selectedWord = LoremIpsumWords[_rnd.Next(LoremIpsumWords.Length)];
                        if (bytesRemaining < selectedWord.Length + 1)
                        {
                            sw.Write(selectedWord.Substring(0, (int)bytesRemaining));
                            bytesRemaining = 0;
                        }
                        else
                        {
                            sw.Write(selectedWord);
                            sw.Write(" ");
                            bytesRemaining -= (selectedWord.Length + 1);
                        }
                        if (update != null)
                        {
                            update(size - bytesRemaining);
                        }
                    } while (bytesRemaining > 0);
                    sw.Close();
                }
            }
        }
        internal static void CreateAndFillFileText(string filename,
                                                   Int64 size,
                                                   Action<Int64> update)
        {
            Int64 bytesRemaining = size;

            if (size > 128 * 1024)
            {
                var rnd = new System.Random();
                RandomTextGenerator rtg = new RandomTextGenerator();
                int chunkSize = 48 * 1024;
                int variationSize = 2 * 1024;
                var newLinePair = Encoding.ASCII.GetBytes("\n\n");
                int nCycles = 0;
                // fill the file with text data, selecting large blocks at a time
                var fodder = new byte[32][];
                using (var fs = File.Create(filename))
                {
                    do
                    {
                        int n = rnd.Next(fodder.Length);
                        if (fodder[n] == null)
                        {
                            string generatedText = rtg.Generate(chunkSize);
                            fodder[n] = Encoding.ASCII.GetBytes(generatedText);
                        }

                        var bytes = fodder[n];
                        int len = bytes.Length - rnd.Next(variationSize);
                        fs.Write(bytes,0,len);
                        bytesRemaining -= len;
                        fs.Write(newLinePair, 0, newLinePair.Length);
                        bytesRemaining -= newLinePair.Length;
                        nCycles++;
                        if ((nCycles % 1024) == 0)
                        {
                            if (update != null)
                                update(size - bytesRemaining);
                        }
                    } while (bytesRemaining > 0);
                }
            }
            else
            {
                // fill the file with text data, selecting one word at a time
                using (StreamWriter sw = File.CreateText(filename))
                {
                    do
                    {
                        // pick a word at random
                        string selectedWord = LoremIpsumWords[_rnd.Next(LoremIpsumWords.Length)];
                        if (bytesRemaining < selectedWord.Length + 1)
                        {
                            sw.Write(selectedWord.Substring(0, (int)bytesRemaining));
                            bytesRemaining = 0;
                        }
                        else
                        {
                            sw.Write(selectedWord);
                            sw.Write(" ");
                            bytesRemaining -= (selectedWord.Length + 1);
                        }
                        if (update != null)
                            update(size - bytesRemaining);

                    } while (bytesRemaining > 0);
                    sw.Close();
                }
            }
        }
Пример #7
0
 public RandomTextInputStream(Int64 length, System.Text.Encoding encoding)
     : base()
 {
     _desiredLength = length;
     _rtg = new RandomTextGenerator();
     _encoding = encoding;
     _randomText = new byte[_chunks][];
     _rnd = new System.Random();
 }