コード例 #1
0
ファイル: IntervalTests.cs プロジェクト: yusharth/psi
        public void IntervalScaling()
        {
            var interval = new IntInterval(7, true, 42, false);

            Assert.IsTrue(interval.LeftEndpoint.Bounded);
            Assert.IsTrue(interval.LeftEndpoint.Inclusive);
            Assert.AreEqual(7, interval.Left);
            Assert.IsTrue(interval.RightEndpoint.Bounded);
            Assert.IsFalse(interval.RightEndpoint.Inclusive);
            Assert.AreEqual(42, interval.Right);
            Assert.AreEqual(42 - 7, interval.Span);

            var byFactor = interval.Scale(2.0f, 3.0f);

            Assert.IsTrue(byFactor.LeftEndpoint.Bounded);
            Assert.IsTrue(byFactor.LeftEndpoint.Inclusive);
            Assert.AreEqual(7 - (42 - 7), byFactor.Left);
            Assert.IsTrue(byFactor.RightEndpoint.Bounded);
            Assert.IsFalse(byFactor.RightEndpoint.Inclusive);
            Assert.AreEqual(42 + (42 - 7) * 2, byFactor.Right);
            Assert.AreEqual((42 - 7) * 4, byFactor.Span);

            var leftByFactor = interval.ScaleRight(2.0f);

            Assert.IsTrue(leftByFactor.LeftEndpoint.Bounded);
            Assert.IsTrue(leftByFactor.LeftEndpoint.Inclusive);
            Assert.AreEqual(7, leftByFactor.Left);
            Assert.IsTrue(leftByFactor.RightEndpoint.Bounded);
            Assert.IsFalse(leftByFactor.RightEndpoint.Inclusive);
            Assert.AreEqual(42 + (42 - 7), leftByFactor.Right);
            Assert.AreEqual((42 - 7) * 2, leftByFactor.Span);

            var centerByFactor = interval.ScaleCenter(2.0f);

            Assert.IsTrue(centerByFactor.LeftEndpoint.Bounded);
            Assert.IsTrue(centerByFactor.LeftEndpoint.Inclusive);
            Assert.AreEqual(7 - (42 - 7) / 2 - 1, centerByFactor.Left); // -1 for round down
            Assert.IsTrue(centerByFactor.RightEndpoint.Bounded);
            Assert.IsFalse(centerByFactor.RightEndpoint.Inclusive);
            Assert.AreEqual(42 + (42 - 7) / 2 + 1, centerByFactor.Right); // +1 for round up
            Assert.AreEqual((42 - 7) * 2 + 1, centerByFactor.Span);       // +1 for round up

            var rightByFactor = interval.ScaleLeft(2.0f);

            Assert.IsTrue(rightByFactor.LeftEndpoint.Bounded);
            Assert.IsTrue(rightByFactor.LeftEndpoint.Inclusive);
            Assert.AreEqual(7 - (42 - 7), rightByFactor.Left);
            Assert.IsTrue(rightByFactor.RightEndpoint.Bounded);
            Assert.IsFalse(rightByFactor.RightEndpoint.Inclusive);
            Assert.AreEqual(42, rightByFactor.Right);
            Assert.AreEqual((42 - 7) * 2, rightByFactor.Span);

            var leftBySpan = interval.ScaleRight(100);

            Assert.IsTrue(leftBySpan.LeftEndpoint.Bounded);
            Assert.IsTrue(leftBySpan.LeftEndpoint.Inclusive);
            Assert.AreEqual(7, leftBySpan.Left);
            Assert.IsTrue(leftBySpan.RightEndpoint.Bounded);
            Assert.IsFalse(leftBySpan.RightEndpoint.Inclusive);
            Assert.AreEqual(42 + 100, leftBySpan.Right);
            Assert.AreEqual(42 - 7 + 100, leftBySpan.Span);

            var centerBySpan = interval.ScaleCenter(100);

            Assert.IsTrue(centerBySpan.LeftEndpoint.Bounded);
            Assert.IsTrue(centerBySpan.LeftEndpoint.Inclusive);
            Assert.AreEqual(7 - 100 / 2, centerBySpan.Left);
            Assert.IsTrue(centerBySpan.RightEndpoint.Bounded);
            Assert.IsFalse(centerBySpan.RightEndpoint.Inclusive);
            Assert.AreEqual(42 + 100 / 2, centerBySpan.Right);
            Assert.AreEqual(42 - 7 + 100, centerBySpan.Span);

            var rightBySpan = interval.ScaleLeft(100);

            Assert.IsTrue(rightBySpan.LeftEndpoint.Bounded);
            Assert.IsTrue(rightBySpan.LeftEndpoint.Inclusive);
            Assert.AreEqual(7 - 100, rightBySpan.Left);
            Assert.IsTrue(rightBySpan.RightEndpoint.Bounded);
            Assert.IsFalse(rightBySpan.RightEndpoint.Inclusive);
            Assert.AreEqual(42, rightBySpan.Right);
            Assert.AreEqual(42 - 7 + 100, rightBySpan.Span);
        }