1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "test_files.h"

#include "gate/tests.h"
#include "gate/files.h"
#include "gate/console.h"
#include "gate/platforms.h"

static int dummy_user_param = 42;

static gate_bool_t GATE_CALL test_filesystem_root_callback(gate_file_entry_t const* entry, void* userparam)
{
    gate_stream_t* strm = (gate_stream_t*)gate_console();

    GATE_TEST_CHECK_EQUAL(userparam, &dummy_user_param);
    GATE_TEST_REQUIRE(entry != NULL);

    gate_stream_println_cstr(strm, entry->path);

    return true;
}


GATE_TEST_FUNCTION(test_filesystem)
{
    static gate_string_t const test_file_path = GATE_STRING_INIT_STATIC("gatecore_test_files.txt");
    gate_filestream_t* ptr_stream = NULL;
    gate_size_t written = 0;
    char buffer[16];
    gate_size_t returned = 0;
    gate_int64_t pos = 0;<--- Shadowed declaration

    GATE_TEST_UNIT_BEGIN(test_filesystem);

    GATE_TEST_CHECK_OK(gate_file_root_list(&test_filesystem_root_callback, &dummy_user_param));

    if (gate_file_exists(&test_file_path) == GATE_RESULT_OK)
    {
        gate_file_delete(&test_file_path);
    }

    GATE_TEST_REQUIRE_OK(gate_file_openstream(&test_file_path, GATE_STREAM_OPEN_WRITE, &ptr_stream));
    GATE_TEST_REQUIRE(ptr_stream != NULL);

    GATE_TEST_CHECK_OK(gate_stream_write(ptr_stream, "X", 1, &written));
    GATE_TEST_CHECK_OK(gate_stream_flush(ptr_stream));
    GATE_TEST_CHECK_OK(gate_stream_close(ptr_stream, GATE_STREAM_CLOSE_DEFAULT));
    gate_object_release(ptr_stream);
    ptr_stream = NULL;

    GATE_TEST_REQUIRE_OK(gate_file_exists(&test_file_path));

    GATE_TEST_REQUIRE_OK(gate_file_openstream(&test_file_path, GATE_STREAM_OPEN_READ, &ptr_stream));
    GATE_TEST_REQUIRE(ptr_stream != NULL);
    GATE_TEST_CHECK_OK(gate_stream_peek(ptr_stream, buffer, sizeof(buffer), &returned));
    GATE_TEST_CHECK_EQUAL(returned, 1);
    GATE_TEST_CHECK_EQUAL(buffer[0], 'X');
    GATE_TEST_CHECK_OK(gate_stream_read(ptr_stream, buffer, sizeof(buffer), &returned));
    GATE_TEST_CHECK_EQUAL(returned, 1);
    GATE_TEST_CHECK_EQUAL(buffer[0], 'X');
    GATE_TEST_CHECK_OK(gate_stream_seek(ptr_stream, 0, GATE_STREAM_SEEK_BEGIN, &pos));
    GATE_TEST_CHECK_EQUAL(pos, 0);
    GATE_TEST_CHECK_OK(gate_stream_read(ptr_stream, buffer, sizeof(buffer), &returned));
    GATE_TEST_CHECK_EQUAL(returned, 1);
    GATE_TEST_CHECK_EQUAL(buffer[0], 'X');
    GATE_TEST_CHECK_OK(gate_stream_close(ptr_stream, GATE_STREAM_CLOSE_DEFAULT));
    gate_object_release(ptr_stream);
    ptr_stream = NULL;

    GATE_TEST_CHECK_OK(gate_file_delete(&test_file_path));

    {
        /* test file access with native platform stream interface */
        gate_platform_stream_t pfs;
        gate_int64_t pos;<--- Shadow variable
        gate_platform_stream_set_invalid(&pfs);
        GATE_TEST_CHECK_EQUAL(gate_platform_stream_valid(pfs), false);

        GATE_TEST_CHECK_OK(gate_platform_stream_open(test_file_path.str, GATE_PLATFORM_STREAM_OPEN_WRITE, 0, &pfs));
        GATE_TEST_CHECK_EQUAL(gate_platform_stream_valid(pfs), true);
        GATE_TEST_CHECK_OK(gate_platform_stream_write(pfs, "T", 1, &written));
        GATE_TEST_CHECK_EQUAL(written, 1);
        GATE_TEST_CHECK_OK(gate_platform_stream_close(&pfs));

        GATE_TEST_CHECK_OK(gate_platform_stream_open(test_file_path.str, GATE_PLATFORM_STREAM_OPEN_READ, 0, &pfs));
        GATE_TEST_CHECK_EQUAL(gate_platform_stream_valid(pfs), true);
        GATE_TEST_CHECK_OK(gate_platform_stream_read(pfs, buffer, sizeof(buffer), &returned));
        GATE_TEST_CHECK_OK(gate_platform_stream_seek(pfs, 0, GATE_PLATFORM_STREAM_ORIGIN_CURRENT, &pos));
        GATE_TEST_CHECK_OK(gate_platform_stream_seek(pfs, -1, GATE_PLATFORM_STREAM_ORIGIN_END, &pos));
        GATE_TEST_CHECK_OK(gate_platform_stream_seek(pfs, 0, GATE_PLATFORM_STREAM_ORIGIN_BEGIN, &pos));
        GATE_TEST_CHECK_EQUAL(returned, 1);
        GATE_TEST_CHECK_OK(gate_platform_stream_close(&pfs));

        GATE_TEST_CHECK_OK(gate_file_delete(&test_file_path));
    }

    GATE_TEST_UNIT_END;
}