Beispiel #1
0
        public static void CreateNext_ArrayWithWrongSize_ShouldThrowException(PyramidSection sut, Generator <int> generator, IFixture fixture)
        {
            var size   = generator.First(number => number != sut.Step + 1);
            var values = fixture.CreateMany <int>(size).ToArray();

            Assert.Throws <ArgumentException>(() => sut.CreateNext(values));
        }
        /// <summary>
        /// Generates linked pyramid sections and returns the bottom one.
        /// </summary>
        /// <returns></returns>
        public IPyramidSection GeneratePyramidSections(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (!_fileHelper.FileExists(path))
            {
                throw new FileNotFoundException($"File does not exists at path {path}");
            }

            var file       = _fileHelper.ReadAllText(path).TrimEnd().TrimStart();
            var lines      = SplitIntoLines(file);
            var stack      = lines.Select(l => SplitIntoNumbers(l));
            var enumerator = stack.GetEnumerator();

            enumerator.MoveNext();
            var firstSection = enumerator.Current;

            if (firstSection.Count() != 1)
            {
                throw new ArgumentException();
            }
            IPyramidSection currentSection = new PyramidSection(firstSection.First());

            while (enumerator.MoveNext())
            {
                var section = enumerator.Current;
                currentSection = currentSection.CreateNext(section.ToArray());
            }
            return(currentSection);
        }
Beispiel #3
0
        public static void Constructor_WithValue_ShouldCreatesArrayWithOneValue(int value)
        {
            var pyramidSection = new PyramidSection(value);

            pyramidSection.Step.Should().Be(1);
            pyramidSection.Values.Count.Should().Be(1);
            pyramidSection.Values.ElementAt(0).Should().Be(value);
        }
Beispiel #4
0
        public static void CreateNext_ArrayWithCorrectSize_ShouldCreateNextSection(PyramidSection sut, IFixture fixture)
        {
            var size   = sut.Step + 1;
            var values = fixture.CreateMany <int>(size).ToArray();

            var nextSection = sut.CreateNext(values);

            nextSection.Step.Should().Be(size);
            nextSection.Values.Count().Should().Be(size);
            nextSection.Previous.Should().Be(sut);
        }
        public static void Totalize_ShouldTotalizeSections(PyramidTotalizer sut)
        {
            var topValue = 3;
            var array1   = new[] { 7, 4 };
            var array2   = new[] { 2, 4, 6 };
            var array3   = new[] { 8, 5, 9, 3 };

            var bottomSection = new PyramidSection(topValue)
                                .CreateNext(array1)
                                .CreateNext(array2)
                                .CreateNext(array3);
            var value = sut.Totalize(bottomSection);

            value.Should().Be(23);
        }