Skip to content

Commit 83e115e

Browse files
committed
refactor: [arrays,design] Use tests.h
1 parent e0ae0a1 commit 83e115e

File tree

2 files changed

+73
-39
lines changed

2 files changed

+73
-39
lines changed

cpp/arrays/pointers.cpp

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
11
/**
22
* Pointer operators precedence
3+
* ============================
34
*/
45

5-
#include <stdio.h>
6-
#include <stdlib.h>
6+
#include "tests.h"
77

8-
void print_a(int *a, int n)
8+
/* ===========================================================================
9+
* Algorithms implementation
10+
* ===========================================================================
11+
*/
12+
#define _fmt(i, v) format("\n index: {}, value: {}", i, v)
13+
14+
vi_t ptrOpPrec(const int n, string &am)
915
{
10-
printf("a: ");
11-
for (int i = 0; i < n; i++) { printf("%d ", a[i]); }
16+
int *p = (int *)malloc(n * sizeof(int)), *a = p;
17+
fii (i, n) *(p + i) = i;
18+
19+
ostringstream out;
20+
out << _fmt(0, *p++); // *a -> index: 0, value: 0, ptr++
21+
out << _fmt(1, (*p)++); // *a -> index: 1, value: 1, val++
22+
out << _fmt(1, *p); // *a -> index: 1, value: 2
23+
out << _fmt(2, *++p); // *a -> index: 2, value: 2, ++ptr
24+
out << _fmt(2, ++*p); // *a -> index: 2, value: 3, ++val
25+
// indices: 3, 4 with values: 3, 4 are unmodified
26+
vi_t ret(a, a + n);
27+
out << format("\n after: {}", to_string(ret));
28+
free(a);
29+
am = out.str();
30+
return ret;
1231
}
1332

14-
int main(int argc, char *argv[])
33+
/* ===========================================================================
34+
* Test code
35+
* ===========================================================================
36+
*/
37+
TEST(ptrOpPrec, "Pointer operators precedence")
1538
{
16-
int *ptr = (int *)malloc(5 * sizeof(int));
17-
int *p = ptr;
18-
for (int i = 0; i < 5; i++) *(ptr + i) = i;
19-
print_a(p, 5);
20-
printf("\tp: %d\n", *ptr++); // ptr -> index: 0, value: 0, ptr++
21-
print_a(p, 5);
22-
printf("\tp: %d\n", (*ptr)++); // ptr -> index: 1, value: 1, val++
23-
print_a(p, 5);
24-
printf("\tp: %d\n", *ptr); // ptr -> index: 1, value: 2
25-
print_a(p, 5);
26-
printf("\tp: %d\n", *++ptr); // ptr -> index: 2, value: 2, ++ptr
27-
print_a(p, 5);
28-
printf("\tp: %d\n", ++*ptr); // ptr -> index: 2, value: 3, ++val
29-
print_a(p, 5); // indices: 3, 4 with values: 3, 4 are unmodified
30-
printf("\n");
31-
free(p);
32-
return 0;
39+
const int n = 5;
40+
string am;
41+
vi_t e = {0, 2, 3, 3, 4};
42+
vi_t a = ptrOpPrec(n, am);
43+
CHECK_EQ(e, a);
44+
SHOW_OUTPUT(n, am);
3345
}
46+
47+
INIT_TEST_MAIN();

cpp/design/memmove.cpp

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,47 @@
1-
#include <stdio.h>
2-
31
/**
4-
* Implement overlapping memcpy () or memmove () function
2+
* Memmove with overlapping range
3+
* ==============================
54
*/
6-
static void *_memmove(void *dst, const void *src, size_t n)
5+
6+
#include "tests.h"
7+
8+
/* ===========================================================================
9+
* Algorithms implementation
10+
* ===========================================================================
11+
*/
12+
void _memmove(void *dst, const void *src, size_t n)
713
{
8-
char *psrc = (char *)src;
9-
char *pdst = (char *)dst;
10-
if (!psrc || !pdst) return NULL;
11-
if ((psrc < pdst) && (pdst < psrc + n)) {
14+
char *s = (char *)src, *d = (char *)dst;
15+
if (!s || !d) return;
16+
if ((s < d) && (d < s + n)) {
1217
// Buffer overlaps so copy backwards
13-
for (pdst += n, psrc += n; n--;) { *--pdst = *--psrc; }
18+
for (d += n, s += n; n--;) *--d = *--s;
1419
} else {
1520
// No buffer overlap so copy forwards (normal)
16-
while (n--) { *pdst++ = *psrc++; }
21+
while (n--) *d++ = *s++;
1722
}
18-
return dst;
1923
}
2024

21-
int main(int argc, char *argv[])
25+
/* ===========================================================================
26+
* Test code
27+
* ===========================================================================
28+
*/
29+
TEST(_memmove, "Memmove with overlapping range")
2230
{
23-
char str[] = "abcde";
24-
char *d = _memmove(str + 3, str, 3);
25-
printf("d: %s\n", d);
26-
return 0;
31+
const int n = 3;
32+
char s[] = "abcde";
33+
string i(s), e(s, n);
34+
35+
char *d = s + n;
36+
_memmove((void *)d, (void *)s, n);
37+
38+
string a(d, n);
39+
CHECK_EQ(e, a);
40+
41+
int di = d - s - 1, si = s - &s[0];
42+
string im;
43+
im += format("arr = {}, dst = {}, src = {}, size = {}", i, di, si, n);
44+
SHOW_OUTPUT(im, a);
2745
}
46+
47+
INIT_TEST_MAIN();

0 commit comments

Comments
 (0)