-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.c
49 lines (38 loc) · 1.34 KB
/
tests.c
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
/* tests.c - Copyright (c) 2019, Sijmen J. Mulder (see LICENSE.md) */
#include <string.h>
#include <assert.h>
#include "leftpad.h"
int
main()
{
char buf[10];
/* no padding needed */
assert(leftpad("test", "*", 4, buf, 10) == 4);
assert(strcmp("test", buf) == 0);
/* padding is applied */
assert(leftpad("test", "*", 9, buf, 10) == 9);
assert(strcmp("*****test", buf) == 0);
/* string is not truncated for lower target length */
assert(leftpad("test", "*", 2, buf, 10) == 4);
assert(strcmp("test", buf) == 0);
/* padding is repeated */
assert(leftpad("test", " .", 9, buf, 10) == 9);
assert(strcmp(" . . test", buf) == 0);
/* default padding is spaces */
assert(leftpad("test", NULL, 9, buf, 10) == 9);
assert(strcmp(" test", buf) == 0);
/* default string is an empty string */
assert(leftpad(NULL, "*", 9, buf, 10) == 9);
assert(strcmp("*********", buf) == 0);
/* doesn't write beyond buffer */
assert(leftpad("test", "*", 11, buf, 10) == 9);
assert(strcmp("*******te", buf) == 0);
/* calucates required size if no buf or buf_sz given */
assert(leftpad("test", " ", 2, NULL, 10) == 4);
assert(leftpad("test", " ", 4, NULL, 10) == 4);
assert(leftpad("test", " ", 6, NULL, 10) == 6);
assert(leftpad("test", " ", 2, buf, 0) == 4);
assert(leftpad("test", " ", 4, buf, 0) == 4);
assert(leftpad("test", " ", 6, buf, 0) == 6);
return 0;
}