Browse code

passing package checks

Tom Sherman authored on 12/06/2018 19:23:36
Showing8 changed files

... ...
@@ -25,4 +25,10 @@
25 25
     #define GAPS_ASSERT_MSG(cond, msg) ((void)sizeof(cond))
26 26
 #endif 
27 27
 
28
+#define GAPS_ERROR(msg)                             \
29
+    do {                                            \
30
+        Rcpp::Rcout << "error: " << msg << '\n';    \
31
+        Rcpp::stop("CoGAPS aborted");                              \
32
+    } while(0)
33
+
28 34
 #endif
29 35
\ No newline at end of file
... ...
@@ -9,9 +9,14 @@
9 9
 
10 10
 TEST_CASE("Test Parsers")
11 11
 {
12
+    Rcpp::Environment env = Rcpp::Environment::global_env();
13
+    std::string csvPath = Rcpp::as<std::string>(env["gistCsvPath"]);
14
+    std::string tsvPath = Rcpp::as<std::string>(env["gistTsvPath"]);
15
+    std::string mtxPath = Rcpp::as<std::string>(env["gistMtxPath"]);
16
+
12 17
     SECTION("Test CsvParser")
13 18
     {
14
-        CsvParser p("../../inst/extdata/GIST.csv");
19
+        CsvParser p(csvPath);
15 20
         REQUIRE(p.nRow() == 1363);
16 21
         REQUIRE(p.nCol() == 9);
17 22
 
... ...
@@ -36,7 +41,7 @@ TEST_CASE("Test Parsers")
36 41
 
37 42
     SECTION("Test TsvParser")
38 43
     {
39
-        TsvParser p("../../inst/extdata/GIST.tsv");
44
+        TsvParser p(tsvPath);
40 45
         REQUIRE(p.nRow() == 1363);
41 46
         REQUIRE(p.nCol() == 9);
42 47
 
... ...
@@ -61,25 +66,21 @@ TEST_CASE("Test Parsers")
61 66
 
62 67
     SECTION("Test MtxParser")
63 68
     {
64
-        MtxParser p("../../inst/extdata/GIST.mtx");
69
+        MtxParser p(mtxPath);
65 70
         REQUIRE(p.nRow() == 1363);
66 71
         REQUIRE(p.nCol() == 9);
67 72
 
68
-        unsigned row = 0;
69
-        unsigned col = 0;
70 73
         unsigned count = 0;
71 74
         while (p.hasNext())
72 75
         {
73 76
             MatrixElement e(p.getNext());
74
-            REQUIRE(e.row() == row);
75
-            REQUIRE(e.col() == col);
77
+
78
+            REQUIRE(e.row() < 1363);
79
+            REQUIRE(e.row() >= 0);
80
+            REQUIRE(e.col() < 9);
81
+            REQUIRE(e.col() >= 0);
76 82
 
77 83
             ++count;
78
-            ++row;
79
-            if (row == 1363) {
80
-                ++col;
81
-                row = 0;
82
-            }
83 84
         }
84 85
         REQUIRE(count == 12267);
85 86
     }
... ...
@@ -51,410 +51,90 @@ TEST_CASE("Test Matrix.h")
51 51
         REQUIRE(cmS.nRow() == 1363);
52 52
         REQUIRE(cmS.nCol() == 9);
53 53
     }
54
+}
54 55
 
55
-    SECTION("Matrix Initialization from .tsv file")
56
+static void populateSequential(std::vector<unsigned> &vec, unsigned n)
57
+{
58
+    for (unsigned i = 0; i < n; ++i)
56 59
     {
57
-        std::vector<unsigned> whichRows;
58
-        std::vector<unsigned> whichCols;
59
-        for (unsigned i = 0; i < 1363; ++i)
60
-        {
61
-            whichRows.push_back(i);
62
-        }
63
-        for (unsigned i = 0; i < 9; ++i)
64
-        {
65
-            whichCols.push_back(i);
66
-        }
67
-        TsvParser p1("../../inst/extdata/GIST.tsv");
68
-        RowMatrix tsvRowMatrix(p1, true, whichRows);
69
-        REQUIRE(tsvRowMatrix.nRow() == 1363);
70
-        REQUIRE(tsvRowMatrix.nCol() == 9);
71
-
72
-        TsvParser p2("../../inst/extdata/GIST.tsv");
73
-        ColMatrix tsvColMatrix(p2, true, whichRows);
74
-        REQUIRE(tsvColMatrix.nRow() == 1363);
75
-        REQUIRE(tsvColMatrix.nCol() == 9);
76
-
77
-        TsvParser p3("../../inst/extdata/GIST.tsv");
78
-        RowMatrix tsvRowMatrixT(p3, false, whichCols);
79
-        REQUIRE(tsvRowMatrixT.nRow() == 9);
80
-        REQUIRE(tsvRowMatrixT.nCol() == 1363);
81
-
82
-        TsvParser p4("../../inst/extdata/GIST.tsv");
83
-        ColMatrix tsvColMatrixT(p4, false, whichCols);
84
-        REQUIRE(tsvColMatrixT.nRow() == 9);
85
-        REQUIRE(tsvColMatrixT.nCol() == 1363);
86
-
87
-        float sum = 0;
88
-        for (unsigned i = 0; i < tsvRowMatrix.nCol(); ++i)
89
-        {
90
-            sum += tsvRowMatrix.getRow(0)[i];
91
-        }
92
-        sum *= 10;
93
-        sum = (int) sum;
94
-        REQUIRE(sum == 97);
95
-
96
-        sum = 0;
97
-        for (unsigned i = 0; i < tsvColMatrix.nCol(); ++i)
98
-        {
99
-            sum += tsvColMatrix.getCol(i)[0];
100
-        }
101
-        sum *= 10;
102
-        sum = (int) sum;
103
-        REQUIRE(sum == 97);
104
-
105
-        sum = 0;
106
-        for (unsigned i = 0; i < tsvRowMatrixT.nRow(); ++i)
107
-        {
108
-            sum += tsvRowMatrixT.getRow(i)[0];
109
-        }
110
-        sum *= 10;
111
-        sum = (int) sum;
112
-        REQUIRE(sum == 97);
113
-
114
-        sum = 0;
115
-        for (unsigned i = 0; i < tsvRowMatrixT.nRow(); ++i)
116
-        {
117
-            sum += tsvColMatrixT.getCol(0)[i];
118
-        }
119
-        sum *= 10;
120
-        sum = (int) sum;
121
-        REQUIRE(sum == 97);
122
-
123
-        for (unsigned i = 0; i < 1000; ++i)
124
-        {
125
-            whichRows.pop_back();
126
-        }
127
-
128
-        for (unsigned i = 0; i < 7; ++i)
129
-        {
130
-            whichCols.pop_back();
131
-        }
132
-
133
-        TsvParser p5("../../inst/extdata/GIST.tsv");
134
-        tsvRowMatrix = RowMatrix(p5, true, whichRows);
135
-        REQUIRE(tsvRowMatrix.nRow() == 363);
136
-        REQUIRE(tsvRowMatrix.nCol() == 9);
137
-
138
-        TsvParser p6("../../inst/extdata/GIST.tsv");
139
-        tsvColMatrix = ColMatrix(p6, true, whichRows);
140
-        REQUIRE(tsvColMatrix.nRow() == 363);
141
-        REQUIRE(tsvColMatrix.nCol() == 9);
142
-
143
-        TsvParser p7("../../inst/extdata/GIST.tsv");
144
-        tsvRowMatrixT = RowMatrix(p7, false, whichCols);
145
-        REQUIRE(tsvRowMatrixT.nRow() == 2);
146
-        REQUIRE(tsvRowMatrixT.nCol() == 1363);
147
-
148
-        TsvParser p8("../../inst/extdata/GIST.tsv");
149
-        tsvColMatrixT = ColMatrix(p8, false, whichCols);
150
-        REQUIRE(tsvColMatrixT.nRow() == 2);
151
-        REQUIRE(tsvColMatrixT.nCol() == 1363);
152
-
153
-        sum = 0;
154
-        for (unsigned i = 0; i < tsvRowMatrix.nCol(); ++i)
155
-        {
156
-            sum += tsvRowMatrix.getRow(0)[i];
157
-        }
158
-        sum *= 10;
159
-        sum = (int) sum;
160
-        REQUIRE(sum == 97);
161
-
162
-        sum = 0;
163
-        for (unsigned i = 0; i < tsvColMatrix.nCol(); ++i)
164
-        {
165
-            sum += tsvColMatrix.getCol(i)[0];
166
-        }
167
-        sum *= 10;
168
-        sum = (int) sum;
169
-        REQUIRE(sum == 97);
170
-
171
-        sum = 0;
172
-        for (unsigned i = 0; i < tsvRowMatrixT.nRow(); ++i)
173
-        {
174
-            sum += tsvRowMatrixT.getRow(i)[0];
175
-        }
176
-        sum *= 10;
177
-        sum = (int) sum;
178
-        REQUIRE(sum == 20);
179
-
180
-        sum = 0;
181
-        for (unsigned i = 0; i < tsvRowMatrixT.nRow(); ++i)
182
-        {
183
-            sum += tsvColMatrixT.getCol(0)[i];
184
-        }
185
-        sum *= 10;
186
-        sum = (int) sum;
187
-        REQUIRE(sum == 20);
60
+        vec.push_back(i);
188 61
     }
62
+}
189 63
 
190
-    SECTION("Matrix Initialization from .mtx file")
64
+template <class Parser>
65
+static void testMatrixConstruction(const std::string &path)
66
+{
67
+    std::vector<unsigned> allRows, allCols;
68
+    std::vector<unsigned> someRows, someCols;
69
+
70
+    populateSequential(allRows, 1363);
71
+    populateSequential(allCols, 9);
72
+    populateSequential(someRows, 363);
73
+    populateSequential(someCols, 2);
74
+
75
+    Parser p1(path);
76
+    RowMatrix allRowMat(p1, true, allRows);
77
+    REQUIRE(allRowMat.nRow() == 1363);
78
+    REQUIRE(allRowMat.nCol() == 9);
79
+
80
+    Parser p2(path);
81
+    ColMatrix allColMat(p2, true, allRows);
82
+    REQUIRE(allColMat.nRow() == 1363);
83
+    REQUIRE(allColMat.nCol() == 9);
84
+
85
+    Parser p3(path);
86
+    RowMatrix allRowMatT(p3, false, allCols);
87
+    REQUIRE(allRowMatT.nRow() == 9);
88
+    REQUIRE(allRowMatT.nCol() == 1363);
89
+
90
+    Parser p4(path);
91
+    ColMatrix allColMatT(p4, false, allCols);
92
+    REQUIRE(allColMatT.nRow() == 9);
93
+    REQUIRE(allColMatT.nCol() == 1363);
94
+
95
+    for (unsigned i = 0; i < 1363; ++i)
191 96
     {
192
-        std::vector<unsigned> whichRows;
193
-        std::vector<unsigned> whichCols;
194
-        for (unsigned i = 0; i < 1363; ++i)
195
-        {
196
-            whichRows.push_back(i);
197
-        }
198
-        for (unsigned i = 0; i < 9; ++i)
199
-        {
200
-            whichCols.push_back(i);
201
-        }
202
-
203
-        MtxParser p1("../../inst/extdata/GIST.mtx");
204
-        RowMatrix mtxRowMatrix(p1, true, whichRows);
205
-        REQUIRE(mtxRowMatrix.nRow() == 1363);
206
-        REQUIRE(mtxRowMatrix.nCol() == 9);
207
-
208
-        MtxParser p2("../../inst/extdata/GIST.mtx");
209
-        ColMatrix mtxColMatrix(p2, true, whichRows);
210
-        REQUIRE(mtxColMatrix.nRow() == 1363);
211
-        REQUIRE(mtxColMatrix.nCol() == 9);
212
-
213
-        MtxParser p3("../../inst/extdata/GIST.mtx");
214
-        RowMatrix mtxRowMatrixT = RowMatrix(p3, false, whichCols);
215
-        REQUIRE(mtxRowMatrixT.nRow() == 9);
216
-        REQUIRE(mtxRowMatrixT.nCol() == 1363);
217
-
218
-        MtxParser p4("../../inst/extdata/GIST.mtx");
219
-        ColMatrix mtxColMatrixT = ColMatrix(p4, false, whichCols);
220
-        REQUIRE(mtxColMatrixT.nRow() == 9);
221
-        REQUIRE(mtxColMatrixT.nCol() == 1363);
222
-
223
-        float sum = 0;
224
-        for (unsigned i = 0; i < mtxRowMatrix.nCol(); ++i)
225
-        {
226
-            sum += mtxRowMatrix.getRow(0)[i];
227
-        }
228
-        sum *= 10;
229
-        sum = (int) sum;
230
-        REQUIRE(sum == 97);
231
-
232
-        sum = 0;
233
-        for (unsigned i = 0; i < mtxColMatrix.nCol(); ++i)
234
-        {
235
-            sum += mtxColMatrix.getCol(i)[0];
236
-        }
237
-        sum *= 10;
238
-        sum = (int) sum;
239
-        REQUIRE(sum == 97);
240
-
241
-        sum = 0;
242
-        for (unsigned i = 0; i < mtxRowMatrixT.nRow(); ++i)
243
-        {
244
-            sum += mtxRowMatrixT.getRow(i)[0];
245
-        }
246
-        sum *= 10;
247
-        sum = (int) sum;
248
-        REQUIRE(sum == 97);
249
-
250
-        sum = 0;
251
-        for (unsigned i = 0; i < mtxRowMatrixT.nRow(); ++i)
252
-        {
253
-            sum += mtxColMatrixT.getCol(0)[i];
254
-        }
255
-        sum *= 10;
256
-        sum = (int) sum;
257
-        REQUIRE(sum == 97);
258
-
259
-        for (unsigned i = 0; i < 1000; ++i)
260
-        {
261
-            whichRows.pop_back();
262
-        }
263
-
264
-        for (unsigned i = 0; i < 7; ++i)
265
-        {
266
-            whichCols.pop_back();
267
-        }
268
-
269
-        MtxParser p5("../../inst/extdata/GIST.mtx");
270
-        mtxRowMatrix = RowMatrix(p5, true, whichRows);
271
-        REQUIRE(mtxRowMatrix.nRow() == 363);
272
-        REQUIRE(mtxRowMatrix.nCol() == 9);
273
-
274
-        MtxParser p6("../../inst/extdata/GIST.mtx");
275
-        mtxColMatrix = ColMatrix(p6, true, whichRows);
276
-        REQUIRE(mtxColMatrix.nRow() == 363);
277
-        REQUIRE(mtxColMatrix.nCol() == 9);
278
-
279
-        MtxParser p7("../../inst/extdata/GIST.mtx");
280
-        mtxRowMatrixT = RowMatrix(p7, false, whichCols);
281
-        REQUIRE(mtxRowMatrixT.nRow() == 2);
282
-        REQUIRE(mtxRowMatrixT.nCol() == 1363);
283
-
284
-        MtxParser p8("../../inst/extdata/GIST.mtx");
285
-        mtxColMatrixT = ColMatrix(p8, false, whichCols);
286
-        REQUIRE(mtxColMatrixT.nRow() == 2);
287
-        REQUIRE(mtxColMatrixT.nCol() == 1363);
288
-
289
-        sum = 0;
290
-        for (unsigned i = 0; i < mtxRowMatrix.nCol(); ++i)
291
-        {
292
-            sum += mtxRowMatrix.getRow(0)[i];
293
-        }
294
-        sum *= 10;
295
-        sum = (int) sum;
296
-        REQUIRE(sum == 97);
297
-
298
-        sum = 0;
299
-        for (unsigned i = 0; i < mtxColMatrix.nCol(); ++i)
300
-        {
301
-            sum += mtxColMatrix.getCol(i)[0];
302
-        }
303
-        sum *= 10;
304
-        sum = (int) sum;
305
-        REQUIRE(sum == 97);
306
-
307
-        sum = 0;
308
-        for (unsigned i = 0; i < mtxRowMatrixT.nRow(); ++i)
309
-        {
310
-            sum += mtxRowMatrixT.getRow(i)[0];
311
-        }
312
-        sum *= 10;
313
-        sum = (int) sum;
314
-        REQUIRE(sum == 20);
315
-
316
-        sum = 0;
317
-        for (unsigned i = 0; i < mtxRowMatrixT.nRow(); ++i)
318
-        {
319
-            sum += mtxColMatrixT.getCol(0)[i];
320
-        }
321
-        sum *= 10;
322
-        sum = (int) sum;
323
-        REQUIRE(sum == 20);
97
+        REQUIRE(allRowMat(i, 3) == allColMat(i, 3));
98
+        REQUIRE(allRowMat(i, 3) == allRowMatT(3, i));
99
+        REQUIRE(allRowMat(i, 3) == allColMatT(3, i));
324 100
     }
325 101
 
326
-    SECTION("Matrix Initialization from .csv file")
327
-    {
328
-        std::vector<unsigned> whichRows;
329
-        std::vector<unsigned> whichCols;
330
-        for (unsigned i = 0; i < 1363; ++i)
331
-        {
332
-            whichRows.push_back(i);
333
-        }
334
-        for (unsigned i = 0; i < 9; ++i)
335
-        {
336
-            whichCols.push_back(i);
337
-        }
338
-        CsvParser p1("../../inst/extdata/GIST.csv");
339
-        RowMatrix csvRowMatrix(p1, true, whichRows);
340
-        REQUIRE(csvRowMatrix.nRow() == 1363);
341
-        REQUIRE(csvRowMatrix.nCol() == 9);
342
-
343
-        CsvParser p2("../../inst/extdata/GIST.csv");
344
-        ColMatrix csvColMatrix(p2, true, whichRows);
345
-        REQUIRE(csvColMatrix.nRow() == 1363);
346
-        REQUIRE(csvColMatrix.nCol() == 9);
347
-
348
-        CsvParser p3("../../inst/extdata/GIST.csv");
349
-        RowMatrix csvRowMatrixT = RowMatrix(p3, false, whichCols);
350
-        REQUIRE(csvRowMatrixT.nRow() == 9);
351
-        REQUIRE(csvRowMatrixT.nCol() == 1363);
102
+    Parser p5(path);
103
+    RowMatrix someRowMat(p1, true, someRows);
104
+    REQUIRE(someRowMat.nRow() == 363);
105
+    REQUIRE(someRowMat.nCol() == 9);
352 106
 
353
-        CsvParser p4("../../inst/extdata/GIST.csv");
354
-        ColMatrix csvColMatrixT = ColMatrix(p4, false, whichCols);
355
-        REQUIRE(csvColMatrixT.nRow() == 9);
356
-        REQUIRE(csvColMatrixT.nCol() == 1363);
107
+    Parser p6(path);
108
+    ColMatrix someColMat(p2, true, someRows);
109
+    REQUIRE(someColMat.nRow() == 363);
110
+    REQUIRE(someColMat.nCol() == 9);
357 111
 
358
-        float sum = 0;
359
-        for (unsigned i = 0; i < csvRowMatrix.nCol(); ++i)
360
-        {
361
-            sum += csvRowMatrix.getRow(0)[i];
362
-        }
363
-        sum *= 10;
364
-        sum = (int) sum;
365
-        REQUIRE(sum == 97);
112
+    Parser p7(path);
113
+    RowMatrix someRowMatT(p3, false, someCols);
114
+    REQUIRE(someRowMatT.nRow() == 2);
115
+    REQUIRE(someRowMatT.nCol() == 1363);
366 116
 
367
-        sum = 0;
368
-        for (unsigned i = 0; i < csvColMatrix.nCol(); ++i)
369
-        {
370
-            sum += csvColMatrix.getCol(i)[0];
371
-        }
372
-        sum *= 10;
373
-        sum = (int) sum;
374
-        REQUIRE(sum == 97);
117
+    Parser p8(path);
118
+    ColMatrix someColMatT(p4, false, someCols);
119
+    REQUIRE(someColMatT.nRow() == 2);
120
+    REQUIRE(someColMatT.nCol() == 1363);
375 121
 
376
-        sum = 0;
377
-        for (unsigned i = 0; i < csvRowMatrixT.nRow(); ++i)
378
-        {
379
-            sum += csvRowMatrixT.getRow(i)[0];
380
-        }
381
-        sum *= 10;
382
-        sum = (int) sum;
383
-        REQUIRE(sum == 97);
384
-
385
-        sum = 0;
386
-        for (unsigned i = 0; i < csvRowMatrixT.nRow(); ++i)
387
-        {
388
-            sum += csvColMatrixT.getCol(0)[i];
389
-        }
390
-        sum *= 10;
391
-        sum = (int) sum;
392
-        REQUIRE(sum == 97);
393
-
394
-        for (unsigned i = 0; i < 1000; ++i)
395
-        {
396
-            whichRows.pop_back();
397
-        }
398
-
399
-        for (unsigned i = 0; i < 7; ++i)
400
-        {
401
-            whichCols.pop_back();
402
-        }
403
-
404
-        CsvParser p5("../../inst/extdata/GIST.csv");
405
-        csvRowMatrix = RowMatrix(p5, true, whichRows);
406
-        REQUIRE(csvRowMatrix.nRow() == 363);
407
-        REQUIRE(csvRowMatrix.nCol() == 9);
408
-
409
-        CsvParser p6("../../inst/extdata/GIST.csv");
410
-        csvColMatrix = ColMatrix(p6, true, whichRows);
411
-        REQUIRE(csvColMatrix.nRow() == 363);
412
-        REQUIRE(csvColMatrix.nCol() == 9);
413
-
414
-        CsvParser p7("../../inst/extdata/GIST.csv");
415
-        csvRowMatrixT = RowMatrix(p7, false, whichCols);
416
-        REQUIRE(csvRowMatrixT.nRow() == 2);
417
-        REQUIRE(csvRowMatrixT.nCol() == 1363);
418
-
419
-        CsvParser p8("../../inst/extdata/GIST.csv");
420
-        csvColMatrixT = ColMatrix(p8, false, whichCols);
421
-        REQUIRE(csvColMatrixT.nRow() == 2);
422
-        REQUIRE(csvColMatrixT.nCol() == 1363);
423
-
424
-        sum = 0;
425
-        for (unsigned i = 0; i < csvRowMatrix.nCol(); ++i)
426
-        {
427
-            sum += csvRowMatrix.getRow(0)[i];
428
-        }
429
-        sum *= 10;
430
-        sum = (int) sum;
431
-        REQUIRE(sum == 97);
432
-
433
-        sum = 0;
434
-        for (unsigned i = 0; i < csvColMatrix.nCol(); ++i)
435
-        {
436
-            sum += csvColMatrix.getCol(i)[0];
437
-        }
438
-        sum *= 10;
439
-        sum = (int) sum;
440
-        REQUIRE(sum == 97);
441
-
442
-        sum = 0;
443
-        for (unsigned i = 0; i < csvRowMatrixT.nRow(); ++i)
444
-        {
445
-            sum += csvRowMatrixT.getRow(i)[0];
446
-        }
447
-        sum *= 10;
448
-        sum = (int) sum;
449
-        REQUIRE(sum == 20);
450
-
451
-        sum = 0;
452
-        for (unsigned i = 0; i < csvRowMatrixT.nRow(); ++i)
453
-        {
454
-            sum += csvColMatrixT.getCol(0)[i];
455
-        }
456
-        sum *= 10;
457
-        sum = (int) sum;
458
-        REQUIRE(sum == 20);
122
+    for (unsigned i = 0; i < 363; ++i)
123
+    {
124
+        REQUIRE(someRowMat(i, 1) == someColMat(i, 1));
125
+        REQUIRE(someRowMat(i, 1) == someRowMatT(1, i));
126
+        REQUIRE(someRowMat(i, 1) == someColMatT(1, i));
459 127
     }
460 128
 }
129
+
130
+TEST_CASE("Test Matrix Construction from file")
131
+{
132
+    Rcpp::Environment env = Rcpp::Environment::global_env();
133
+    std::string csvPath = Rcpp::as<std::string>(env["gistCsvPath"]);
134
+    std::string tsvPath = Rcpp::as<std::string>(env["gistTsvPath"]);
135
+    std::string mtxPath = Rcpp::as<std::string>(env["gistMtxPath"]);
136
+
137
+    testMatrixConstruction<CsvParser>(csvPath);
138
+    testMatrixConstruction<TsvParser>(tsvPath);
139
+    testMatrixConstruction<MtxParser>(mtxPath);
140
+}
461 141
\ No newline at end of file
... ...
@@ -103,7 +103,8 @@ inline void fill(Matrix &mat, Parser &p, bool partitionRows, std::vector<unsigne
103 103
         std::vector<unsigned>::iterator newRowIndex = std::find(whichIndices.begin(), whichIndices.end(), e[rowSelect]);
104 104
         if (newRowIndex != whichIndices.end())
105 105
         {
106
-            mat.operator()(std::distance(whichIndices.begin(), newRowIndex), e[colSelect]) = e.value;
106
+            unsigned row = std::distance(whichIndices.begin(), newRowIndex);
107
+            mat.operator()(row, e[colSelect]) = e.value;
107 108
         }
108 109
     }
109 110
 }
... ...
@@ -1,4 +1,5 @@
1 1
 #include "CsvParser.h"
2
+#include "../GapsAssert.h"
2 3
 #include "../math/Algorithms.h"
3 4
 
4 5
 #include <fstream>
... ...
@@ -15,12 +16,20 @@ mCurrentRow(0), mCurrentCol(0)
15 16
     // read first entry (blank)
16 17
     std::string line;
17 18
     std::getline(file_str, line, ',');
19
+    if (file_str.eof() || file_str.fail())
20
+    {
21
+        GAPS_ERROR("Invalid CSV file");
22
+    }
18 23
 
19 24
     // get col size
20 25
     std::size_t pos;
21 26
     do
22 27
     {
23 28
         std::getline(file_str, line, ',');
29
+        if (file_str.eof() || file_str.fail())
30
+        {
31
+            GAPS_ERROR("Invalid CSV file");
32
+        }
24 33
         ++mNumCols;
25 34
     }
26 35
     while ((pos = line.find('\n')) == std::string::npos);
... ...
@@ -50,6 +59,7 @@ mCurrentRow(0), mCurrentCol(0)
50 59
 
51 60
 bool CsvParser::hasNext()
52 61
 {
62
+    mFile >> std::ws;
53 63
     return mFile.peek() != EOF;
54 64
 }
55 65
 
... ...
@@ -1,17 +1,30 @@
1 1
 #include "MtxParser.h"
2 2
 #include "MatrixElement.h"
3
+#include "../GapsAssert.h"
3 4
 
4 5
 #include <sstream>
6
+#include <Rcpp.h>
5 7
 
6 8
 MtxParser::MtxParser(const std::string &path) : mNumRows(0), mNumCols(0)
7 9
 {
8 10
     mFile.open(path.c_str());
9 11
 
12
+    // read first line
13
+    std::string line;
14
+    std::getline(mFile, line);
15
+    if (mFile.eof() || mFile.fail())
16
+    {
17
+        GAPS_ERROR("Invalid MTX file");
18
+    }
19
+
10 20
     // skip over comments
11
-    std::string line = "%";
12 21
     while (line.find('%') != std::string::npos)
13 22
     {
14 23
         std::getline(mFile, line);
24
+        if (mFile.eof() || mFile.fail())
25
+        {
26
+            GAPS_ERROR("Invalid MTX file");
27
+        }
15 28
     }
16 29
     std::stringstream ss(line); // this line contains dimensions
17 30
 
... ...
@@ -21,17 +34,18 @@ MtxParser::MtxParser(const std::string &path) : mNumRows(0), mNumCols(0)
21 34
 
22 35
 bool MtxParser::hasNext()
23 36
 {
37
+    mFile >> std::ws; // get rid of whitespace
24 38
     return mFile.peek() != EOF;
25 39
 }
26 40
 
27 41
 MatrixElement MtxParser::getNext()
28 42
 {
29
-    MatrixElement e(0, 0, 0.f);
30
-    unsigned buff;
31
-    mFile >> buff;
32
-    e.dim[0] = buff - 1;
33
-    mFile >> buff;
34
-    e.dim[1] = buff - 1;
35
-    mFile >> e.value;
36
-    return e;
43
+    unsigned row = 0, col = 0;
44
+    float val = 0.f;
45
+
46
+    mFile >> row;
47
+    mFile >> col;
48
+    mFile >> val;
49
+
50
+    return MatrixElement(row - 1, col - 1, val);
37 51
 }
... ...
@@ -1,4 +1,5 @@
1 1
 #include "TsvParser.h"
2
+#include "../GapsAssert.h"
2 3
 #include "../math/Algorithms.h"
3 4
 
4 5
 #include <fstream>
... ...
@@ -15,12 +16,21 @@ mCurrentRow(0), mCurrentCol(0)
15 16
     // read first entry (blank)
16 17
     std::string line;
17 18
     std::getline(file_str, line, '\t');
19
+    if (file_str.eof() || file_str.fail())
20
+    {
21
+        GAPS_ERROR("Invalid TSV file");
22
+    }
18 23
 
19 24
     // get col size
20 25
     std::size_t pos;
21 26
     do
22 27
     {
23 28
         std::getline(file_str, line, '\t');
29
+        if (file_str.eof() || file_str.fail())
30
+        {
31
+            GAPS_ERROR("Invalid TSV file");
32
+        }
33
+
24 34
         ++mNumCols;
25 35
     }
26 36
     while ((pos = line.find('\n')) == std::string::npos);
... ...
@@ -50,6 +60,7 @@ mCurrentRow(0), mCurrentCol(0)
50 60
 
51 61
 bool TsvParser::hasNext()
52 62
 {
63
+    mFile >> std::ws;
53 64
     return mFile.peek() != EOF;
54 65
 }
55 66
 
... ...
@@ -5,7 +5,9 @@ test_that("Catch unit tests pass",
5 5
     data(SimpSim)
6 6
     data(GIST)
7 7
 
8
-    gistMtxPath <- system.file("data/GIST.mtx", package="CoGAPS")
8
+    gistCsvPath <<- system.file("extdata/GIST.csv", package="CoGAPS")
9
+    gistTsvPath <<- system.file("extdata/GIST.tsv", package="CoGAPS")
10
+    gistMtxPath <<- system.file("extdata/GIST.mtx", package="CoGAPS")
9 11
 
10 12
     run_catch_unit_tests()
11 13
 })