public void UseNonExistingCode() { var codeGenerator = new CodeGenerator(); var codes = new CodesCollection(); var codeLength = 8; var numberOfCodesToGenerate = 10; var wasSuccess = codeGenerator.TryAddNewUniqueCodes(codes, codeLength, numberOfCodesToGenerate, out _); Assert.IsTrue(wasSuccess); Assert.AreEqual(10, codes.Count); Code code; while (true) { code = codeGenerator.GenerateCode(codeLength); if (!codes.Contains(code)) { break; } } var codeState = codes.CheckCode(code); Assert.AreEqual(CodeState.DoesNotExist, codeState); var states = codes.GroupBy(c => c.State).ToDictionary(g => g.Key, g => g.Count()); Assert.AreEqual(1, states.Count); Assert.AreEqual(numberOfCodesToGenerate, states[CodeState.NotUsed]); Assert.AreEqual(numberOfCodesToGenerate, codes.Count); }
public void UseUsedCodeTest() { var codes = new CodesCollection(); var generator = new CodeGenerator(); var codeLength = 8; var numberOfCodesToGenerate = 10; var wasSuccess = generator.TryAddNewUniqueCodes(codes, codeLength, numberOfCodesToGenerate, out _); Assert.IsTrue(wasSuccess); Assert.AreEqual(numberOfCodesToGenerate, codes.Count); var codeState = codes.CheckCode(codes.First()); Assert.AreEqual(CodeState.NotUsed, codeState); codeState = codes.CheckCode(codes.First()); Assert.AreEqual(CodeState.Used, codeState); var states = codes.GroupBy(c => c.State).ToDictionary(g => g.Key, g => g.Count()); Assert.AreEqual(2, states.Count); Assert.AreEqual(numberOfCodesToGenerate - 1, states[CodeState.NotUsed]); Assert.AreEqual(1, states[CodeState.Used]); Assert.AreEqual(numberOfCodesToGenerate, codes.Count); }
public void GenerateAndAddCodesToHashSetTest(int codeLength, int numberOfCodesToGenerate) { var generator = new CodeGenerator(); var codes = new CodesCollection(); var wasSuccessful = generator.TryAddNewUniqueCodes(codes, codeLength, numberOfCodesToGenerate, out var newCodes); Assert.AreEqual(numberOfCodesToGenerate, newCodes.Count); Assert.AreEqual(numberOfCodesToGenerate, codes.Count); Assert.IsTrue(wasSuccessful); }
public void CodesGenerationAttemptsCeilingTest() { var generator = new CodeGenerator(); var codes = new CodesCollection(); var wasSuccessful = generator.TryAddNewUniqueCodes(codes, 0, 1, out _); Assert.AreEqual(1, codes.Count); Assert.IsTrue(wasSuccessful); wasSuccessful = generator.TryAddNewUniqueCodes(codes, 0, 1, out _); Assert.AreEqual(1, codes.Count); Assert.IsFalse(wasSuccessful); }
public void UseCodeWhenCodeCollectionIsEmpty() { var codeGenerator = new CodeGenerator(); var codes = new CodesCollection(); var codeLength = 8; var code = codeGenerator.GenerateCode(codeLength); var codeState = codes.CheckCode(code); Assert.AreEqual(CodeState.DoesNotExist, codeState); var states = codes.GroupBy(c => c.State).ToDictionary(g => g.Key, g => g.Count()); Assert.AreEqual(0, states.Count); Assert.AreEqual(0, codes.Count); }
public void CodePseudoUniquenessTest() { var generator = new CodeGenerator(); var codes = new CodesCollection(); var numberOfCodesToGenerateFirstTime = 10_000_000; var wasSuccessful = generator.TryAddNewUniqueCodes(codes, 8, numberOfCodesToGenerateFirstTime, out var newCodes); Assert.AreEqual(numberOfCodesToGenerateFirstTime, newCodes.Count); Assert.AreEqual(numberOfCodesToGenerateFirstTime, codes.Count); Assert.IsTrue(wasSuccessful); var numberOfCodesToGenerateSecondTime = 3_000_000; wasSuccessful = generator.TryAddNewUniqueCodes(codes, 8, numberOfCodesToGenerateSecondTime, out newCodes); Assert.AreEqual(numberOfCodesToGenerateSecondTime, newCodes.Count); Assert.AreEqual(numberOfCodesToGenerateFirstTime + numberOfCodesToGenerateSecondTime, codes.Count); Assert.IsTrue(wasSuccessful); Assert.AreEqual(numberOfCodesToGenerateFirstTime + numberOfCodesToGenerateSecondTime, codes.Select(c => c.Value).Distinct().Count()); }
public MainPageViewModel() { Codes = new CodesCollection(); Codes.ReadCodes(); RefreshButtonCommand = new Command(OnRefreshButtonClicked); }