From 24f9b937a6e06f4eeb9151d753553cb2e6b07037 Mon Sep 17 00:00:00 2001 From: kevin Date: Wed, 29 May 2024 12:37:53 -0400 Subject: [PATCH] cwFileSys,cwLex,cwVectOps,cwObject,cwTime : Updated to use cwTest to manage test() function. --- Makefile.am | 8 +-- cwFileSys.cpp | 36 +++++++++++- cwFileSys.h | 2 + cwFlowTest.h | 2 +- cwLex.cpp | 5 +- cwLex.h | 2 +- cwObject.cpp | 155 +++++++++++++++++++++++++++++++++++++++++++------- cwObject.h | 2 + cwTime.cpp | 15 ++--- cwTime.h | 2 +- cwVectOps.cpp | 15 +++++ cwVectOps.h | 13 ++--- 12 files changed, 213 insertions(+), 44 deletions(-) create mode 100644 cwVectOps.cpp diff --git a/Makefile.am b/Makefile.am index 5fbc7e0..82f0f8d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -4,8 +4,8 @@ libcwSRC = libcwHDR += src/libcw/cwCommon.h src/libcw/cwCommonImpl.h src/libcw/cwMem.h src/libcw/cwLog.h src/libcw/cwUtility.h libcwSRC += src/libcw/cwCommonImpl.cpp src/libcw/cwMem.cpp src/libcw/cwLog.cpp src/libcw/cwUtility.cpp -libcwHDR += src/libcw/cwString.h src/libcw/cwMath.h src/libcw/cwVectOps.h src/libcw/cwMtx.h src/libcw/cwVariant.h -libcwSRC += src/libcw/cwString.cpp src/libcw/cwMath.cpp src/libcw/cwMtx.cpp src/libcw/cwVariant.cpp +libcwHDR += src/libcw/cwString.h src/libcw/cwMath.h src/libcw/cwVectOps.h src/libcw/cwMtx.h src/libcw/cwVariant.h +libcwSRC += src/libcw/cwString.cpp src/libcw/cwMath.cpp src/libcw/cwVectOps.cpp src/libcw/cwMtx.cpp src/libcw/cwVariant.cpp libcwHDR += src/libcw/cwB23Tree.h libcwSRC += src/libcw/cwB23Tree.cpp @@ -46,8 +46,8 @@ libcwSRC += src/libcw/cwUi.cpp src/libcw/cwUiTest.cpp endif -libcwHDR += src/libcw/cwKeyboard.h -libcwSRC += src/libcw/cwKeyboard.cpp +libcwHDR += src/libcw/cwKeyboard.h src/libcw/cwTest.h +libcwSRC += src/libcw/cwKeyboard.cpp src/libcw/cwTest.cpp libcwHDR += src/libcw/cwSerialPortDecls.h src/libcw/cwSerialPort.h src/libcw/cwSerialPortSrv.h libcwSRC += src/libcw/cwSerialPort.cpp src/libcw/cwSerialPortSrv.cpp diff --git a/cwFileSys.cpp b/cwFileSys.cpp index 6661082..9574f50 100644 --- a/cwFileSys.cpp +++ b/cwFileSys.cpp @@ -1,8 +1,12 @@ #include "cwCommon.h" #include "cwLog.h" +#include "cwCommonImpl.h" + +#include "cwTest.h" +#include "cwObject.h" #include "cwFileSys.h" -#include "cwCommonImpl.h" + #include "cwMem.h" #include "cwString.h" #include "cwText.h" @@ -893,3 +897,33 @@ cw::rc_t cw::filesys::makeDir( const char* dirStr ) return kOkRC; } + + +cw::rc_t cw::filesys::test( const test::test_args_t& args ) +{ + rc_t rc = kOkRC; + + filesys::pathPart_t* pp = filesys::pathParts(__FILE__); + + cwLogPrint("dir:%s",pp->dirStr); + cwLogPrint("fn: %s",pp->fnStr); + cwLogPrint("ext:%s",pp->extStr); + + char* fn = filesys::makeFn( pp->dirStr, pp->fnStr, pp->extStr, nullptr ); + + cwLogPrint("fn: %s",fn); + + mem::release(pp); + mem::release(fn); + + + const char myPath[] = "~/src/foo"; + + char* expPath = filesys::expandPath(myPath); + + cwLogPrint("%s %s",myPath,expPath); + + mem::release(expPath); + + return rc; +} diff --git a/cwFileSys.h b/cwFileSys.h index ae2924c..d55af8c 100644 --- a/cwFileSys.h +++ b/cwFileSys.h @@ -90,6 +90,8 @@ namespace cw char* makeVersionedDirectory(const char* recordDir, const char* recordFolder ); rc_t makeDir( const char* dirStr ); + + rc_t test( const test::test_args_t& args ); } } diff --git a/cwFlowTest.h b/cwFlowTest.h index 51c40bb..5fb828a 100644 --- a/cwFlowTest.h +++ b/cwFlowTest.h @@ -4,6 +4,6 @@ namespace cw namespace flow { - rc_t test( const object_t* cfg, int argc, const char* argv[] ); + rc_t test( const test::test_args_t& args ); } } diff --git a/cwLex.cpp b/cwLex.cpp index da73cc2..91144f2 100644 --- a/cwLex.cpp +++ b/cwLex.cpp @@ -3,7 +3,8 @@ #include "cwCommonImpl.h" #include "cwMem.h" #include "cwFile.h" - +#include "cwTest.h" +#include "cwObject.h" #include "cwLex.h" @@ -853,7 +854,7 @@ namespace cw //) //( - rc_t test() + rc_t test( const test::test_args_t& args ) { rc_t rc = kOkRC; unsigned tid = kInvalidId; diff --git a/cwLex.h b/cwLex.h index c77aafd..9e5a72f 100644 --- a/cwLex.h +++ b/cwLex.h @@ -129,7 +129,7 @@ namespace cw const char* idToLabel( handle_t h, unsigned typeId ); // Lexer testing stub. - rc_t test( ); + rc_t test( const test::test_args_t& args ); } } diff --git a/cwObject.cpp b/cwObject.cpp index 60682f8..f5382db 100644 --- a/cwObject.cpp +++ b/cwObject.cpp @@ -5,6 +5,7 @@ #include "cwCommonImpl.h" #include "cwMem.h" #include "cwFile.h" +#include "cwTest.h" #include "cwLex.h" #include "cwText.h" #include "cwNumericConvert.h" @@ -152,37 +153,37 @@ namespace cw void _objTypePrintIndent( const char* text, unsigned indent, const char* indentStr=" " ) { for(unsigned i=0; itype->print(o,c); - printf("%s",eolStr); + cwLogPrint("%s",eolStr); } - void _objTypePrintNull( const object_t* o, print_ctx_t& c ) { printf("NULL "); } - void _objTypePrintError( const object_t* o, print_ctx_t& c ) { printf("Error "); } - void _objTypePrintChar( const object_t* o, print_ctx_t& c ) { printf("%c",o->u.c); } - void _objTypePrintInt8( const object_t* o, print_ctx_t& c ) { printf("%i",o->u.i8); } - void _objTypePrintUInt8( const object_t* o, print_ctx_t& c ) { printf("%i",o->u.u8); } - void _objTypePrintInt16( const object_t* o, print_ctx_t& c ) { printf("%i",o->u.i16); } - void _objTypePrintUInt16( const object_t* o, print_ctx_t& c ) { printf("%i",o->u.u16); } - void _objTypePrintInt32( const object_t* o, print_ctx_t& c ) { printf("%i",o->u.i32); } - void _objTypePrintUInt32( const object_t* o, print_ctx_t& c ) { printf("%i",o->u.u32); } - void _objTypePrintInt64( const object_t* o, print_ctx_t& c ) { printf("%lli", o->u.i64); } - void _objTypePrintUInt64( const object_t* o, print_ctx_t& c ) { printf("%lli", o->u.u64); } - void _objTypePrintBool( const object_t* o, print_ctx_t& c ) { printf("%s",o->u.b ? "true" : "false"); } - void _objTypePrintFloat( const object_t* o, print_ctx_t& c ) { printf("%f",o->u.f); } - void _objTypePrintDouble( const object_t* o, print_ctx_t& c ) { printf("%f",o->u.d); } - void _objTypePrintString( const object_t* o, print_ctx_t& c ) { printf("%s",o->u.str); } - void _objTypePrintVect( const object_t* o, print_ctx_t& c ) { printf(""); } + void _objTypePrintNull( const object_t* o, print_ctx_t& c ) { cwLogPrint("NULL "); } + void _objTypePrintError( const object_t* o, print_ctx_t& c ) { cwLogPrint("Error "); } + void _objTypePrintChar( const object_t* o, print_ctx_t& c ) { cwLogPrint("%c",o->u.c); } + void _objTypePrintInt8( const object_t* o, print_ctx_t& c ) { cwLogPrint("%i",o->u.i8); } + void _objTypePrintUInt8( const object_t* o, print_ctx_t& c ) { cwLogPrint("%i",o->u.u8); } + void _objTypePrintInt16( const object_t* o, print_ctx_t& c ) { cwLogPrint("%i",o->u.i16); } + void _objTypePrintUInt16( const object_t* o, print_ctx_t& c ) { cwLogPrint("%i",o->u.u16); } + void _objTypePrintInt32( const object_t* o, print_ctx_t& c ) { cwLogPrint("%i",o->u.i32); } + void _objTypePrintUInt32( const object_t* o, print_ctx_t& c ) { cwLogPrint("%i",o->u.u32); } + void _objTypePrintInt64( const object_t* o, print_ctx_t& c ) { cwLogPrint("%lli", o->u.i64); } + void _objTypePrintUInt64( const object_t* o, print_ctx_t& c ) { cwLogPrint("%lli", o->u.u64); } + void _objTypePrintBool( const object_t* o, print_ctx_t& c ) { cwLogPrint("%s",o->u.b ? "true" : "false"); } + void _objTypePrintFloat( const object_t* o, print_ctx_t& c ) { cwLogPrint("%f",o->u.f); } + void _objTypePrintDouble( const object_t* o, print_ctx_t& c ) { cwLogPrint("%f",o->u.d); } + void _objTypePrintString( const object_t* o, print_ctx_t& c ) { cwLogPrint("%s",o->u.str); } + void _objTypePrintVect( const object_t* o, print_ctx_t& c ) { cwLogPrint(""); } void _objTypePrintPair( const object_t* o, print_ctx_t& c ) { o->u.children->type->print(o->u.children,c); - printf(": "); + cwLogPrint(": "); o->u.children->sibling->type->print(o->u.children->sibling,c); } @@ -1013,4 +1014,118 @@ cw::rc_t cw::objectToFile( const char* fn, const object_t* obj ) return rc; } +namespace cw +{ + rc_t _object_test_basic( const test::test_args_t& args ) + { + rc_t rc = kOkRC; + cw::object_t* o = nullptr; + const char s [] = "{ a:1, b:2, c:[ 1.23, 4.56 ], d:true, e:false, f:true }"; + + int a = 0; + int b = 0; + const cw::object_t* c = nullptr; + bool d,e,f; + + const unsigned bufN = 128; + char buf[bufN]; + + unsigned i = 0; + + cw::object_t* oo = nullptr; + + if((rc = cw::objectFromString(s,o)) != kOkRC ) + goto errLabel; + + int v; + if((rc = o->get("b",v)) != kOkRC ) + goto errLabel; + + cwLogPrint("value:%i\n",v); + + o->print(); + + + if((rc = o->getv("a",a,"b",b)) != kOkRC ) + goto errLabel; + + cwLogPrint("G: %i %i\n",a,b); + + + if((rc = o->readv("a",0,a, + "b",0,b, + "c",cw::kOptFl | cw::kListTId,c, + "d",0,d, + "e",0,e, + "f",0,f)) != kOkRC ) + { + goto errLabel; + } + + cwLogPrint("R: %i %i : %i %i %i\n",a,b,d,e,f); + + + i = o->to_string(buf,bufN); + cwLogPrint("%i : %s\n",i, buf); + + oo = o->duplicate(); + + oo->print(); + + oo->free(); + + + o->free(); + + errLabel: + return rc; + } + + rc_t _object_test_to_json( const test::test_args_t& args ) + { + double v0[] = {1.23,2.34,3.45}; + unsigned v0N = sizeof(v0)/sizeof(v0[0]); + int v1[] = {-1,0,1,2,3,4}; + unsigned v1N = sizeof(v1)/sizeof(v1[0]); + + cw::object_t* d = cw::newDictObject(); + + d->putv("A","Abc","B",1.234); + d->put_numeric_list("v0",v0,v0N); + d->put_numeric_list("v1",v1,v1N); + + char* s = d->to_string(); + cwLogPrint("%s\n",s); + cw::mem::release(s); + + d->free(); + + return kOkRC; + + } + +} + + +cw::rc_t cw::object_test( const test::test_args_t& args ) +{ + rc_t rc = kOkRC; + + if( textIsEqual(args.test_label,"basic") ) + { + rc = _object_test_basic(args); + goto errLabel; + } + + if( textIsEqual(args.test_label,"to_json") ) + { + rc = _object_test_to_json(args); + goto errLabel; + } + + rc = cwLogError(kInvalidArgRC,"Unknown test case module:%s test:%s.",args.module_label,args.test_label); + +errLabel: + return cw::kOkRC; +} diff --git a/cwObject.h b/cwObject.h index ed1a6ad..3b937fa 100644 --- a/cwObject.h +++ b/cwObject.h @@ -460,6 +460,8 @@ namespace cw rc_t objectToFile( const char* fn, const object_t* obj ); + rc_t object_test( const test::test_args_t& args ); + } diff --git a/cwTime.cpp b/cwTime.cpp index ef7f211..7fd5658 100644 --- a/cwTime.cpp +++ b/cwTime.cpp @@ -1,6 +1,7 @@ #include "cwCommon.h" #include "cwLog.h" #include "cwCommonImpl.h" +#include "cwTest.h" #include "cwTime.h" #ifdef OS_OSX @@ -399,7 +400,7 @@ unsigned cw::time::formatDateTime( char* buffer, unsigned bufN, bool includeDate return (unsigned)n; } -cw::rc_t cw::time::test() +cw::rc_t cw::time::test(const test::test_args_t& test ) { spec_t t0,t1; @@ -410,15 +411,15 @@ cw::rc_t cw::time::test() unsigned dMs = elapsedMs(t0,t1); - printf("dMs:%i : GTE:%i LTE:%i\n",dMs, isGTE(t0,t1), isLTE(t0,t1) ); + cwLogPrint("dMs:%i : GTE:%i LTE:%i\n",dMs, isGTE(t0,t1), isLTE(t0,t1) ); microsecondsToSpec( t0, 2500000 ); // 2.5 seconds - printf("%li %li\n",t0.tv_sec,t0.tv_nsec); + cwLogPrint("%li %li\n",t0.tv_sec,t0.tv_nsec); subtractMicros( t0, 750000 ); // subtract .75 seconds - printf("%li %li\n",t0.tv_sec,t0.tv_nsec); + cwLogPrint("%li %li\n",t0.tv_sec,t0.tv_nsec); subtractMicros( t0, 500000 ); // subtract .5 seconds - printf("%li %li\n",t0.tv_sec,t0.tv_nsec); + cwLogPrint("%li %li\n",t0.tv_sec,t0.tv_nsec); time::get(t0); @@ -429,11 +430,11 @@ cw::rc_t cw::time::test() int usec = time::elapsedMicros(t0,t1); - printf("usec:%i\n",usec); + cwLogPrint("usec:%i\n",usec); t0 = current_time(); sleepMs(1000); - printf("sleep %i ms\n",elapsedMs(t0)); + cwLogPrint("sleep %i ms\n",elapsedMs(t0)); return kOkRC; diff --git a/cwTime.h b/cwTime.h index 7f1a3ed..5c4d9b7 100644 --- a/cwTime.h +++ b/cwTime.h @@ -85,7 +85,7 @@ namespace cw // Return count of bytes in in buf[] unsigned formatDateTime( char* buf, unsigned bufN, bool includeDateFl=false ); - rc_t test(); + rc_t test( const test::test_args_t& test ); //) diff --git a/cwVectOps.cpp b/cwVectOps.cpp new file mode 100644 index 0000000..c8288b8 --- /dev/null +++ b/cwVectOps.cpp @@ -0,0 +1,15 @@ +#include "cwCommon.h" +#include "cwLog.h" +#include "cwCommonImpl.h" +#include "cwTest.h" +#include "cwVectOps.h" + +cw::rc_t cw::vop::test( const test::test_args_t& args ) +{ + int v1[] = { 1,2,1,2,1,2,1,2,1,2 }; + int v0[ 10 ]; + + cw::vop::deinterleave( v0, v1, 5, 2 ); + cw::vop::print(v0,10,"%i "); + return cw::kOkRC; +} diff --git a/cwVectOps.h b/cwVectOps.h index 277ec11..bc8f087 100644 --- a/cwVectOps.h +++ b/cwVectOps.h @@ -17,10 +17,10 @@ namespace cw if( label != nullptr ) { - printf("%s : ",label); + cwLogPrint("%s : ",label); if( colN && n > colN ) { - printf("\n"); + cwLogPrint("\n"); newline_fl = true; } } @@ -30,19 +30,19 @@ namespace cw for(unsigned i=0; i