summaryrefslogtreecommitdiffstats
path: root/tools/slang-unit-test/unit-test-io.cpp
blob: 568a8c58f199adea29d1518e392552a3ccf87de5 (plain)
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
// unit-test-io.cpp

#include "../../source/core/slang-io.h"
#include "unit-test/slang-unit-test.h"

using namespace Slang;

static SlangResult _checkGenerateTemporary()
{
    /// Test temporary file functionality

    List<String> paths;

    for (Index i = 0; i < 10; ++i)
    {
        String path;
        SLANG_RETURN_ON_FAIL(File::generateTemporary(toSlice("slang-check"), path));

        // The path should exist exist
        SLANG_CHECK(File::exists(path));

        if (paths.contains(path))
        {
            return SLANG_FAIL;
        }

        paths.add(path);
    }

    // It should be possible to write to the temporary files
    for (auto& path : paths)
    {
        SLANG_RETURN_ON_FAIL(File::writeAllText(path, path));
    }
    // It should be possible to read from the temporary files

    for (auto& path : paths)
    {
        String contents;
        SLANG_RETURN_ON_FAIL(File::readAllText(path, contents))

        SLANG_CHECK(contents == path);
    }

    // Remove all the temporary files
    for (auto& path : paths)
    {
        SLANG_CHECK(File::exists(path));

        const auto removeResult = File::remove(path);
        SLANG_CHECK(SLANG_SUCCEEDED(removeResult));

        // Check remove worked
        SLANG_CHECK(!File::exists(path));
    }

    return SLANG_OK;
}

SLANG_UNIT_TEST(io)
{
    SLANG_CHECK(SLANG_SUCCEEDED(_checkGenerateTemporary()));
}