summaryrefslogtreecommitdiff
path: root/test/test_csv.cpp
blob: cb93a32b9bb5dd2fd4b1ce32560d81629b6c6e41 (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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * libfud
 * Copyright 2024 Dominick Allen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "fud_csv.hpp"
#include "fud_print.hpp"

#include "gtest/gtest.h"

//NOLINTBEGIN(readability-magic-numbers)

namespace fud {

const StringView happyCsvFilename{u8"fud-happy-test.csv"};

const StringView happyData{
    u8"foo,bar,baz\n"
    u8"1,Unquoted Text,\"Quoted Text with embedded \"\" quote and embedded newline \nsee\"\n"
    u8",,\"Prior two fields are empty\"\n"};

Csv expectedHappyCsv() {
    Csv csv{Csv::makeDefault()};
    csv.numLines = 3;
    csv.numColumns = 3;

    auto resizeStatus = csv.entries.resize(csv.numLines * csv.numColumns);
    fudAssert(resizeStatus == FudStatus::Success);
    csv.entries[0] = StringView{u8"foo"};
    csv.entries[1] = StringView{u8"bar"};
    csv.entries[2] = StringView{u8"baz"};
    csv.entries[3] = StringView{u8"1"};
    csv.entries[4] = StringView{u8"Unquoted Text"};
    csv.entries[5] = StringView{u8"Quoted Text with embedded \" quote and embedded newline \nsee"};
    csv.entries[6] = StringView{};
    csv.entries[7] = StringView{};
    csv.entries[8] = StringView{u8"Prior two fields are empty"};

    for (const auto& entry : csv.entries) {
        if (entry.length() > 0) {
            auto extendStatus = csv.buffer.extend(entry.asSpan());
            fudAssert(extendStatus == FudStatus::Success);
        }
    }

    size_t totalSize = 0;
    for (auto& entry : csv.entries) {
        entry.m_data = csv.buffer.data() + totalSize;
        totalSize += entry.length();
    }

    return csv;
}

auto writeHappyCsv() -> FudStatus
{
    auto fileResult{RegularFile::create(
        happyCsvFilename,
        FileAccessMode::Write,
        OpenFlags{OpenFlagEnum::Truncate},
        PermUserRwGroupRead,
        false,
        NullOpt)};
    if (fileResult.isError()) {
        debugPrint(u8"Error opening file: {}\n", FudStatusToString(fileResult.getError()));
        return fileResult.takeError();
    }
    auto file{fileResult.takeOkay()};

    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
    auto writeResult = file.write(reinterpret_cast<const std::byte*>(happyData.data()), happyData.length());
    if (writeResult.status != FudStatus::Success) {
        return writeResult.status;
    }

    if (writeResult.bytesDrained != happyData.length()) {
        return FudStatus::Failure;
    }

    return FudStatus::Success;
}

TEST(FudCsv, ParseCsvFromFilename)
{
    Csv csv{Csv::makeDefault()};

    ASSERT_EQ(writeHappyCsv(), FudStatus::Success);

    auto parseStatus = Csv::parseFromFilenameUnbuffered(csv, happyCsvFilename);
    if (parseStatus != FudStatus::Success) {
        debugPrint(u8"Error parsing file: {}\n", FudStatusToString(parseStatus));
    }
    ASSERT_EQ(parseStatus, FudStatus::Success);

    auto expectedCsv{expectedHappyCsv()};
    ASSERT_EQ(expectedCsv.numLines, csv.numLines);
    ASSERT_EQ(expectedCsv.numColumns, csv.numColumns);
    ASSERT_EQ(expectedCsv.buffer.size(), csv.buffer.size());
    ASSERT_EQ(expectedCsv.entries.size(), csv.entries.size());
    EXPECT_EQ(0, compareMem(expectedCsv.buffer.data(), expectedCsv.buffer.size(), csv.buffer.data(), csv.buffer.size()).takeOkayOr(-1));
    for (size_t line = 0; line < csv.numLines; ++line)
    {
        for (size_t column = 0; column < csv.numColumns; ++column)
        {
            auto expectedEntryResult = expectedCsv.entry(line, column);
            ASSERT_TRUE(expectedEntryResult.isOkay());
            auto expectedEntryOpt{expectedEntryResult.takeOkay()};

            auto csvEntryResult = csv.entry(line, column);
            EXPECT_TRUE(csvEntryResult.isOkay());
            if (csvEntryResult.isError()) {
                continue;
            }
            auto csvEntryOpt{csvEntryResult.takeOkay()};

            EXPECT_EQ(expectedEntryOpt.hasValue(), csvEntryOpt.hasValue());
            if (expectedEntryOpt.isNone()) {
                continue;
            }
            auto expectedEntry{expectedEntryOpt.value()};
            auto csvEntry{csvEntryOpt.value()};

            EXPECT_EQ(expectedEntry.length(), csvEntry.length());
            EXPECT_EQ(0, compareMem(expectedEntry.data(), expectedEntry.length(), csvEntry.data(), csvEntry.length()).takeOkayOr(-1));
        }
    }
}

} // namespace fud

//NOLINTEND(readability-magic-numbers)