|
| 1 | +// test104.cpp - narrow integer type range checking |
| 2 | + |
| 3 | +#include <rapidcsv.h> |
| 4 | +#include "unittest.h" |
| 5 | + |
| 6 | +int main() |
| 7 | +{ |
| 8 | + int rv = 0; |
| 9 | + |
| 10 | + std::string csv = |
| 11 | + "col\n" |
| 12 | + "127\n" |
| 13 | + "128\n" |
| 14 | + "-128\n" |
| 15 | + "-129\n" |
| 16 | + "255\n" |
| 17 | + "256\n" |
| 18 | + "32767\n" |
| 19 | + "32768\n" |
| 20 | + "65535\n" |
| 21 | + "65536\n" |
| 22 | + ; |
| 23 | + |
| 24 | + std::string path = unittest::TempPath(); |
| 25 | + unittest::WriteFile(path, csv); |
| 26 | + |
| 27 | + try |
| 28 | + { |
| 29 | + rapidcsv::Document doc(path); |
| 30 | + |
| 31 | + // signed char: valid range [-128, 127] |
| 32 | + unittest::ExpectEqual(int, static_cast<int>(doc.GetCell<signed char>(0, 0)), 127); |
| 33 | + ExpectException(doc.GetCell<signed char>(0, 1), std::out_of_range); |
| 34 | + unittest::ExpectEqual(int, static_cast<int>(doc.GetCell<signed char>(0, 2)), -128); |
| 35 | + ExpectException(doc.GetCell<signed char>(0, 3), std::out_of_range); |
| 36 | + |
| 37 | + // unsigned char: valid range [0, 255] |
| 38 | + unittest::ExpectEqual(int, static_cast<int>(doc.GetCell<unsigned char>(0, 4)), 255); |
| 39 | + ExpectException(doc.GetCell<unsigned char>(0, 5), std::out_of_range); |
| 40 | + |
| 41 | + // short: valid range [-32768, 32767] |
| 42 | + unittest::ExpectEqual(int, static_cast<int>(doc.GetCell<short>(0, 6)), 32767); |
| 43 | + ExpectException(doc.GetCell<short>(0, 7), std::out_of_range); |
| 44 | + |
| 45 | + // unsigned short: valid range [0, 65535] |
| 46 | + unittest::ExpectEqual(int, static_cast<int>(doc.GetCell<unsigned short>(0, 8)), 65535); |
| 47 | + ExpectException(doc.GetCell<unsigned short>(0, 9), std::out_of_range); |
| 48 | + } |
| 49 | + catch (const std::exception& ex) |
| 50 | + { |
| 51 | + std::cout << ex.what() << std::endl; |
| 52 | + rv = 1; |
| 53 | + } |
| 54 | + |
| 55 | + unittest::DeleteFile(path); |
| 56 | + |
| 57 | + return rv; |
| 58 | +} |
0 commit comments