コード例 #1
0
        public void TrimSegment2d_SegmentCollinear()
        {
            // Checks that when the segment is coincident with segments of the polygon,
            // segments that lie on polygon edges are not included, as per the intended
            // behavior.

            // Arrange
            var square = new GeneralPolygon2d(new Polygon2d(new double[] {
                0, 0,
                3, 0,
                3, 1,
                2, 1,
                2, 1.3,
                2, 1.6,
                2, 2,
                3, 2,
                3, 3,
                0, 3,
            }));
            var seg = new Segment2d(new Vector2d(2, 0.5), new Vector2d(2, 2.5));

            // Act
            var result = square.TrimSegment2d(seg);

            // Assert
            Assert.AreEqual(2, result.Count);
            AssertExtensions.AreEqual(new Vector2d(2, 0.5), result[0].P0);
            AssertExtensions.AreEqual(new Vector2d(2, 1.0), result[0].P1);
            AssertExtensions.AreEqual(new Vector2d(2, 2.0), result[1].P0);
            AssertExtensions.AreEqual(new Vector2d(2, 2.5), result[1].P1);
        }
コード例 #2
0
        public void DecodeNumbers()
        {
            var input    = new[] { "12", "123", "26", "126" };
            var expected = new[] { 2, 3, 2, 3 };
            var result   = new DecodeNumbers(input).Run();

            AssertExtensions.AreEqual(expected, result);
        }
コード例 #3
0
        public void StackImplementation()
        {
            var input    = new[] { "1 2 3 4", "10 -2 3 4" };
            var expected = new[] { "4 2", "4 -2" };
            var result   = new StackImplementation(input).Run();

            AssertExtensions.AreEqual(expected, result);
        }
コード例 #4
0
        public void StringRotation()
        {
            var input    = new[] { "Hello,lloHe", "Basefont,tBasefon" };
            var expected = new[] { "True", "True" };
            var result   = new StringRotation(input).Run();

            AssertExtensions.AreEqual(expected, result);
        }
コード例 #5
0
        public void StringList()
        {
            var input    = new[] { "1,aa", "2,ab", "3,pop" };
            var expected = new[] { "a", "aa,ab,ba,bb", "ooo,oop,opo,opp,poo,pop,ppo,ppp" };
            var result   = new StringList(input).Run();

            AssertExtensions.AreEqual(expected, result);
        }
コード例 #6
0
        public void sudoku_grid_checks_validity()
        {
            var input    = new[] { "4;1,4,2,3,2,3,1,4,4,2,3,1,3,1,4,2", "4;2,1,3,2,3,2,1,4,1,4,2,3,2,3,4,1" };
            var expected = new[] { "True", "False" };
            var results  = new Sudoku(input).Run();

            AssertExtensions.AreEqual(expected, results);
        }
コード例 #7
0
        public void RemoveCharactersTest()
        {
            var input    = new[] { "how are you, abc", "hello world, def" };
            var expected = new[] { "how re you", "hllo worl" };
            var result   = new RemoveCharacters(input).Run();

            AssertExtensions.AreEqual(expected, result);
        }
コード例 #8
0
        public void CashRegister()
        {
            var input    = new[] { "15.94;16.00", "17;16", "35;35", "45;50", ".01;.05" };
            var expected = new[] { "NICKEL,PENNY", "ERROR", "ZERO", "FIVE", "PENNY,PENNY,PENNY,PENNY" };
            var result   = new CashRegister(input).Run();

            AssertExtensions.AreEqual(expected, result);
        }
コード例 #9
0
        public void ReverseGroups()
        {
            var input    = new [] { "1,2,3,4,5;2", "1,2,3,4,5;3" };
            var expected = new[] { "2,1,4,3,5", "3,2,1,4,5" };
            var result   = new ReverseGroups(input).Run();

            AssertExtensions.AreEqual(expected, result);
        }
コード例 #10
0
        public void SumOfIntegersTest()
        {
            var input    = new[] { "-10,2,3,-2,0,5,-15", "2,3,-2,-1,10" };
            var expected = new[] { 8, 12 };
            var result   = new SumOfIntegers(input).Run();

            AssertExtensions.AreEqual(expected, result);
        }
コード例 #11
0
        public void JollyJumpers()
        {
            var input    = new[] { "4 1 4 2 3", "3 7 7 8", "9 8 9 7 10 6 12 17 24 38" };
            var expected = new[] { "Jolly", "Not jolly", "Not jolly" };
            var result   = new JollyJumpers(input).Run();

            AssertExtensions.AreEqual(expected, result);
        }
