Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_CSVReader/tests/test_csvreader.cpp
Views: 1799
#include <AP_gtest.h>1#include <AP_Common/AP_Common.h>23#include <AP_Math/AP_Math.h>45#include <AP_CSVReader/AP_CSVReader.h>67TEST(AP_CSVReader, basic)8{9static const char *basic_csv =10"A 1\n"11"B 2\n"12"C 3\n"13"Fred 31\n"14;15static const char *basic_csv_crlf =16"A 1\r\n"17"B 2\r\n"18"C 3\r\n"19"Fred 31\r\n"20;21static const char *basic_csv_results[][2] = {22{"A", "1"},23{"B", "2"},24{"C", "3"},25{"Fred", "31"}26};2728uint8_t term[16];29AP_CSVReader csvreader{term, ARRAY_SIZE(term), ' '};3031const char *csvs[] {32basic_csv,33basic_csv_crlf34};3536for (const char *csv : csvs) {37uint8_t termcount = 0;38uint8_t linecount = 0;39for (uint8_t i=0; i<strlen(csv); i++) {40switch (csvreader.feed(csv[i])) {41case AP_CSVReader::RetCode::ERROR:42abort();43case AP_CSVReader::RetCode::OK:44continue;45case AP_CSVReader::RetCode::TERM_DONE:46EXPECT_STREQ(basic_csv_results[linecount][termcount], (char*)term);47termcount++;48continue;49case AP_CSVReader::RetCode::VECTOR_DONE:50EXPECT_STREQ(basic_csv_results[linecount][termcount], (char*)term);51termcount++;52EXPECT_EQ(termcount, 2);53termcount = 0;54linecount++;55continue;56}57}5859EXPECT_EQ(linecount, 4);60EXPECT_EQ(termcount, 0);61}62}6364TEST(AP_CSVReader, commabasic)65{66static const char *basic_csv =67"A,1\n"68"B,2\n"69"C,3\n"70"Fred,31\n"71;72static const char *basic_csv_results[][2] = {73{"A", "1"},74{"B", "2"},75{"C", "3"},76{"Fred", "31"}77};7879uint8_t term[16];80AP_CSVReader csvreader{term, ARRAY_SIZE(term), ','};8182uint8_t termcount = 0;83uint8_t linecount = 0;84for (uint8_t i=0; i<strlen(basic_csv); i++) {85switch (csvreader.feed(basic_csv[i])) {86case AP_CSVReader::RetCode::ERROR:87abort();88case AP_CSVReader::RetCode::OK:89continue;90case AP_CSVReader::RetCode::TERM_DONE:91EXPECT_STREQ(basic_csv_results[linecount][termcount], (char*)term);92termcount++;93continue;94case AP_CSVReader::RetCode::VECTOR_DONE:95EXPECT_STREQ(basic_csv_results[linecount][termcount], (char*)term);96termcount++;97EXPECT_EQ(termcount, 2);98termcount = 0;99linecount++;100continue;101}102}103104EXPECT_EQ(linecount, 4);105EXPECT_EQ(termcount, 0);106}107108TEST(AP_CSVReader, missinglastcr)109{110static const char *basic_csv =111"A,1"112;113uint8_t term[16];114AP_CSVReader csvreader{term, ARRAY_SIZE(term), ','};115116uint8_t termcount = 0;117uint8_t linecount = 0;118for (uint8_t i=0; i<strlen(basic_csv); i++) {119switch (csvreader.feed(basic_csv[i])) {120case AP_CSVReader::RetCode::ERROR:121abort();122case AP_CSVReader::RetCode::OK:123continue;124case AP_CSVReader::RetCode::TERM_DONE:125if (linecount == 0 && termcount == 0) {126EXPECT_STREQ((char*)term, "A");127}128termcount++;129continue;130case AP_CSVReader::RetCode::VECTOR_DONE:131abort();132}133}134135EXPECT_STREQ((char*)term, "1");136}137138AP_GTEST_MAIN()139140141int hal = 0; // bizarrely, this fixes an undefined-symbol error but doesn't raise a type exception. Yay.142143144