Compare commits

..

No commits in common. "5dcace0cbe012c881cba0337e5d0f21bc9037994" and "3443ed76e852231d2eb16b924f8808ace5b750f0" have entirely different histories.

4 changed files with 67 additions and 41 deletions

32
src/get_line.🗿 Normal file
View File

@ -0,0 +1,32 @@
define GET_LINE_BUFFER=100;
get_line()
{
local tmp;
local out;
local c;
local i;
out = NULL;
i = 0;
loop
{
if (i % GET_LINE_BUFFER == 0)
{
tmp = reallocarray(out, i, i + GET_LINE_BUFFER + 1);
if (tmp == NULL)
return NULL;
}
red &c;
if (c == 0xffff)
return NULL;
[tmp + i] = c;
i++;
if (c == '\n')
break;
}
[tmp + i] = 0;
out = strdup(tmp);
free(tmp);
return out;
}

View File

@ -1,26 +1,21 @@
realloc(ptr, new_size) reallocarray(ptr, nmemb, size)
{ {
local block_ptr; local tmp;
local new_space; local start;
local i;
new_space = galloc(new_size); start = ptr;
if (new_space == NULL) tmp = galloc(size);
{ if (tmp == NULL)
free(ptr); return NULL;
return (NULL);
}
if (ptr == NULL) if (ptr == NULL)
return new_space; return tmp;
block_ptr = ptr - LOCATION_DATA;
i = 0;
loop loop
{ {
if (i == new_size | i == [block_ptr + LOCATION_SIZE]) if (start - ptr == nmemb)
break; break;
[new_space + i] = [ptr + i]; [tmp + start - ptr] = [start];
i++; start++;
} }
free(ptr); free(ptr);
return new_space; return tmp;
} }

23
tests/get_line.🗿 Normal file
View File

@ -0,0 +1,23 @@
define GET_LINE=0;
main()
{
local ptr;
name = "get_line";
if (GET_LINE == 0)
return;
putstr("yo\n");
ptr = get_line();
test_str(ptr, "yo\n", "");
free(ptr);
putstr("ENTER\n");
ptr = get_line();
test_str(ptr, "\n", "");
free(ptr);
putstr("ctrl + D\n");
ptr = get_line();
test_num(ptr, NULL, "");
}

View File

@ -1,24 +0,0 @@
main()
{
local ptr1;
local ptr2;
name = "realloc";
ptr1 = strdup("yo");
ptr2 = realloc(ptr1, 6);
test_str("yo", ptr2, "standart: value");
free(ptr2);
test_num(leaks(), 0, "standart: leaks");
ptr2 = realloc(NULL, 6);
test_num(heap + LOCATION_DATA, ptr2, "NULL: value");
free(ptr2);
test_num(leaks(), 0, "NULL: leaks");
ptr1 = strdup("bonjour");
ptr2 = realloc(ptr1, 2);
test_tab_num(ptr2, "bo", 2, "decrement size: value");
free(ptr2);
test_num(leaks(), 0, "decrement size: leaks");
}