コード例 #12
0
        public void LowestCommonAncestorTest()
        {
            var input    = new[] { "8 52", "3 29" };
            var expected = new[] { "30", "8" };
            var result   = new CodeEvalChallenges.Challenges.LowestCommonAcestor(input).Run();

            AssertExtensions.AreEqual(expected, result);
        }
コード例 #13
0
        public void ArrayAbsurdity()
        {
            var input    = new[] { "5;0,1,2,3,0", "20; 0,1,10,3,2,4,5,7,6,8,11,9,15,12,13,4,16,18,17,14" };
            var expected = new[] { "0", "4" };
            var result   = new ArrayAbsurdity(input).Run();

            AssertExtensions.AreEqual(expected, result);
        }
コード例 #14
0
        public void NumberOfOnesTest()
        {
            var input    = new[] { "10", "22", "56" };
            var expected = new[] { "2", "3", "3" };
            var result   = new NumberOfOnes(input).Run();

            AssertExtensions.AreEqual(expected, result);
        }
コード例 #15
0
        public void StringSearchingTest()
        {
            var input    = new[] { "Hello,ell", "This is good, is", "CodeEval,C*Eval", "Old,Young" };
            var expected = new[] { "true", "true", "true", "false" };
            var result   = new StringSearching(input).Run();

            AssertExtensions.AreEqual(expected, result);
        }
コード例 #16
0
        public void MinimumCoins()
        {
            var input    = new[] { 11, 20 };
            var expected = new[] { 3, 4 };
            var result   = new MinimumCoins(input).Run();

            AssertExtensions.AreEqual(expected, result);
        }
コード例 #17
0
        public void MthToLast()
        {
            var input    = new[] { "a b c d 4", "e f g h 2" };
            var expected = new[] { "a", "g" };
            var prog     = new MthToLastElement(input);
            var result   = prog.Run();

            AssertExtensions.AreEqual(expected, result);
        }
コード例 #18
0
        public void sudoku_box_parses_second_box()
        {
            var inputText = "4;1,4,2,3,2,3,1,4,4,2,3,1,3,1,4,2";
            var inputDim  = int.Parse(inputText[0].ToString());
            var inputNums = inputText.Split(';')[1].Split(',').Select(int.Parse).ToArray();
            var grid      = SudokuGrid.ParseGrid(inputDim, inputNums);
            var box       = grid.Boxes.ElementAt(1);

            var expectedNums = new[] { 2, 3, 1, 4 };

            AssertExtensions.AreEqual(expectedNums, box.Numbers);
        }
コード例 #19
0
        public void TrimSegment2d_SegmentEndsOutside()
        {
            // Arrange
            var square = new GeneralPolygon2d(Polygon2d.MakeRectangle(new Vector2d(-1, 0), new Vector2d(1, 1)));
            var seg    = new Segment2d(new Vector2d(0, -1), new Vector2d(0, 2));

            // Act
            var result = square.TrimSegment2d(seg);

            // Assert
            Assert.AreEqual(1, result.Count);
            AssertExtensions.AreEqual(new Vector2d(0, 0), result[0].P0);
            AssertExtensions.AreEqual(new Vector2d(0, 1), result[0].P1);
        }
コード例 #20
0
        public void Deserialize()
        {
            AssertExtensions.Throws <ArgumentNullException> (() =>
            {
                OutputCache.Deserialize(null);
            }, "#A1");

            foreach (object o in serializeObjects)
            {
                using (var m = new MemoryStream(SerializeToBinary(o)))
                {
                    AssertExtensions.Throws <ArgumentException> (() =>
                    {
                        OutputCache.Deserialize(m);
                    }, String.Format("#A2-{0}", o.GetType()));
                }
            }

            MemoryResponseElement       mre, mreOrig;
            FileResponseElement         fre, freOrig;
            SubstitutionResponseElement sre, sreOrig, sreBad, sreBadOrig;
            MemoryStream ms;

            CreateElements(out mre, out fre, out sre, out sreBad);

            using (ms = new MemoryStream(memoryResponseElement))
                mreOrig = OutputCache.Deserialize(ms) as MemoryResponseElement;
            Assert.IsNotNull(mreOrig, "#B1-1");
            AssertExtensions.AreEqual(mreOrig.Buffer, mre.Buffer, "#B1-2");
            Assert.AreEqual(mreOrig.Length, mre.Length, "#B1-3");

            using (ms = new MemoryStream(fileResponseElement))
                freOrig = OutputCache.Deserialize(ms) as FileResponseElement;
            Assert.IsNotNull(freOrig, "#C1-1");
            Assert.AreEqual(freOrig.Length, fre.Length, "#C1-2");
            Assert.AreEqual(freOrig.Offset, fre.Offset, "#C1-3");
            Assert.AreEqual(freOrig.Path, fre.Path, "#C1-4");

            using (ms = new MemoryStream(substitutionResponseElement))
                sreOrig = OutputCache.Deserialize(ms) as SubstitutionResponseElement;
            Assert.IsNotNull(sreOrig);
            Assert.AreEqual(sreOrig.Callback, sre.Callback, "#D1");

            // Callback here is not a static method - Substitution delegates must be static
            AssertExtensions.Throws <ArgumentException> (() =>
            {
                using (ms = new MemoryStream(badSubstitutionResponseElement))
                    sreBadOrig = OutputCache.Deserialize(ms) as SubstitutionResponseElement;
            }, "#E1");
        }
