aboutsummaryrefslogtreecommitdiff
path: root/src/armnn/test/CsvReaderTest.cpp
blob: ed27e57f8e109f5be3ab5250ef4b7eb84ab1b525 (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
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//
#include "CsvReader.hpp"

#include <boost/algorithm/string.hpp>
#include <boost/test/unit_test.hpp>

#include <iostream>
#include <string>
#include <boost/filesystem.hpp>
#include <boost/optional.hpp>

using namespace armnnUtils;

namespace {
struct TestHelper {

    TestHelper()
    {
        BOOST_TEST_MESSAGE("setup fixture");
    }

    ~TestHelper()
    {
        BOOST_TEST_MESSAGE("teardown fixture");
        TearDown();
    }

    std::string CreateTempCsvFile()
    {
        boost::filesystem::path fileDir = boost::filesystem::temp_directory_path();
        boost::filesystem::path p{fileDir / boost::filesystem::unique_path("%%%%-%%%%-%%%%.csv")};
        try
        {
            boost::filesystem::ofstream ofs{p};
            ofs << "airplane, bicycle , bird , \"m,o,n,k,e,y\"\n";
            ofs << "banana, shoe, \"ice\"";
            ofs.close();
        } catch (std::exception &e)
        {
            std::cerr << "Unable to write to file at location [" << p.c_str() << "] : " << e.what() << std::endl;
            BOOST_TEST(false);
        }

        m_CsvFile = p;
        return p.string();
    }

    int CheckStringsMatch(CsvRow &row, unsigned int index, std::string expectedValue)
    {
        return row.values.at(index).compare(expectedValue);
    }

    void TearDown()
    {
        RemoveCsvFile();
    }

    void RemoveCsvFile()
    {
        if (m_CsvFile)
        {
            try
            {
                boost::filesystem::remove(*m_CsvFile);
            }
            catch (std::exception &e)
            {
                std::cerr << "Unable to delete file [" << *m_CsvFile << "] : " << e.what() << std::endl;
                BOOST_TEST(false);
            }
        }
    }

    boost::optional<boost::filesystem::path> m_CsvFile;
};
}

BOOST_AUTO_TEST_SUITE(CsvReaderTest)

BOOST_FIXTURE_TEST_CASE(TestParseVector, TestHelper)
{
    CsvReader reader;
    std::vector<std::string> csvStrings;
    csvStrings.reserve(2);
    csvStrings.push_back("airplane, automobile , bird , \"c,a,t\"");
    csvStrings.push_back("banana, shoe, \"ice\"");

    std::vector<CsvRow> row = reader.ParseVector(csvStrings);
    CsvRow row1 = row[0];
    CsvRow row2 = row[1];

    BOOST_CHECK(row.size() == 2);

    BOOST_CHECK(row1.values.size() == 4);
    BOOST_CHECK(CheckStringsMatch(row1, 0, "airplane") == 0);
    BOOST_CHECK(CheckStringsMatch(row1, 1, "automobile") == 0);
    BOOST_CHECK(CheckStringsMatch(row1, 2, "bird") == 0);
    BOOST_CHECK(CheckStringsMatch(row1, 3, "c,a,t") == 0);

    BOOST_CHECK(row2.values.size() == 3);
    BOOST_CHECK(CheckStringsMatch(row2, 0, "banana") == 0);
    BOOST_CHECK(CheckStringsMatch(row2, 1, "shoe") == 0);
    BOOST_CHECK(CheckStringsMatch(row2, 2, "ice") == 0);
}

BOOST_FIXTURE_TEST_CASE(TestLoadingFileFromDisk, TestHelper)
{
    CsvReader reader;
    std::string theFilePath = TestHelper::CreateTempCsvFile();

    std::vector<CsvRow> row = reader.ParseFile(theFilePath);
    CsvRow row1 = row[0];
    CsvRow row2 = row[1];

    BOOST_CHECK(row.size() == 2);

    BOOST_CHECK(row1.values.size() == 4);
    BOOST_CHECK(CheckStringsMatch(row1, 0, "airplane") == 0);
    BOOST_CHECK(CheckStringsMatch(row1, 1, "bicycle") == 0);
    BOOST_CHECK(CheckStringsMatch(row1, 2, "bird") == 0);
    BOOST_CHECK(CheckStringsMatch(row1, 3, "m,o,n,k,e,y") == 0);

    BOOST_CHECK(row2.values.size() == 3);
    BOOST_CHECK(CheckStringsMatch(row2, 0, "banana") == 0);
    BOOST_CHECK(CheckStringsMatch(row2, 1, "shoe") == 0);
    BOOST_CHECK(CheckStringsMatch(row2, 2, "ice") == 0);
}

BOOST_AUTO_TEST_SUITE_END()