Skip to content

Commit fe79931

Browse files
committed
Add more string literal tests
1 parent e748032 commit fe79931

8 files changed

Lines changed: 185 additions & 0 deletions

tests/unit/string_literals.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ int main() {
1111
char mutable_string_arr[] = "papanasi";
1212
const char immutable_string_arr[] = "papanasi";
1313

14+
char *mutable_empty = "";
15+
const char *immutable_empty = "";
16+
char mutable_empty_arr[] = "";
17+
const char immutable_empty_arr[] = "";
18+
1419
foo_mut("world");
1520
foo_mut(mutable_string);
1621
foo_mut(mutable_string_arr);
@@ -20,5 +25,11 @@ int main() {
2025
foo_const(immutable_string);
2126
foo_const(mutable_string_arr);
2227
foo_const(immutable_string_arr);
28+
29+
foo_const("");
30+
foo_const(mutable_empty);
31+
foo_const(immutable_empty);
32+
foo_const(mutable_empty_arr);
33+
foo_const(immutable_empty_arr);
2334
return 0;
2435
}

tests/unit/string_literals_c.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ int main() {
1111
char mutable_string_arr[] = "papanasi";
1212
const char immutable_string_arr[] = "papanasi";
1313

14+
char *mutable_empty = "";
15+
const char *immutable_empty = "";
16+
char mutable_empty_arr[] = "";
17+
const char immutable_empty_arr[] = "";
18+
1419
foo_mut("world");
1520
foo_mut(mutable_string);
1621
foo_mut(mutable_string_arr);
@@ -20,5 +25,11 @@ int main() {
2025
foo_const(immutable_string);
2126
foo_const(mutable_string_arr);
2227
foo_const(immutable_string_arr);
28+
29+
foo_const("");
30+
foo_const(mutable_empty);
31+
foo_const(immutable_empty);
32+
foo_const(mutable_empty_arr);
33+
foo_const(immutable_empty_arr);
2334
return 0;
2435
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <cassert>
2+
3+
int main() {
4+
const char *joined = "alpha\n"
5+
"beta\n"
6+
"gamma\n";
7+
assert(joined[0] == 'a');
8+
assert(joined[5] == '\n');
9+
assert(joined[6] == 'b');
10+
11+
char arr[] = "foo" "bar";
12+
assert(arr[0] == 'f');
13+
assert(arr[3] == 'b');
14+
assert(arr[5] == 'r');
15+
assert(arr[6] == '\0');
16+
17+
const char *split_pieces = "abc" "def" "ghi";
18+
assert(split_pieces[0] == 'a');
19+
assert(split_pieces[3] == 'd');
20+
assert(split_pieces[6] == 'g');
21+
assert(split_pieces[8] == 'i');
22+
assert(split_pieces[9] == '\0');
23+
return 0;
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <assert.h>
2+
3+
int main(void) {
4+
char arr[] = "foo" "bar";
5+
assert(arr[0] == 'f');
6+
assert(arr[3] == 'b');
7+
assert(arr[5] == 'r');
8+
assert(arr[6] == '\0');
9+
10+
const char *split_pieces = "abc" "def" "ghi";
11+
assert(split_pieces[0] == 'a');
12+
assert(split_pieces[3] == 'd');
13+
assert(split_pieces[6] == 'g');
14+
assert(split_pieces[8] == 'i');
15+
assert(split_pieces[9] == '\0');
16+
return 0;
17+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <cassert>
2+
3+
const char *get_greeting() { return "hello"; }
4+
const char *get_empty() { return ""; }
5+
6+
const char *get_branch(int x) {
7+
if (x > 0) {
8+
return "positive";
9+
}
10+
return "non-positive";
11+
}
12+
13+
int main() {
14+
const char *a = get_greeting();
15+
assert(a[0] == 'h');
16+
assert(a[4] == 'o');
17+
assert(a[5] == '\0');
18+
19+
const char *b = get_empty();
20+
assert(b[0] == '\0');
21+
22+
const char *c = get_branch(1);
23+
assert(c[0] == 'p');
24+
assert(c[7] == 'e');
25+
assert(c[8] == '\0');
26+
27+
const char *d = get_branch(-1);
28+
assert(d[0] == 'n');
29+
assert(d[11] == 'e');
30+
assert(d[12] == '\0');
31+
return 0;
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <assert.h>
2+
3+
const char *get_greeting(void) { return "hello"; }
4+
const char *get_empty(void) { return ""; }
5+
6+
const char *get_branch(int x) {
7+
if (x > 0) {
8+
return "positive";
9+
}
10+
return "non-positive";
11+
}
12+
13+
int main(void) {
14+
const char *a = get_greeting();
15+
assert(a[0] == 'h');
16+
assert(a[4] == 'o');
17+
assert(a[5] == '\0');
18+
19+
const char *b = get_empty();
20+
assert(b[0] == '\0');
21+
22+
const char *c = get_branch(1);
23+
assert(c[0] == 'p');
24+
assert(c[7] == 'e');
25+
assert(c[8] == '\0');
26+
27+
const char *d = get_branch(-1);
28+
assert(d[0] == 'n');
29+
assert(d[11] == 'e');
30+
assert(d[12] == '\0');
31+
return 0;
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <cassert>
2+
3+
int main() {
4+
char empty_buf[256] = "";
5+
assert(empty_buf[0] == '\0');
6+
assert(empty_buf[255] == '\0');
7+
8+
char prefix_buf[32] = "%";
9+
assert(prefix_buf[0] == '%');
10+
assert(prefix_buf[1] == '\0');
11+
assert(prefix_buf[31] == '\0');
12+
13+
char short_buf[16] = "hi";
14+
assert(short_buf[0] == 'h');
15+
assert(short_buf[1] == 'i');
16+
assert(short_buf[2] == '\0');
17+
assert(short_buf[15] == '\0');
18+
19+
char exact_buf[6] = "hello";
20+
assert(exact_buf[0] == 'h');
21+
assert(exact_buf[4] == 'o');
22+
assert(exact_buf[5] == '\0');
23+
24+
assert(sizeof("hello") == 6);
25+
assert(sizeof("hello") - 1 == 5);
26+
assert(sizeof("") == 1);
27+
assert(sizeof("fifteen-bytes!!") - 1 == 15);
28+
return 0;
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <assert.h>
2+
3+
int main() {
4+
char empty_buf[256] = "";
5+
assert(empty_buf[0] == '\0');
6+
assert(empty_buf[255] == '\0');
7+
8+
char prefix_buf[32] = "%";
9+
assert(prefix_buf[0] == '%');
10+
assert(prefix_buf[1] == '\0');
11+
assert(prefix_buf[31] == '\0');
12+
13+
char short_buf[16] = "hi";
14+
assert(short_buf[0] == 'h');
15+
assert(short_buf[1] == 'i');
16+
assert(short_buf[2] == '\0');
17+
assert(short_buf[15] == '\0');
18+
19+
char exact_buf[6] = "hello";
20+
assert(exact_buf[0] == 'h');
21+
assert(exact_buf[4] == 'o');
22+
assert(exact_buf[5] == '\0');
23+
24+
assert(sizeof("hello") == 6);
25+
assert(sizeof("hello") - 1 == 5);
26+
assert(sizeof("") == 1);
27+
assert(sizeof("fifteen-bytes!!") - 1 == 15);
28+
return 0;
29+
}

0 commit comments

Comments
 (0)