コード例 #21
0
        public void sudoku_box_parses_box()
        {
            var inputText = "4;1,4,2,3,2,3,1,4,4,2,3,1,3,1,4,2";
            var inputDim  = int.Parse(inputText[0].ToString());
            var inputNums = inputText.Split(';')[1].Split(',').Select(int.Parse).ToArray();
            var grid      = SudokuGrid.ParseGrid(inputDim, inputNums);
            var firstBox  = grid.Boxes.First();
            var valid     = firstBox.IsValid;

            var expectedNums = new[] { 1, 4, 2, 3 };

            Assert.IsTrue(expectedNums.Select((n, i) => firstBox.Numbers[i] == n).All(b => b));
            AssertExtensions.AreEqual(expectedNums, firstBox.Numbers);
            Assert.IsTrue(valid);
        }
コード例 #22
0
        public void Serialize()
        {
            ResponseElement data = new MemoryResponseElement(new byte[10], 10);

            AssertExtensions.Throws <ArgumentNullException> (() =>
            {
                OutputCache.Serialize(null, data);
            }, "#A1");

            var ms = new MemoryStream();

            AssertExtensions.Throws <ArgumentException> (() =>
            {
                OutputCache.Serialize(ms, null);
            }, "#A2");

            foreach (object o in serializeObjects)
            {
                AssertExtensions.Throws <ArgumentException> (() =>
                {
                    OutputCache.Serialize(ms, o);
                }, String.Format("#A3-{0}", o.GetType()));
            }

            MemoryResponseElement       mre;
            FileResponseElement         fre;
            SubstitutionResponseElement sre, sreBad;

            CreateElements(out mre, out fre, out sre, out sreBad);
            // WriteSerializedBytes ("/tmp/bytes.txt", "memoryResponseElement", mre);
            // WriteSerializedBytes ("/tmp/bytes.txt", "fileResponseElement", fre);
            // WriteSerializedBytes ("/tmp/bytes.txt", "substitutionResponseElement", sre);
            // WriteSerializedBytes ("/tmp/bytes.txt", "badSubstitutionResponseElement", sreBad);

            byte[] bytes = SerializeElement(mre);
            AssertExtensions.AreEqual(bytes, memoryResponseElement, "#B1");

            bytes = SerializeElement(fre);
            AssertExtensions.AreEqual(bytes, fileResponseElement, "#B2");

            bytes = SerializeElement(sre);
            AssertExtensions.AreEqual(bytes, substitutionResponseElement, "#B3");

            bytes = SerializeElement(sreBad);
            AssertExtensions.AreEqual(bytes, badSubstitutionResponseElement, "#B3");
        }
コード例 #23
0
        public void TrimSegment2d_SegmentAcrossHole()
        {
            // Arrange
            var square = new GeneralPolygon2d(Polygon2d.MakeRectangle(new Vector2d(0, 0), 4, 4));
            var hole   = Polygon2d.MakeRectangle(new Vector2d(0, 0), 2, 2);

            hole.Reverse();
            square.AddHole(hole);
            var seg = new Segment2d(new Vector2d(0, -4), new Vector2d(0, 4));

            // Act
            var result = square.TrimSegment2d(seg);

            // Assert
            Assert.AreEqual(2, result.Count);
            AssertExtensions.AreEqual(new Vector2d(0, -2), result[0].P0);
            AssertExtensions.AreEqual(new Vector2d(0, -1), result[0].P1);
            AssertExtensions.AreEqual(new Vector2d(0, 1), result[1].P0);
            AssertExtensions.AreEqual(new Vector2d(0, 2), result[1].P1);
        }