aboutsummaryrefslogtreecommitdiff
path: root/basics/main.c
blob: 207fee2217f9e2499e3386e4af0a73fda4ca3644 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>

/*
 * Returns the factorial for the given unsigned 64-bit integer.
 *
 * Implemented in factorial.S
 */
int factorial(uint64_t n);

void test_factorial()
{
	assert(factorial(0) == 0);
	assert(factorial(1) == 1);
	assert(factorial(2) == 2);
	assert(factorial(3) == 6);
	assert(factorial(4) == 24);

	printf("factorial:\tOK\n");
}

/*
 * Returns a pointer with the same given string but reversed. Note that the
 * string cannot be in read-only space since the reversal is done in-place.
 *
 * Implemented in string.S
 */
char * reverse_string(char *str);

void test_reverse_string()
{
	assert(reverse_string(NULL) == NULL);
	assert(reverse_string("") == "");

	char s1[] = "This is a string.";
	assert(strcmp(reverse_string(s1), ".gnirts a si sihT") == 0);

	char s2[] = ".";
	assert(strcmp(reverse_string(s2), ".") == 0);

	printf("reverse_string:\tOK\n");
}

/*
 * Returns true if the given string is a palyndrome, false otherwise.
 *
 * Implemented in string.S
 */
bool is_palyndrome(char *str);

void test_is_palyndrome()
{
	assert(is_palyndrome(NULL) == 0);
	assert(is_palyndrome("") == 0);
	assert(is_palyndrome("aba") == 1);
	assert(is_palyndrome("aaa") == 1);
	assert(is_palyndrome("This is a a si sihT") == 1);

	printf("is_palyndrome:\tOK\n");
}

int main()
{
	test_factorial();
	test_reverse_string();
	test_is_palyndrome();
}