PageRenderTime 107ms CodeModel.GetById 82ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/MongoDB.Bson.Tests/PowerOf2Tests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 96 lines | 71 code | 11 blank | 14 comment | 0 complexity | 665becf0bf86b6b37b40801dcfd6b2d1 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2010-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using FluentAssertions;
  21. using MongoDB.Bson.TestHelpers.XunitExtensions;
  22. using Xunit;
  23. namespace MongoDB.Bson.Tests
  24. {
  25. public class PowerOf2Tests
  26. {
  27. [Theory]
  28. [ParameterAttributeData]
  29. public void IsPowerOf2_should_return_false_when_not_a_power_of_2(
  30. [Values(3, 5, 6, 7, 9, 127, 129, 0x37ffffff)]
  31. int n)
  32. {
  33. var result = PowerOf2.IsPowerOf2(n);
  34. result.Should().BeFalse();
  35. }
  36. [Theory]
  37. [ParameterAttributeData]
  38. public void IsPowerOf2_should_return_true_when_a_power_of_2(
  39. [Values(0, 1, 2, 4, 8, 128, 0x40000000)]
  40. int n)
  41. {
  42. var result = PowerOf2.IsPowerOf2(n);
  43. result.Should().BeTrue();
  44. }
  45. [Theory]
  46. [ParameterAttributeData]
  47. public void IsPowerOf2_should_throw_when_n_is_invalid(
  48. [Values(-1, 0x40000001)]
  49. int n)
  50. {
  51. Action action = () => PowerOf2.IsPowerOf2(n);
  52. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("n");
  53. }
  54. [Theory]
  55. [InlineData(0, 0)]
  56. [InlineData(1, 1)]
  57. [InlineData(2, 2)]
  58. [InlineData(3, 4)]
  59. [InlineData(4, 4)]
  60. [InlineData(5, 8)]
  61. [InlineData(6, 8)]
  62. [InlineData(7, 8)]
  63. [InlineData(8, 8)]
  64. [InlineData(9, 16)]
  65. [InlineData(127, 128)]
  66. [InlineData(128, 128)]
  67. [InlineData(129, 256)]
  68. [InlineData(0x37ffffff, 0x40000000)]
  69. [InlineData(0x40000000, 0x40000000)]
  70. public void RoundUpToPowerOf2_should_return_expected_result(int n, int expectedResult)
  71. {
  72. var result = PowerOf2.RoundUpToPowerOf2(n);
  73. result.Should().Be(expectedResult);
  74. }
  75. [Theory]
  76. [ParameterAttributeData]
  77. public void RoundUpToPowerOf2_should_throw_when_n_is_invalid(
  78. [Values(-1, 0x40000001)]
  79. int n)
  80. {
  81. Action action = () => PowerOf2.RoundUpToPowerOf2(n);
  82. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("n");
  83. }
  84. }
  85. }