|
| 1 | +// test103.cpp - set supported datatypes |
| 2 | + |
| 3 | +#include <rapidcsv.h> |
| 4 | +#include "unittest.h" |
| 5 | + |
| 6 | +int main() |
| 7 | +{ |
| 8 | + int rv = 0; |
| 9 | + |
| 10 | + std::string csvref = |
| 11 | + "int,32767\n" |
| 12 | + "long,2147483647\n" |
| 13 | + "long long,9223372036854775807\n" |
| 14 | + "unsigned,65535\n" |
| 15 | + "unsigned long,4294967295\n" |
| 16 | + "unsigned long long,18446744073709551615\n" |
| 17 | + "float,3.40282347e+38\n" |
| 18 | + "double,1.6e+308\n" |
| 19 | + "long double,1.6e+308\n" |
| 20 | + "signed char,-128\n" |
| 21 | + "unsigned char,255\n" |
| 22 | + "short,32767\n" |
| 23 | + "unsigned short,65535\n" |
| 24 | + ; |
| 25 | + |
| 26 | + std::string csv = |
| 27 | + "int,0\n" |
| 28 | + "long,0\n" |
| 29 | + "long long,0\n" |
| 30 | + "unsigned,0\n" |
| 31 | + "unsigned long,0\n" |
| 32 | + "unsigned long long,0\n" |
| 33 | + "float,0\n" |
| 34 | + "double,0\n" |
| 35 | + "long double,0\n" |
| 36 | + "signed char,0\n" |
| 37 | + "unsigned char,0\n" |
| 38 | + "short,0\n" |
| 39 | + "unsigned short,0\n" |
| 40 | + ; |
| 41 | + |
| 42 | + std::string path = unittest::TempPath(); |
| 43 | + unittest::WriteFile(path, csv); |
| 44 | + |
| 45 | + try |
| 46 | + { |
| 47 | + rapidcsv::LabelParams labelParams(-1, -1); |
| 48 | + rapidcsv::SeparatorParams separatorParams(',', false, rapidcsv::sPlatformHasCR, false, false /*pAutoQuote*/); |
| 49 | + rapidcsv::Document doc(path, labelParams, separatorParams); |
| 50 | + |
| 51 | + // set cells |
| 52 | + doc.SetCell<int>(1, 0, 32767); |
| 53 | + doc.SetCell<long>(1, 1, 2147483647); |
| 54 | + doc.SetCell<long long>(1, 2, 9223372036854775807); |
| 55 | + |
| 56 | + doc.SetCell<unsigned>(1, 3, 65535); |
| 57 | + doc.SetCell<unsigned long>(1, 4, 4294967295); |
| 58 | + doc.SetCell<unsigned long long>(1, 5, 18446744073709551615llu); |
| 59 | + |
| 60 | + doc.SetCell<float>(1, 6, 3.40282347e+38); |
| 61 | + doc.SetCell<double>(1, 7, 1.6E308); |
| 62 | + doc.SetCell<long double>(1, 8, 1.6E308); |
| 63 | + |
| 64 | + doc.SetCell<signed char>(1, 9, -128); |
| 65 | + doc.SetCell<unsigned char>(1, 10, 255); |
| 66 | + doc.SetCell<short>(1, 11, 32767); |
| 67 | + doc.SetCell<unsigned short>(1, 12, 65535); |
| 68 | + |
| 69 | + // read back |
| 70 | + unittest::ExpectEqual(int, doc.GetCell<int>(1, 0), 32767); |
| 71 | + unittest::ExpectEqual(long, doc.GetCell<long>(1, 1), 2147483647); |
| 72 | + unittest::ExpectEqual(long long, doc.GetCell<long long>(1, 2), 9223372036854775807); |
| 73 | + |
| 74 | + unittest::ExpectEqual(unsigned, doc.GetCell<unsigned>(1, 3), 65535); |
| 75 | + unittest::ExpectEqual(unsigned long, doc.GetCell<unsigned long>(1, 4), 4294967295); |
| 76 | + unittest::ExpectEqual(unsigned long long, doc.GetCell<unsigned long long>(1, 5), 18446744073709551615llu); |
| 77 | + |
| 78 | + float floatval = doc.GetCell<float>(1, 6); |
| 79 | + unittest::ExpectTrue((floatval > 3.40E38) && (floatval < 3.45E38)); |
| 80 | + |
| 81 | + double doubleval = doc.GetCell<double>(1, 7); |
| 82 | + unittest::ExpectTrue((doubleval > 1.5E308) && (doubleval < 1.7E308)); |
| 83 | + |
| 84 | + long double longdoubleval = doc.GetCell<long double>(1, 8); |
| 85 | + unittest::ExpectTrue((longdoubleval > 1.5E308) && (longdoubleval < 1.7E308)); |
| 86 | + |
| 87 | + unittest::ExpectEqual(signed char, doc.GetCell<signed char>(1, 9), -128); |
| 88 | + unittest::ExpectEqual(unsigned char, doc.GetCell<unsigned char>(1, 10), 255); |
| 89 | + unittest::ExpectEqual(short, doc.GetCell<short>(1, 11), 32767); |
| 90 | + unittest::ExpectEqual(unsigned short, doc.GetCell<unsigned short>(1, 12), 65535); |
| 91 | + |
| 92 | + // check full doc |
| 93 | + doc.Save(); |
| 94 | + std::string csvread = unittest::ReadFile(path); |
| 95 | + unittest::ExpectEqual(std::string, csvref, csvread); |
| 96 | + } |
| 97 | + catch (const std::exception& ex) |
| 98 | + { |
| 99 | + std::cout << ex.what() << std::endl; |
| 100 | + rv = 1; |
| 101 | + } |
| 102 | + |
| 103 | + unittest::DeleteFile(path); |
| 104 | + |
| 105 | + return rv; |
| 106 | +} |
0 commit comments