PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/test/NRopes.Tests/ConcateRopeTests.cs

https://github.com/mingdayfly/NRopes
C# | 81 lines | 66 code | 15 blank | 0 comment | 6 complexity | 5c4512727c7dd2e5b6e7198611e7ca49 MD5 | raw file
  1. namespace NRopes.Tests {
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using FluentAssertions;
  6. using Xunit;
  7. public class ConcateRopeTests : RopeTestsBase {
  8. private static int Fib(int n) {
  9. if (n <= 2) return 1;
  10. return Fib(n - 1) + Fib(n - 2);
  11. }
  12. private static ConcateRope CreateRope(int depth, int length) {
  13. Debug.Assert(depth >= 1);
  14. var numOfParts = depth + 1;
  15. var lengthOfParts = length / numOfParts;
  16. var primitiveRope = new FlatRope(new String(' ', lengthOfParts));
  17. Rope rope = primitiveRope;
  18. for (var i = 1; i < numOfParts; i++) {
  19. if (i < numOfParts - 1) {
  20. rope = new ConcateRope(rope, primitiveRope);
  21. }
  22. else {
  23. var lastRope = new FlatRope(new String(' ', length - rope.Length));
  24. rope = new ConcateRope(rope, lastRope);
  25. }
  26. }
  27. var concateRope = rope.As<ConcateRope>();
  28. Debug.Assert(concateRope.Depth == depth);
  29. Debug.Assert(concateRope.Length == length);
  30. return concateRope;
  31. }
  32. protected override IEnumerable<Rope> TargetRopes {
  33. get { yield return new ConcateRope(new FlatRope("A"), new FlatRope("B")); }
  34. }
  35. [Fact]
  36. public void ConstructorShouldThrowIfEitherChildIsNull() {
  37. new Action(() => new ConcateRope(null, new FlatRope("A")))
  38. .ShouldThrow<ArgumentNullException>().And
  39. .ParamName.Should().Be("left");
  40. new Action(() => new ConcateRope(new FlatRope("B"), null))
  41. .ShouldThrow<ArgumentNullException>().And
  42. .ParamName.Should().Be("right");
  43. }
  44. [Fact]
  45. public void SimplePropertiesShouldBeRight() {
  46. var hwRope = new ConcateRope(new FlatRope("Hello "), new FlatRope("World"));
  47. hwRope.Depth.Should().Be(1);
  48. var rope = new ConcateRope(new FlatRope("Say "), hwRope);
  49. rope.Depth.Should().Be(2);
  50. rope.Length.Should().Be("Say Hello World".Length);
  51. rope.ToString().Should().Be("Say Hello World");
  52. }
  53. [Fact]
  54. public void IsBalancedShouldBeFalseForLengthLowerThanFibDepthPlusTwo() {
  55. CreateRope(1, Fib(3) - 1).IsBalanced.Should().BeFalse();
  56. CreateRope(2, Fib(4) - 1).IsBalanced.Should().BeFalse();
  57. CreateRope(5, Fib(7) - 1).IsBalanced.Should().BeFalse();
  58. }
  59. [Fact]
  60. public void IsBalancedShouldBeTrueForLengthGreaterThanOrEqualToFibDepthPlusTwo() {
  61. CreateRope(1, Fib(3)).IsBalanced.Should().BeTrue();
  62. CreateRope(2, Fib(4)).IsBalanced.Should().BeTrue();
  63. CreateRope(5, Fib(7)).IsBalanced.Should().BeTrue();
  64. }
  65. }
  66. }