|
| 1 | +using System.Collections.Generic; |
| 2 | +using System; |
| 3 | +using BitFaster.Caching; |
| 4 | +using FluentAssertions; |
| 5 | +using Xunit; |
| 6 | +using Xunit.Abstractions; |
| 7 | + |
| 8 | +namespace BitFaster.Caching.UnitTests |
| 9 | +{ |
| 10 | + public class HashTablePrimesTests |
| 11 | + { |
| 12 | + private readonly ITestOutputHelper testOutputHelper; |
| 13 | + |
| 14 | + public HashTablePrimesTests(ITestOutputHelper testOutputHelper) |
| 15 | + { |
| 16 | + this.testOutputHelper = testOutputHelper; |
| 17 | + } |
| 18 | + |
| 19 | + [Theory] |
| 20 | + [InlineData(3, 7)] |
| 21 | + [InlineData(8, 11)] |
| 22 | + [InlineData(12, 17)] |
| 23 | + [InlineData(132, 137)] |
| 24 | + [InlineData(500, 137)] |
| 25 | + public void NextPrimeGreaterThan(int input, int nextPrime) |
| 26 | + { |
| 27 | + HashTablePrimes.NextPrimeGreaterThan(input).Should().Be(nextPrime); |
| 28 | + } |
| 29 | + |
| 30 | + // This test method replicates the hash table sizes that will be computed by ConcurrentDictionary |
| 31 | + // on earlier versions of .NET before prime numbers are used. |
| 32 | + // 277 is prime |
| 33 | + // 557 is prime |
| 34 | + // 1117 is prime |
| 35 | + // 2237 is prime |
| 36 | + // 4477 has factors 11, 37, 121, 407 |
| 37 | + // 8957 has factors 13, 53, 169, 689 |
| 38 | + // 17917 has factors 19, 23, 41, 437, 779, 943 |
| 39 | + // 35837 is prime |
| 40 | + // 71677 has factors 229, 313 |
| 41 | + // 143357 is prime |
| 42 | + // 286717 has factors 163, 1759 |
| 43 | + // 573437 is prime |
| 44 | + // 1146877 is prime |
| 45 | + // 2293757 is prime |
| 46 | + // 4587517 has factors 11, 103, 1133, 4049, 44539, 417047 |
| 47 | + // 9175037 is prime |
| 48 | + // 18350077 has factors 701, 26177 |
| 49 | + // 36700157 has factors 13, 23, 299, 122743, 1595659, 2823089 |
| 50 | + // 73400317 has factors 4999, 14683 |
| 51 | + // 146800637 is prime |
| 52 | + // 293601277 has factors 6113, 48029 |
| 53 | + // 587202557 has factors 1877, 312841 |
| 54 | + // 1174405117 has factors 10687, 109891 |
| 55 | + [Fact(Skip="Not a functional test")] |
| 56 | + public void ComputeHashTableSizes() |
| 57 | + { |
| 58 | + // 137 gives a good balance of primes for smaller sizes, and few factors for larger sizes. |
| 59 | + // Other good candidates: 131, 151, 163, 211 |
| 60 | + int size = 137; |
| 61 | + for (int i = 0; i < 23; i++) |
| 62 | + { |
| 63 | + int nextSize = NextTableSize(size); |
| 64 | + this.testOutputHelper.WriteLine($"{nextSize} {GetFactorsString(nextSize)}"); |
| 65 | + size = nextSize; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Replicates .NET framework ConcurrentDictionary resize logic: |
| 70 | + // https://github.com/microsoft/referencesource/blob/51cf7850defa8a17d815b4700b67116e3fa283c2/mscorlib/system/collections/Concurrent/ConcurrentDictionary.cs#L1828C29-L1828C29 |
| 71 | + private static int NextTableSize(int initial) |
| 72 | + { |
| 73 | + // Double the size of the buckets table and add one, so that we have an odd integer. |
| 74 | + int newLength = initial * 2 + 1; |
| 75 | + |
| 76 | + // Now, we only need to check odd integers, and find the first that is not divisible |
| 77 | + // by 3, 5 or 7. |
| 78 | + while (newLength % 3 == 0 || newLength % 5 == 0 || newLength % 7 == 0) |
| 79 | + { |
| 80 | + newLength += 2; |
| 81 | + } |
| 82 | + |
| 83 | + return newLength; |
| 84 | + } |
| 85 | + |
| 86 | + private static string GetFactorsString(int nextSize) |
| 87 | + { |
| 88 | + var factors = Factor(nextSize); |
| 89 | + |
| 90 | + factors.Remove(1); |
| 91 | + factors.Remove(nextSize); |
| 92 | + factors.Sort(); |
| 93 | + |
| 94 | + if (factors.Count == 0) |
| 95 | + { |
| 96 | + return "is prime"; |
| 97 | + } |
| 98 | + |
| 99 | + return $"has factors {string.Join(", ", factors)}"; |
| 100 | + } |
| 101 | + |
| 102 | + // https://stackoverflow.com/questions/239865/best-way-to-find-all-factors-of-a-given-number |
| 103 | + private static List<int> Factor(int number) |
| 104 | + { |
| 105 | + var factors = new List<int>(); |
| 106 | + int max = (int)Math.Sqrt(number); // Round down |
| 107 | + |
| 108 | + for (int factor = 1; factor <= max; ++factor) // Test from 1 to the square root, or the int below it, inclusive. |
| 109 | + { |
| 110 | + if (number % factor == 0) |
| 111 | + { |
| 112 | + factors.Add(factor); |
| 113 | + if (factor != number / factor) // Don't add the square root twice! Thanks Jon |
| 114 | + factors.Add(number / factor); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return factors; |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments