diff options
Diffstat (limited to 'src/tests')
| -rw-r--r-- | src/tests/optparser.cpp | 27 | ||||
| -rw-r--r-- | src/tests/signals.cpp | 87 | ||||
| -rw-r--r-- | src/tests/socket.cpp | 66 |
3 files changed, 180 insertions, 0 deletions
diff --git a/src/tests/optparser.cpp b/src/tests/optparser.cpp new file mode 100644 index 0000000..9168af8 --- /dev/null +++ b/src/tests/optparser.cpp | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | #include <bu/optparser.h> | ||
| 2 | #include <bu/sio.h> | ||
| 3 | using namespace Bu; | ||
| 4 | |||
| 5 | class Opts : public Bu::OptParser | ||
| 6 | { | ||
| 7 | public: | ||
| 8 | Opts() : | ||
| 9 | iBob( 542 ) | ||
| 10 | { | ||
| 11 | callback( this, &Opts::cb ); | ||
| 12 | } | ||
| 13 | |||
| 14 | int cb( int argc, char *argv[] ) | ||
| 15 | { | ||
| 16 | sio << "Hey, cb was called, here's a class var: " << iBob << sio.nl; | ||
| 17 | return 5; | ||
| 18 | } | ||
| 19 | |||
| 20 | int iBob; | ||
| 21 | }; | ||
| 22 | |||
| 23 | int main( int argc, char *argv[] ) | ||
| 24 | { | ||
| 25 | Opts o; | ||
| 26 | } | ||
| 27 | |||
diff --git a/src/tests/signals.cpp b/src/tests/signals.cpp new file mode 100644 index 0000000..b4b1363 --- /dev/null +++ b/src/tests/signals.cpp | |||
| @@ -0,0 +1,87 @@ | |||
| 1 | #include <bu/signals.h> | ||
| 2 | #include <bu/sio.h> | ||
| 3 | |||
| 4 | using namespace Bu; | ||
| 5 | |||
| 6 | class Thing | ||
| 7 | { | ||
| 8 | public: | ||
| 9 | Thing() : | ||
| 10 | iState( 82731 ) | ||
| 11 | { | ||
| 12 | } | ||
| 13 | |||
| 14 | virtual ~Thing() | ||
| 15 | { | ||
| 16 | } | ||
| 17 | |||
| 18 | void fnc0() | ||
| 19 | { | ||
| 20 | sio << iState << ": void fnc0()" << sio.nl; | ||
| 21 | } | ||
| 22 | |||
| 23 | void fnc1( int a ) | ||
| 24 | { | ||
| 25 | sio << iState << ": void fnc1( " << a << " )" << sio.nl; | ||
| 26 | } | ||
| 27 | |||
| 28 | void fnc2( int a, Bu::FString b ) | ||
| 29 | { | ||
| 30 | sio << iState << ": void fnc2( " << a << ", \"" << b << "\" )" << sio.nl; | ||
| 31 | } | ||
| 32 | |||
| 33 | void fnc3( int a, Bu::FString b, double c ) | ||
| 34 | { | ||
| 35 | sio << iState << ": void fnc3( " << a << ", \"" << b << "\", " << c << " )" << sio.nl; | ||
| 36 | } | ||
| 37 | |||
| 38 | void fnc4( int a, Bu::FString b, double c, char d ) | ||
| 39 | { | ||
| 40 | sio << iState << ": void fnc4( " << a << ", \"" << b << "\", " << c << ", '" << d << "' )" << sio.nl; | ||
| 41 | } | ||
| 42 | |||
| 43 | void fnc5( int a, Bu::FString b, double c, char d, long e ) | ||
| 44 | { | ||
| 45 | sio << iState << ": void fnc5( " << a << ", \"" << b << "\", " << c << ", '" << d << "', " << e << " )" << sio.nl; | ||
| 46 | } | ||
| 47 | |||
| 48 | private: | ||
| 49 | int iState; | ||
| 50 | }; | ||
| 51 | |||
| 52 | void pfnc0() | ||
| 53 | { | ||
| 54 | sio << "This doesn't have state, it's pfnc0()!" << sio.nl; | ||
| 55 | } | ||
| 56 | |||
| 57 | void callit( Signal0<void> sig ) | ||
| 58 | { | ||
| 59 | sig(); | ||
| 60 | } | ||
| 61 | |||
| 62 | int main() | ||
| 63 | { | ||
| 64 | Thing t; | ||
| 65 | |||
| 66 | Signal0<void> cb0( slot( &t, &Thing::fnc0 ) ); | ||
| 67 | cb0(); | ||
| 68 | |||
| 69 | Signal1<void, int> cb1( slot( &t, &Thing::fnc1 ) ); | ||
| 70 | cb1( 5 ); | ||
| 71 | |||
| 72 | Signal2<void, int, Bu::FString> cb2( slot( &t, &Thing::fnc2 ) ); | ||
| 73 | cb2( 5, "Hi there" ); | ||
| 74 | |||
| 75 | Signal3<void, int, Bu::FString, double> cb3( slot( &t, &Thing::fnc3 ) ); | ||
| 76 | cb3( 5, "Hi there", 12.85 ); | ||
| 77 | |||
| 78 | Signal4<void, int, Bu::FString, double, char> cb4( slot( &t, &Thing::fnc4 ) ); | ||
| 79 | cb4( 5, "Hi there", 12.85, 'z' ); | ||
| 80 | |||
| 81 | Signal5<void, int, Bu::FString, double, char, long> cb5( slot( &t, &Thing::fnc5 ) ); | ||
| 82 | cb5( 5, "Hi there", 12.85, 'z', 849 ); | ||
| 83 | |||
| 84 | // Signal1<int, int> cb1( slot( &t, &Thing::fnc1 ) ); | ||
| 85 | // sio << "Result: " << cb1( 5 ) << sio.nl; | ||
| 86 | } | ||
| 87 | |||
diff --git a/src/tests/socket.cpp b/src/tests/socket.cpp new file mode 100644 index 0000000..1cf63e4 --- /dev/null +++ b/src/tests/socket.cpp | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | #include <bu/socket.h> | ||
| 2 | #include <bu/sio.h> | ||
| 3 | |||
| 4 | #include <sys/time.h> | ||
| 5 | #include <time.h> | ||
| 6 | |||
| 7 | using namespace Bu; | ||
| 8 | |||
| 9 | bool isUp() | ||
| 10 | { | ||
| 11 | try | ||
| 12 | { | ||
| 13 | Socket s("xagasoft.com", 9898, 1 ); | ||
| 14 | |||
| 15 | char buf[5]; | ||
| 16 | buf[s.read(buf, 2, 1, 0)] = '\0'; | ||
| 17 | |||
| 18 | if( !strcmp( buf, "hi" ) ) | ||
| 19 | return true; | ||
| 20 | else | ||
| 21 | return false; | ||
| 22 | } | ||
| 23 | catch(...) | ||
| 24 | { | ||
| 25 | return false; | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | int main() | ||
| 30 | { | ||
| 31 | uint32_t uUp = 0; | ||
| 32 | uint32_t uDown = 0; | ||
| 33 | uint32_t uTotal = 0; | ||
| 34 | struct timeval tv; | ||
| 35 | |||
| 36 | for(;;) | ||
| 37 | { | ||
| 38 | gettimeofday( &tv, NULL ); | ||
| 39 | time_t goal = ((tv.tv_sec/5)+1)*5; | ||
| 40 | tv.tv_sec = goal-tv.tv_sec; | ||
| 41 | tv.tv_usec = 0-tv.tv_usec; | ||
| 42 | if( tv.tv_usec < 0 ) | ||
| 43 | { | ||
| 44 | tv.tv_sec--; | ||
| 45 | tv.tv_usec = 1000000+tv.tv_usec; | ||
| 46 | } | ||
| 47 | select( 0, NULL, NULL, NULL, &tv ); | ||
| 48 | gettimeofday( &tv, NULL ); | ||
| 49 | if( isUp() ) | ||
| 50 | { | ||
| 51 | uUp++; | ||
| 52 | sio << "status: up "; | ||
| 53 | } | ||
| 54 | else | ||
| 55 | { | ||
| 56 | uDown++; | ||
| 57 | sio << "status: down "; | ||
| 58 | } | ||
| 59 | uTotal++; | ||
| 60 | |||
| 61 | sio << "(up=" << (uUp*5) << "s, down=" << (uDown*5) << ") up for " | ||
| 62 | << uUp*100/uTotal << "% of " << uTotal*5 << "s" << sio.nl | ||
| 63 | << sio.flush; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
