/* StringNDriver.cpp 2015-11-08 - 2018-06-30 Esp. Ing. José María Sola Profesor UTN FRBA */ #include #include #include #include #include "StringN.h" template void PrintSizesAndContents(std::string, String, std::string); int main(){ { // Tests { // array más corto que string std::array a = PackString("Hello, World!"); assert( "Hello" == UnpackString(a) ); } { // String más corto que string String<4> s = PackString("Hello, World!"); assert( "Hell" == UnpackString(s) ); } { // String igual de largo que string String<13> s = PackString("Hello, World!"); assert( "Hello, World!" == UnpackString(s) ); } { // String más largo que string String<42> s = PackString("Hello, World!"); assert( "Hello, World!" == UnpackString(s) ); } } { // Ejemplos using std::array; using std::string; { // String string s{"Hello, World!"}; String<5> a = PackString(s); string t{UnpackString(a)}; assert( t == "Hello" ); } { // array string s{"Hello, World!"}; array a = PackString(s); string t{UnpackString(a)}; assert( t == "Hello"); } { // String más corto que string string s{"Texto de largo mayor a límite de registro."}; String<12> a = PackString(s); // array auto t{UnpackString(a)}; // desempaque todo lo que se pudo guardar assert(s.compare(0, 12, t) == 0); PrintSizesAndContents(s,a,t); } { // String igual de largo que string string s{"abcd"}; // string type String<4> a = PackString(s); // array auto t{UnpackString(a)}; assert(s == t); PrintSizesAndContents(s,a,t); } { // String más largo que string string s{"xyz"}; array a = PackString(s); auto t{UnpackString(a)}; assert(s == t); PrintSizesAndContents(s,a,t); } } } template void PrintString(const String&); template void PrintSizesAndContents(std::string s, String a, std::string t){ std::cout << "s : " << s << "\n" << "sizeof s: " << sizeof s << "\n" << "length s: " << s.length() << "\n" << "a : "; PrintString(a); std::cout << "\n" << "sizeof a: " << sizeof a << "\n" << "t : " << t << "\n" << "sizeof t: " << sizeof t << "\n" << "length t: " << t.length() << "\n\n"; } template void PrintString(const String& s){ for(auto c : s){ if(c=='\0') break; std::cout << c; } }