// test suite for checked_array.hpp // (c) 2006 David Manura, Licensed under the MIT license. #include "checked_array.hpp" using namespace std; template string get_type(T& t) { return info::debug_type(); } int main() { { char s[10]; assert( get_type(s) == "char [10]" ); const char s2[10] = "ok"; assert( get_type(s2) == "const char [10]"); char * s3 = "asdfghjkl"; assert( get_type(s3) == "T *" ); const char * s4 = "asdfghjkl"; assert( get_type(s4) == "T *" ); } { char_array<6> s1("abc", false); char_array<4> s2(s1); char_array<3> s3("zz", false); //s1.maxlength(5); strcat_t(s1, s3); cout << s1.c_str() << " " << s2.c_str() << endl; char s4[3] = "a"; strcpy_t(s1, s4); //strcat_t(s1, s4); cout << s1.c_str() << " " << s2.c_str() << endl; } { // test: char_array construct from char[] cout << "t6\n"; char s[3] = "z"; char_array<4> s2(s); assert( s2.m_capacity == 4 ); assert( s2.length() == 2 ); assert( s2.maxlength() == 3 ); } { // test: char_array construct from const char[] cout << "t6c\n"; const char s[3] = "z"; char_array<4> s2(s); assert( s2.m_capacity == 4 ); assert( s2.length() == 2 ); assert( s2.maxlength() == 3 ); strcpy_t(s2, "a"); // ok } { // test: char_array construct from char* cout << "t7\n"; char * s = "z"; char_array<4> s2(s); assert( s2.m_capacity == 4 ); assert( s2.length() == 2 ); assert( s2.maxlength() == 4 ); } { // test: char_array construct from const char* cout << "t7c\n"; const char * s = "z"; char_array<4> s2(s); assert( s2.m_capacity == 4 ); assert( s2.length() == 2 ); assert( s2.maxlength() == 4 ); strcpy_t(s2, "a"); //ok } { // test: const char_array cout << "t7c2\n"; const char_array<4> s2("ab"); // compiler error:strcpy_t(s2, "a"); } { // test: check_array with non-char type int s[3]; //FIX:not yet implemented: checked_array s2(s); } { // test: array_ref construct from char[] cout << "t6b\n"; char s[4] = "z"; char_array_ref<3> s2(s); // note: smaller assert( s2.m_capacity == 3 ); assert( s2.length() == 2 ); assert( s2.maxlength() == 3 ); } { // test: array_ref construct from const char[] cout << "t6b\n"; const char s[4] = "z"; char_array_ref<3,const char> s2(s); assert( s2.m_capacity == 3 ); assert( s2.length() == 2 ); assert( s2.maxlength() == 3 ); //compile error: strcpy_t(s2, "a"); } { // test: array_ref construct from char * cout << "t7b\n"; char * s = "z"; char_array_ref<4> s2(s); assert( s2.m_capacity == 4 ); assert( s2.length() == 2 ); assert( s2.maxlength() == 4 ); } { // test: array_ref construct from const char* cout << "t7b\n"; const char * s = "z"; char_array_ref<4,const char> s2(s); assert( s2.m_capacity == 4 ); assert( s2.length() == 2 ); assert( s2.maxlength() == 4 ); // compiler error: strcpy_t(s2, "a"); } { // test: array_ref copy contructor cout << "t7b2\n"; char s[5] = "z"; char_array_ref<4> s2(s); char_array_ref<3> s3(s); assert( s3.m_capacity == 3 ); assert( s3.length() == 2 ); assert( s3.maxlength() == 3 ); } { // test: char_array_ref_cast cout << "t8\n"; assert( char_array_ref_cast("").capacity() == 1 ); assert( char_array_ref_cast("").length() == 1 ); assert( char_array_ref_cast("").maxlength() == 1 ); assert( char_array_ref_cast("", false).maxlength() == 1 ); assert( char_array_ref_cast("1").capacity() == 2 ); assert( char_array_ref_cast("1").length() == 2 ); assert( char_array_ref_cast("1").maxlength() == 2 ); assert( char_array_ref_cast("1", false).maxlength() == 2 ); //char_array_ref_cast(""); // not compile } { // test: char_array_ref_cast (#2) cout << "t9\n"; char s[3] = "1"; assert( char_array_ref_cast(s).capacity() == 3 ); assert( char_array_ref_cast(s).length() == 2 ); assert( char_array_ref_cast(s).maxlength() == 3 ); assert( char_array_ref_cast(s, false).maxlength() == 2 ); assert( char_array_ref_cast(s).capacity() == 3 ); assert( char_array_ref_cast(s).length() == 2 ); assert( char_array_ref_cast(s).maxlength() == 3 ); assert( char_array_ref_cast(s,false).maxlength() == 2 ); } { cout << "t10\n"; char s[3] = "a"; //not allowed: strcat_t(char_array_ref_cast(s), "d"); char_array_ref<3> s2(s, false); strcat_t(s2, "d"); cout << s << endl; // strcat_t(s, "e"); } return 0; }