/
ClassCSV_Tests.cpp
123 lines (95 loc) · 2.53 KB
/
ClassCSV_Tests.cpp
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
/*
* #%~
* The VDM to C Code Generator
* %%
* Copyright (C) 2015 - 2016 Overture
* %%
* 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
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #~%
*/
#include "gtest/gtest.h"
//#include <unistd.h>
extern "C"
{
#include "Vdm.h"
#include "CSVLib.h"
}
#ifndef NO_CSV
TVP packString(const char *charseq)
{
int len = strlen(charseq);
int i;
TVP *theString;
theString = (TVP *)malloc(len * sizeof(TVP));
for(i = 0; i < len; i++)
{
theString[i] = newChar(charseq[i]);
}
return newSeqWithValues(len, theString);
}
TEST(ClassCSV, linecount)
{
TVP CSVFileAsSeq;
TVP lineCount;
const char *CSVContents[] =
{ "1,2,3,4",
"5,6,7,8",
"9,10,11,12",
"12.1,12.2,12.3,12.4",
"12.5,12.6,12.7,12.8"
};
FILE *CSVFile = fopen("CSVTestInput.csv", "w");
for(int i = 0; i < 5; i++)
{
fprintf(CSVFile, "%s\n", CSVContents[i]);
}
fclose(CSVFile);
CSVFileAsSeq = packString("CSVTestInput.csv");
lineCount = vdm_CSV_flinecount(CSVFileAsSeq);
UNWRAP_PRODUCT(col, lineCount);
EXPECT_EQ(5, col->value[1]->value.intVal);
vdmFree(lineCount);
vdmFree(CSVFileAsSeq);
}
TEST(ClassCSV, read_value)
{
TVP CSVFileAsSeq;
TVP lineInFileAsSeq;
const char *CSVContents[] =
{ "1,2,3,4",
"5,6,7,8",
"9,10,11,12",
"12.1,12.2,12.3,12.4",
"12.5,12.6,12.7,12.8"
};
FILE *CSVFile = fopen("CSVTestInput.csv", "w");
for(int i = 0; i < 5; i++)
{
fprintf(CSVFile, "%s\n", CSVContents[i]);
}
fclose(CSVFile);
CSVFileAsSeq = packString("CSVTestInput.csv");
lineInFileAsSeq = vdm_CSV_freadval(CSVFileAsSeq, newInt(4));
UNWRAP_PRODUCT(col, lineInFileAsSeq);
//lineAsSeq gets the sequence of chars.
UNWRAP_PRODUCT(lineAsSeq, col->value[1]);
EXPECT_EQ(12.1, lineAsSeq->value[0]->value.doubleVal);
EXPECT_EQ(12.2, lineAsSeq->value[1]->value.doubleVal);
EXPECT_EQ(12.3, lineAsSeq->value[2]->value.doubleVal);
EXPECT_EQ(12.4, lineAsSeq->value[3]->value.doubleVal);
vdmFree(CSVFileAsSeq);
vdmFree(lineInFileAsSeq);
}
#endif /* NO_CSV */