public async Task <PrimeNumViewModel> Add(int index)
        {
            if (VerifyIfExistisOnDatabase(index))
            {
                var primeValueDb = await GetByIndex(index);

                return(new PrimeNumViewModel()
                {
                    Index = primeValueDb.Index, PrimeValue = primeValueDb.PrimeValue
                });
            }

            var valueIndex = FindPrimeByIndex(index);

            var primeInstance = new PrimeNum()
            {
                Index = index, PrimeValue = valueIndex
            };

            await _primeNumberRepository.Create(primeInstance);

            await _primeNumberRepository.UnitOfWork.Commit();

            var pnViewModel = new PrimeNumViewModel()
            {
                Index = primeInstance.Index, PrimeValue = primeInstance.PrimeValue
            };

            return(pnViewModel);
        }
Exemplo n.º 2
0
        public void Test(int userInput, int expected)
        {
            //PrimeNum newprime= new PrimeNum();
            int res = PrimeNum.GetPrime(userInput);

            Assert.Equal(expected, res);
        }
Exemplo n.º 3
0
        public static void Main()
        {
            Console.WriteLine("Enter a Number:");
            int      enteredInt = int.Parse(Console.ReadLine());
            PrimeNum newPrime   = new PrimeNum();
            Dictionary <int, string> allNumbers = new Dictionary <int, string>()
            {
            };

            allNumbers = newPrime.GenNumber(enteredInt);
            List <int> primeNumbers     = new List <int> {
            };
            List <int> compositeNumbers = new List <int> {
            };
            int counter    = 0;
            int currentKey = 0;
            int init       = 1;

            for (int i = 2; i <= enteredInt; i++)
            {
                counter    = 0;
                currentKey = i;
                init       = init + 1;
                for (int k = init; k <= allNumbers.Count; k++)
                //a) IF Loop: you are marking VERY FIRST ENTRY as "prime" (if its NOT already marked as "composite")
                //b) iterate thru rest of entries identify multiples and mark them as "composite"
                {
                    if ((counter == 0) && (allNumbers[k] == "unProcessed"))
                    {
                        primeNumbers.Add(k);
                        allNumbers[k] = "Prime";
                        counter       = 1;
                        // Console.WriteLine(k + " is a Prime");
                    }
                    else
                    {
                        if (((k % currentKey) == 0))
                        {
                            allNumbers[k] = "composite";
                            compositeNumbers.Add(k);
                            // Console.WriteLine(k + " is a composite");
                        }
                        else
                        {
                            // Console.WriteLine(k + " remains unprocessed");
                        }
                    }
                }
            }
            Console.WriteLine("Prime numbers are ...");
            Console.WriteLine(String.Join(", ", primeNumbers));
            Console.WriteLine("Composite numbers are ...");
            Console.WriteLine(String.Join(", ", compositeNumbers));
        }
Exemplo n.º 4
0
        public async void PrimeNumberServiceTests_AddNewRecordToDatabase_ExistsOnDatabase_MustGetFromDatabase()
        {
            var primeNum = new PrimeNum()
            {
                Index = 5, PrimeValue = 7
            };
            var mocker  = new AutoMocker();
            var service = mocker.CreateInstance <PrimeNumberService>();

            mocker.GetMock <IPrimeNumRepository>().Setup(r => r.IndexExistsOnDatabase(primeNum.Index)).Returns(true);
            mocker.GetMock <IPrimeNumRepository>().Setup(r => r.GetByIndex(primeNum.Index)).Returns(Task.FromResult(new PrimeNum()));

            await service.Add(primeNum.Index);

            mocker.GetMock <IPrimeNumRepository>().Verify(r => r.GetByIndex(primeNum.Index), Times.Once);
        }
Exemplo n.º 5
0
        public async void PrimeNumberServiceTests_AddNewRecordToDatabase_NotExistsOnDatabase_MustInsert()
        {
            var primeNum = new PrimeNum()
            {
                Index = 5, PrimeValue = 7
            };
            var mocker  = new AutoMocker();
            var service = mocker.CreateInstance <PrimeNumberService>();

            mocker.GetMock <IPrimeNumRepository>().Setup(r => r.IndexExistsOnDatabase(primeNum.Index)).Returns(false);
            mocker.GetMock <IPrimeNumRepository>().Setup(r => r.UnitOfWork.Commit()).Returns(Task.FromResult(1));
            mocker.GetMock <IPrimeNumRepository>().Setup(r => r.Create(primeNum)).Returns(Task.FromResult(primeNum));

            await service.Add(primeNum.Index);

            mocker.GetMock <IPrimeNumRepository>().Verify(r => r.Create(It.IsAny <PrimeNum>()), Times.Once);
            mocker.GetMock <IPrimeNumRepository>().Verify(r => r.UnitOfWork.Commit(), Times.Once);
        }
Exemplo n.º 6
0
        public void GenNumerals_GeneratedADictionaryWithKVPair_Dictionary()
        {
            //Arrange
            // GenNumber(userenterednumber)
            int      userEnteredNum = 100;
            PrimeNum primeObject    = new PrimeNum();

            //Act
            Dictionary <int, string> allNumbers = new Dictionary <int, string>()
            {
            };

            for (int i = 2; i <= userEnteredNum; i++)
            {
                allNumbers[i] = "unProcessed";
            }
            Dictionary <int, string> result = primeObject.GenNumber(userEnteredNum);

            //Assert
            CollectionAssert.AreEqual(allNumbers, result);
        }
Exemplo n.º 7
0
        public void PrimeNumConstructor_CreateInstanceOfPrimeNum_PrimeNum()
        {
            PrimeNum newItem = new PrimeNum();

            Assert.AreEqual(typeof(PrimeNum), newItem.GetType());
        }
        private void PrimeButton_Click(object sender, RoutedEventArgs e)
        {
            string n = nthNumber.Text;

            label.Content = PrimeNum.CalculateNthPrimeNumber(int.Parse(n));
        }