testStrings.cc
// -*- C++ -*-
// $Id: testStrings.cc,v 1.10 1998/12/05 18:31:21 evc Exp $
//
// This file is a part of the CLHEP - a Class Library for High Energy Physics.
//
// This is a small program for testing the HepString class.
//
#include "CLHEP/String/Strings.h"
#include "CLHEP/config/TemplateFunctions.h"
#include "CLHEP/config/strstream.h"
int main () {
// testing constructors:
HepString s1; if ( s1 != "" || s1.length() != 0 ) exit(1);
HepString s2('t'); if ( s2 != "t" || s2.length() != 1 ) exit(2);
HepString s3('\0'); if ( s3 != "" || s3.length() != 0 ) exit(2);
HepString s4("hello"); if ( s4 != "hello" || s4.length() != 5 ) exit(3);
HepString s5(s4); if ( s5 != "hello" || s5.length() != 5 ) exit(4);
HepString s6(-12345); if ( s6 != "-12345" || s6.length() != 6) exit(5);
#ifndef __GNUG__
// libg++ 2.7.2 has a memory leak. Which means these tests will not pass
HepString s7(12345.6789); if ( s7 != "12345.7" || s7.length() != 7 ) exit(6);
HepString s8(12345.6789, 20); if ( s8(0,10) != "12345.6789" ) exit(6);
HepString s9(-12345.6789, 3); if ( s9.lower() != "-1.23e+04" ) exit(6);
HepString s0(123.456e+20); if ( s0.upper() != "1.23456E+22" ) exit(6);
#else
HepString s7(12345.7);
HepString s8(12345.6789);
HepString s9(-12300.0);
HepString s0(123.456e+20);
#endif
// testing assignment:
s1 = s3; if ( s1 != "" || s1.length() != 0 ) exit(8);
s1 = s4; if ( s1 != "hello" || s1.length() != 5 ) exit(8);
s1 = "hello "; if ( s1 != "hello " || s1.length() != 6 ) exit(9);
s1 = 't'; if ( s1 != 't' ) exit(10); if ( s1.length() != 1 ) exit(10);
s1 += s3; if ( s1 != 't' ) exit(11); if ( s1.length() != 1 ) exit(11);
s1 += s4; if ( s1 != "thello" || s1.length() != 6 ) exit(11);
s1 += " hello"; if (s1 != "thello hello" || s1.length() != 12) exit(13);
s1 += '\0'; if (s1 != "thello hello" || s1.length() != 12) exit(14);
s1 += 't'; if (s1 != "thello hellot" || s1.length() != 13) exit(14);
// testing concatenation:
s1 = s2 + s3; if ( s1 != 't' ) exit(15); if ( s1.length() != 1 ) exit(15);
s1 = s3 + s4; if ( s1 != "hello" || s1.length() != 5 ) exit(15);
s1 = s4 + s5; if ( s1 != "hellohello" || s1.length() != 10 ) exit(15);
s1 = s3 + "hello"; if ( s1 != "hello" || s1.length() != 5 ) exit(16);
s1 = s4 + "hello"; if ( s1 != "hellohello" || s1.length() != 10 ) exit(16);
s1 = "hello" + s4; if ( s1 != "hellohello" || s1.length() != 10 ) exit(16);
s1 = s3 + '\0'; if ( s1 != "" || s1.length() != 0 ) exit(17);
s1 = s3 + 't'; if ( s1 != 't' ) exit(17); if ( s1.length() != 1 ) exit(17);
s1 = s4 + 't'; if ( s1 != "hellot" || s1.length() != 6 ) exit(17);
s1 = "hello" + s2 + 't' + s4; if ( s1 != "hellotthello" ) exit(17);
// testing subscripting:
if ( s4(0) != 'h' || s4(2) != 'l' || s4(4) != 'o' || s4(10) != '\0' )
exit(18);
if ( s4(0,5) != "hello" ) exit(19); if ( s4(0,10) != "hello" ) exit(19);
if ( s4(3,5) != "lo" ) exit(19); if ( s4(10,3) != "" ) exit(19);
if ( s4(2,2) != "ll" ) exit(19);
// testing cast (const char *):
const char * cp = s4; if ( strcmp(cp, "hello") ) exit(20);
// testing case convertion:
s1 = "helloHELLOhello"; if ( s1.lower() != "hellohellohello" ) exit(22);
s1 = "helloHELLOhello"; if ( s1.upper() != "HELLOHELLOHELLO" ) exit(23);
// testing number conversion;
if ( s6.toInt() != -12345 ) exit(24);
if ( s6.toFloat() != double(-12345) ) exit(25);
if ( abs(12345.7-s7.toFloat()) > 0.0001 ) exit(25);
if ( abs(1.23e4+s9.toFloat()) > 100.0 ) exit(25);
// testing boolean operators;
if ( !( s4 == s5 ) ) exit(26);
if ( !( s4 == "hello" ) ) exit(27);
if ( !( "hello" == s4 ) ) exit(27);
if ( "hello" != s4 ) exit(27);
// testing index:
s1 = s4 + s4;
if ( s1.index('h') != 0 || s1.index('l') != 2 || s1.index('o') != 4 )
exit(28);
if ( s1.lIndex('h') != 5 ) exit(28);
if ( s1.lIndex('l') != 8 || s1.lIndex('o') != 9 )
exit(28);
if ( s1.index("he") != 0 ) exit(29); if ( s1.index("ll") != 2 ) exit(29);
if ( s1.index("lo") != 3 ) exit(29);
// testing stream i/o:
std::istrstream is("howdy partner! \n 1.23 45 bright 45");
is >> s1 >> s2 >> s3 >> s4 >> s5 >> s6 >> s7;
if ( s1 != "howdy" || s2 != "partner!" || s6.toInt() != 45 || s7 != "" )
exit(30);
std::ostrstream os;
os << s1 << '#' << s2 << '#' << s3.toFloat() << '#' << s4 << '#'
<< s5 << '#' << s6.toInt() << '#' << s7 << '#' << '\n';
os.put('\0');
if (strcmp(os.str(), "howdy#partner!#1.23#45#bright#45##\n") ) exit(31);
// testing car and cdr
s1 = "This is\ta test";
if ( s1.car() != "This" ) exit(32);
if ( s1.cdr().car() != "is" ) exit(32);
if ( s1.cdr() != "is\ta test" ) exit(32);
if ( !s1 || s1.isEmpty() ) exit(33);
HepString nullString;
if ( (!nullString) == false ) exit(33);
if ( !(nullString.isEmpty()) ) exit(33);
return 0;
}
Generated by GNU enscript 1.6.1.