From dd7ecc09bf0476300ed47c9289d8714d9b719d42 Mon Sep 17 00:00:00 2001 From: Sarvesh Patkar Date: Tue, 31 Oct 2017 18:36:14 -0700 Subject: [PATCH 1/2] Added is prime function with testing code --- isprime.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 isprime.cpp diff --git a/isprime.cpp b/isprime.cpp new file mode 100644 index 0000000..b05266f --- /dev/null +++ b/isprime.cpp @@ -0,0 +1,62 @@ +#include +#include +#include + +/* Update if you are testing more + */ +#define TESTS 7 +#define TEST_INPUTS {-10, \ + 0, \ + 1, \ + 2, \ + 25, \ + 101, \ + 2147483647} + +#define EXPECTED_OUTPUT {false, \ + false, \ + false, \ + true, \ + false, \ + true, \ + true} + +using namespace std; + +/* is_prime checks whether the number is prime and returns true for any prime + * and positive integer and returns false for any other condition + */ + +bool is_prime(long n) { + if(n <= 1) return false; + long test = sqrt(n), i = 0; + for(i = test; i > 1; i--){ + if((n % test) == 0) return false; + } + return true; +} + +/* prime_tester unit tests the is_prime for a set of inputs mentioned at the + * top + */ + +void prime_tester(void) { + bool expected[TESTS] = EXPECTED_OUTPUT; + long inputs[TESTS] = TEST_INPUTS; + int i = 0; + for(i = 0; i < TESTS; i++){ + if(expected[i] == is_prime(inputs[i])) { + cout<<"Test "< Date: Tue, 31 Oct 2017 18:47:49 -0700 Subject: [PATCH 2/2] Corrected variable names --- isprime.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/isprime.cpp b/isprime.cpp index b05266f..8fa8a6e 100644 --- a/isprime.cpp +++ b/isprime.cpp @@ -3,15 +3,16 @@ #include /* Update if you are testing more + * Assuming */ #define TESTS 7 -#define TEST_INPUTS {-10, \ +#define TEST_INPUTS {-128, \ 0, \ 1, \ 2, \ - 25, \ + 45, \ 101, \ - 2147483647} + 127} #define EXPECTED_OUTPUT {false, \ false, \ @@ -31,7 +32,7 @@ bool is_prime(long n) { if(n <= 1) return false; long test = sqrt(n), i = 0; for(i = test; i > 1; i--){ - if((n % test) == 0) return false; + if((n % i) == 0) return false; } return true; } @@ -54,9 +55,9 @@ void prime_tester(void) { } -/* Uncomment to test the code - * int main(void) { - * prime_tester(); - * return 0; - * } -*/ +/* Uncomment to test the code*/ +//int main(void) { +// prime_tester(); +// return 0; +//} +