-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added two prime function to calculate the size of the cache when it n…
…eeds to be resized, since it blows up with a simple 'double its size' algorithm
- Loading branch information
1 parent
b9b470b
commit 6862091
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <math.h> | ||
#include "prime.h" | ||
|
||
int is_prime(int n) { | ||
if (n <= 1) { | ||
return 0; | ||
} | ||
|
||
if (n == 2) { | ||
return 1; | ||
} | ||
|
||
if (n % 2 == 0) { | ||
return 0; | ||
} | ||
|
||
for (int i = 2; i < n; i++) { | ||
if ((n % i) == 0) { | ||
return 0; | ||
} | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
int next_prime(int x) { | ||
while (is_prime(x) != 1) { | ||
x++; | ||
} | ||
|
||
return x; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef DUCKY_PRIME_H | ||
#define DUCKY_PRIME_H | ||
|
||
int is_prime(int n); | ||
|
||
int next_prime(int n); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include "../lib/greatest.h" | ||
#include "../src/prime.h" | ||
|
||
TEST should_return_true_if_n_is_a_prime_number(void) { | ||
ASSERT_EQ(1, is_prime(2)); | ||
ASSERT_EQ(1, is_prime(3)); | ||
ASSERT_EQ(1, is_prime(5)); | ||
ASSERT_EQ(1, is_prime(7)); | ||
ASSERT_EQ(1, is_prime(11)); | ||
ASSERT_EQ(1, is_prime(19)); | ||
|
||
PASS(); | ||
} | ||
|
||
TEST should_return_false_if_n_is_a_not_a_prime_number(void) { | ||
ASSERT_EQ(0, is_prime(-11)); | ||
ASSERT_EQ(0, is_prime(-1)); | ||
ASSERT_EQ(0, is_prime(1)); | ||
ASSERT_EQ(0, is_prime(4)); | ||
ASSERT_EQ(0, is_prime(6)); | ||
ASSERT_EQ(0, is_prime(6)); | ||
ASSERT_EQ(0, is_prime(15)); | ||
PASS(); | ||
} | ||
|
||
TEST should_return_the_next_prime_number_unless_n_is_a_prime_number(void) { | ||
ASSERT_EQ(2, next_prime(1)); | ||
ASSERT_EQ(2, next_prime(2)); | ||
ASSERT_EQ(3, next_prime(3)); | ||
ASSERT_EQ(5, next_prime(4)); | ||
ASSERT_EQ(7, next_prime(6)); | ||
ASSERT_EQ(11, next_prime(10)); | ||
PASS(); | ||
} | ||
|
||
SUITE(suite) { | ||
RUN_TEST(should_return_true_if_n_is_a_prime_number); | ||
RUN_TEST(should_return_false_if_n_is_a_not_a_prime_number); | ||
RUN_TEST(should_return_the_next_prime_number_unless_n_is_a_prime_number); | ||
} | ||
|
||
GREATEST_MAIN_DEFS(); | ||
|
||
int main(int argc, char *argv[]) { | ||
GREATEST_MAIN_BEGIN(); | ||
RUN_SUITE(suite); | ||
GREATEST_MAIN_END(); | ||
} |