Exemple #1
0
        public void Test_Map1252Search()
        {
            for (var ix = 0; ix < Map1252.Length; ++ix)
            {
                var expectedResult = Map1252.At(ix) & 0xFF;
                var result         = Map1252.To1252Bestfit(Map1252.At(ix) >> 8);

                Assert.IsTrue(expectedResult > 0);
                Assert.IsTrue(result == expectedResult);
            }

            var minvalResult = Map1252.To1252Bestfit(0);
            var maxvalResult = Map1252.To1252Bestfit(0x10FFFF);

            Assert.IsTrue(minvalResult == 0);

            // U+10FFFF not expected and not actually against the spec.
            Assert.IsTrue(maxvalResult < 0);
        }
Exemple #2
0
        static void Main(string[] args)
        {
            long tot0 = 0, tot1 = 0;
            var  watch = new Stopwatch();

            for (int reps1 = 0; reps1 < 5; ++reps1)
            {
                // Benchmark the .NET provided binary searcher.
                watch.Reset();
                watch.Start();
                for (int rx = 0; rx < reps; ++rx)
                {
                    for (var ix = 0; ix < Map1252.Length; ++ix)
                    {
                        var pos = Map1252.ArrayBinarySearch(Map1252.At(ix) & 0x7FFFFF00);
                        var fit = Map1252.At(~pos) & 0xFF;
                        Debug.Assert(ix == ~pos);
                    }
                }
                tot0 += watch.ElapsedMilliseconds;

                // Benchmark the custom binary searcher.
                watch.Reset();
                watch.Start();
                for (int rx = 0; rx < reps; ++rx)
                {
                    for (var ix = 0; ix < Map1252.Length; ++ix)
                    {
                        var result = Map1252.To1252Bestfit(Map1252.At(ix) >> 8);
                        Debug.Assert(result == (Map1252.At(ix) & 0xFF));
                    }
                }
                tot1 += watch.ElapsedMilliseconds;
            }

            Console.WriteLine("Custom binary search change over Array.BinarySearch = " + 100f * (tot0 - tot1) / tot0 + "%");

            /* Output:
             *
             * Custom binary search change over Array.BinarySearch = 32.51122%
             *
             */
        }
Exemple #3
0
        public void Test_Map1252Order()
        {
            Assert.AreNotEqual(0, Map1252.Length);

            int prev = 0;

            for (var ix = 0; ix < Map1252.Length; ++ix)
            {
                int  key   = Map1252.At(ix) >> 8;
                byte value = unchecked ((byte)Map1252.At(ix));

                // Table must contain ordered, distinct keys.
                Assert.IsTrue(key > prev);

                // Not against the spec, just not expected.
                Assert.IsTrue(value != 0);

                prev = key;
            }
        }