Skip to content

Commit

Permalink
Strings: fixes al manejar errores en array (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
RaniAgus authored Aug 22, 2021
1 parent 87a500e commit 805b110
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 36 deletions.
15 changes: 5 additions & 10 deletions src/commons/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ static void _string_upper_element(char* ch);
void _string_append_with_format_list(const char* format, char** original, va_list arguments);
char** _string_split(char* text, char* separator, bool(*is_last_token)(int));
static void _string_array_push(char*** array, char* text, int size);
static char* _string_array_replace(char** array, int pos, char* text);

char *string_repeat(char character, int count) {
char *text = calloc(count + 1, 1);
Expand Down Expand Up @@ -246,11 +245,14 @@ void string_array_push(char*** array, char* text) {
}

char* string_array_replace(char** array, int pos, char* text) {
return string_array_size(array) > pos && pos >= 0 ? _string_array_replace(array, pos, text) : NULL;
char* old_text = array[pos];
array[pos] = text;

return old_text;
}

char* string_array_pop(char** array) {
return string_array_size(array) > 0 ? _string_array_replace(array, string_array_size(array) - 1, NULL) : NULL;
return string_array_replace(array, string_array_size(array) - 1, NULL);
}

/** PRIVATE FUNCTIONS **/
Expand Down Expand Up @@ -313,11 +315,4 @@ static void _string_array_push(char*** array, char* text, int size) {
*array = realloc(*array, sizeof(char*) * (size + 2));
(*array)[size] = text;
(*array)[size + 1] = NULL;
}

static char* _string_array_replace(char** array, int pos, char* text) {
char* old_text = array[pos];
array[pos] = text;

return old_text;
}
26 changes: 0 additions & 26 deletions tests/unit-tests/test_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -567,15 +567,6 @@ context (test_string) {
string_iterate_lines(names, _assert_names);
} end

it ("not to remove elements in an empty array") {
for(int i = 0; i < 10; i++) {
string_array_pop(names);
}

should_int(string_array_size(names)) be equal to (0);
should_bool(string_array_is_empty(names)) be truthy;
} end

it("replace an element") {
char* name = string_array_replace(names, 2, "Damian");

Expand All @@ -590,23 +581,6 @@ context (test_string) {
}
string_iterate_lines(names, _assert_names);
} end

it("not to replace an element outside the array") {
char* name = string_array_replace(names, 4, "Damian");

should_ptr(name) be null;
should_int(string_array_size(names)) be equal to (4);
should_ptr(names[4]) be null;

char* expected[] = {"Gaston", "Matias", "Sebastian", "Daniela"};
int i = 0;
void _assert_names(char* name) {
should_ptr(name) not be null;
should_string(name) be equal to (expected[i]);
i++;
}
string_iterate_lines(names, _assert_names);
} end
} end
} end
}

0 comments on commit 805b110

Please sign in to comment.