Skip to content

Commit

Permalink
load vector from file
Browse files Browse the repository at this point in the history
  • Loading branch information
QwEekYhyo committed Sep 30, 2024
1 parent b55ce5e commit 4c303cf
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ build/
.vscode/

*.ncn
!tests/test_vector.ncn
2 changes: 2 additions & 0 deletions include/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define NCN_VECTOR_H

#include <stddef.h>
#include <stdio.h>

typedef struct {
size_t size;
Expand All @@ -33,5 +34,6 @@ void free_vector(Vector* vector);
void print_vector(Vector* vector);

int save_vector(Vector* vector, const char* filename);
Vector* new_vector_from_file(FILE* file);

#endif // NCN_VECTOR_H
26 changes: 26 additions & 0 deletions src/vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#define _CRT_SECURE_NO_WARNINGS

#include <vector.h>
#include <utils.h>

#include <stdio.h>
#include <stdlib.h>

// Maybe check that size is not zero ?
Vector* new_uninitialized_vector(size_t size) {
Vector* new_vector = malloc(sizeof(Vector));
new_vector->size = size;
Expand Down Expand Up @@ -69,6 +72,7 @@ int save_vector(Vector* vector, const char* filename) {
return -1;
}

fprintf(file, "%c ", 'V');
fprintf(file, "%zu\n", vector->size);
for (size_t i = 0; i < vector->size; i++) {
fprintf(file, "%.15lf ", vector->buffer[i]);
Expand All @@ -79,3 +83,25 @@ int save_vector(Vector* vector, const char* filename) {
fclose(file);
return 0;
}

Vector* new_vector_from_file(FILE* file) {
char type;
fscanf(file, "%c", &type);
if (type != 'V') {
printf("Type \"%c\" is not Vector type\n", type);
return NULL;
}

size_t size;
fscanf(file, "%zu", &size);

Vector* new_vector = new_uninitialized_vector(size);
for (size_t i = 0; i < size; i++) {
fscanf(file, "%lf", &new_vector->buffer[i]);
}

char delimiter[5];
fscanf(file, "%5c", delimiter);

return new_vector;
}
14 changes: 14 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ enable_testing()

file(GLOB_RECURSE SOURCES *.c)

set(SOURCE_FILE ${CMAKE_SOURCE_DIR}/tests/test_vector.ncn)
set(DEST_FILE ${CMAKE_BINARY_DIR}/tests/test_vector.ncn)

add_custom_command(
OUTPUT ${DEST_FILE}
COMMAND ${CMAKE_COMMAND} -E copy ${SOURCE_FILE} ${DEST_FILE}
DEPENDS ${SOURCE_FILE}
COMMENT "Copying data files needed by the tests to build directory"
)

add_custom_target(copy_file ALL DEPENDS ${DEST_FILE})

foreach(SOURCE ${SOURCES})
get_filename_component(FILE_NAME ${SOURCE} NAME_WE)

Expand All @@ -10,3 +22,5 @@ foreach(SOURCE ${SOURCES})

add_test(NAME "Run${FILE_NAME}" COMMAND ${FILE_NAME})
endforeach()

add_dependencies(test_load_vector copy_file)
76 changes: 76 additions & 0 deletions tests/test_load_vector.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* New Chad Neural - C library to train neural networks
* Copyright (C) 2024 Lucas Logan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include <vector.h>
#include <utils.h>

#include <stdio.h>

static const char* FILENAME = "test_vector.ncn";

int main(void) {
FILE* file = fopen(FILENAME, "r");
if (!file) {
printf("Error while opening file where vector is saved\n");
return 1;
}

Vector* v = new_vector_from_file(file);
if (!v) {
printf("Error while loading vector from file\n");
return 1;
}

if (v->size != 4) {
printf("Loaded vector size is wrong: %zu\n", v->size);
return 1;
}

for (size_t i = 0; i < 4; i++) {
if (!are_double_equals(v->buffer[i], 6.9)) {
printf("Loaded vector doesn't have the right value: %lf\n", v->buffer[i]);
return 1;
}
}

print_vector(v);
free_vector(v);

v = new_vector_from_file(file);
if (!v) {
printf("Error while loading vector from file\n");
return 1;
}

if (v->size != 3) {
printf("Loaded vector size is wrong: %zu\n", v->size);
return 1;
}

for (size_t i = 0; i < 3; i++) {
if (!are_double_equals(v->buffer[i], 1.0)) {
printf("Loaded vector doesn't have the right value: %lf\n", v->buffer[i]);
return 1;
}
}
print_vector(v);

fclose(file);
free_vector(v);

return 0;
}
10 changes: 10 additions & 0 deletions tests/test_save_vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ int main(void) {
printf("Error while opening file where vector is saved\n");
return 1;
}

char type;
fscanf(file, "%c", &type);
if (type != 'V') {
printf("Saved type is not Vector type, got: %c\n",
type
);
return 1;
}

size_t size;
fscanf(file, "%zu", &size);
if (size != v->size) {
Expand Down
6 changes: 6 additions & 0 deletions tests/test_vector.ncn
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
V 4
6.9 6.9 6.9 6.9
---
V 3
1.0 1.0 1.0
---

0 comments on commit 4c303cf

Please sign in to comment.