TinyHashTest.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Quadarax.Foundation.Core.Value.Generators;
  2. namespace qdr.fnd.core.test.Value.Generators
  3. {
  4. public class TinyHashTest
  5. {
  6. [Test(TestOf = typeof(TinyHash))]
  7. public void ImplementsInterface()
  8. {
  9. Assert.IsInstanceOf<IIdGenerator<string>>(new TinyHash());
  10. }
  11. [Test(TestOf = typeof(TinyHash))]
  12. public void ConstructorFail()
  13. {
  14. // ReSharper disable once ObjectCreationAsStatement
  15. Assert.Throws<ArgumentNullException>(() => new TinyHash(null!, 5));
  16. // ReSharper disable once ObjectCreationAsStatement
  17. Assert.Throws<ArgumentNullException>(() => new TinyHash(string.Empty, 5));
  18. // ReSharper disable once ObjectCreationAsStatement
  19. Assert.Throws<ArgumentOutOfRangeException>(() => new TinyHash("A", 0));
  20. // ReSharper disable once ObjectCreationAsStatement
  21. Assert.Throws<ArgumentOutOfRangeException>(() => new TinyHash("A", -1));
  22. // ReSharper disable once ObjectCreationAsStatement
  23. Assert.Throws<ArgumentOutOfRangeException>(() => new TinyHash( 0));
  24. // ReSharper disable once ObjectCreationAsStatement
  25. Assert.Throws<ArgumentOutOfRangeException>(() => new TinyHash(-1));
  26. }
  27. [Test(TestOf = typeof(TinyHash))]
  28. public void GenerateNumberDefault()
  29. {
  30. var generator = new TinyHash();
  31. var result1 = generator.NewId();
  32. var result2 = generator.NewId();
  33. Assert.IsNotNull(result1);
  34. Assert.IsNotNull(result2);
  35. Assert.That(result2, Is.Not.EqualTo(result1));
  36. Assert.That(result1.Length, Is.EqualTo(TinyHash.DefaultLength));
  37. Assert.That(result2.Length, Is.EqualTo(TinyHash.DefaultLength));
  38. Assert.That(ValueAmongChars(result1, TinyHash.DefaultCharset), "value has some chars out of defined charset.");
  39. Assert.That(ValueAmongChars(result2, TinyHash.DefaultCharset), "value has some chars out of defined charset.");
  40. }
  41. [Test(TestOf = typeof(TinyHash))]
  42. public void GenerateNumberSpecificLength()
  43. {
  44. var length = 3;
  45. var generator = new TinyHash(length);
  46. var result1 = generator.NewId();
  47. var result2 = generator.NewId();
  48. Assert.IsNotNull(result1);
  49. Assert.IsNotNull(result2);
  50. Assert.That(result2, Is.Not.EqualTo(result1));
  51. Assert.That(result1.Length, Is.EqualTo(length));
  52. Assert.That(result2.Length, Is.EqualTo(length));
  53. Assert.That(ValueAmongChars(result1, TinyHash.DefaultCharset), "value has some chars out of defined charset.");
  54. Assert.That(ValueAmongChars(result2, TinyHash.DefaultCharset), "value has some chars out of defined charset.");
  55. }
  56. [Test(TestOf = typeof(TinyHash))]
  57. public void GenerateNumberCustomCharset()
  58. {
  59. var charset = "xyz";
  60. var generator = new TinyHash(charset, TinyHash.DefaultLength);
  61. var result1 = generator.NewId();
  62. var result2 = generator.NewId();
  63. Assert.IsNotNull(result1);
  64. Assert.IsNotNull(result2);
  65. Assert.That(result2, Is.Not.EqualTo(result1));
  66. Assert.That(result1.Length, Is.EqualTo(TinyHash.DefaultLength));
  67. Assert.That(result2.Length, Is.EqualTo(TinyHash.DefaultLength));
  68. Assert.That(ValueAmongChars(result1,charset), "value has some chars out of defined charset.");
  69. Assert.That(ValueAmongChars(result2, charset), "value has some chars out of defined charset.");
  70. }
  71. #region *** Private methods ***
  72. private bool ValueAmongChars(string value, string charset)
  73. {
  74. foreach (var c in value)
  75. {
  76. if (!charset.Contains(c)) return false;
  77. }
  78. return true;
  79. }
  80. #endregion
  81. }
  82. }