From ccb2beb0e1e078a33a8603ec591adadf78ff148a Mon Sep 17 00:00:00 2001 From: fluxgen <fluxgen> Date: Mon, 4 Feb 2002 23:48:31 +0000 Subject: initial import --- src/tests/Makefile | 33 ++++++++++ src/tests/Resourcetest.cc | 139 +++++++++++++++++++++++++++++++++++++++++++ src/tests/StringUtiltest.cc | 140 +++++++++++++++++++++++++++++++++++++++++++ src/tests/keys | 30 ++++++++++ src/tests/main.cc | 142 ++++++++++++++++++++++++++++++++++++++++++++ src/tests/testKeys.cc | 66 ++++++++++++++++++++ 6 files changed, 550 insertions(+) create mode 100644 src/tests/Makefile create mode 100644 src/tests/Resourcetest.cc create mode 100644 src/tests/StringUtiltest.cc create mode 100644 src/tests/keys create mode 100644 src/tests/main.cc create mode 100644 src/tests/testKeys.cc diff --git a/src/tests/Makefile b/src/tests/Makefile new file mode 100644 index 0000000..ac8fdb0 --- /dev/null +++ b/src/tests/Makefile @@ -0,0 +1,33 @@ +OBJ=StringUtiltest.o ../StringUtil.o +CXX=g++ +CXXFLAGS= -I.. -DDEBUG -Wall -g -O2 +LIBS= +XFLAGS= -I/usr/X11R6/include +XLIBS= -L/usr/X11R6/lib -lX11 +all: testStringUtil testKeys testResource + +.cc.o: + $(CXX) -c $(CXXFLAGS) $< + +StringUtil.o: ../StringUtil.cc ../StringUtil.hh + $(CXX) -c $(CXXFLAGS) ../StringUtil.cc -o StringUtil.o + +Keys.o: ../Keys.cc ../Keys.hh + $(CXX) -c $(CXXFLAGS) $(XFLAGS) ../Keys.cc -o Keys.o + +Resource.o: ../Resource.cc ../Resource.hh + $(CXX) -c $(CXXFLAGS) $(XFLAGS) ../Resource.cc -o Resource.o + +testStringUtil: StringUtiltest.o StringUtil.o + $(CXX) $(LIBS) StringUtiltest.o StringUtil.o -o testStringUtil +testKeys: Keys.o testKeys.o StringUtil.o + $(CXX) $(LIBS) $(XLIBS) StringUtil.o Keys.o testKeys.o -o testKeys +testResource: Resourcetest.o Resource.o + ${CXX} ${LIBS} ${XLIBS} Resourcetest.o Resource.o -o testResource + +run: testResource testKeys testStringUtil + ./testKeys + ./testStringUtil + ./testResource +clean: + rm -f *.o diff --git a/src/tests/Resourcetest.cc b/src/tests/Resourcetest.cc new file mode 100644 index 0000000..8cfae58 --- /dev/null +++ b/src/tests/Resourcetest.cc @@ -0,0 +1,139 @@ +// Resourcetest.cc +// Copyright (c) 2001 - 2002 Henrik Kinnunen (fluxgen@linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +#include "Resource.hh" + +//use of strcasecmp +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif // _GNU_SOURCE + +#include <string> +#include <iostream> + +using namespace std; + +enum TestEnum{TEST1, THIS_IS_TRUE, USE_LOVE}; + +//---------------- +// Manipulators +//----------------- + +template<> +void Resource<TestEnum>:: +setFromString(const char *str) { + if (strcasecmp(str, "TEST1")==0) + *this = TEST1; + else if (strcasecmp(str, "THIS_IS_TRUE")==0) + *this = THIS_IS_TRUE; + else if (strcasecmp(str, "USE_LOVE")==0) + *this = USE_LOVE; +} + +template<> +void Resource<int>:: +setFromString(const char* strval) { + int val; + if (sscanf(strval, "%d", &val)==1) + *this = val; +} + +template<> +void Resource<std::string>:: +setFromString(const char *strval) { + *this = strval; +} + +template<> +void Resource<bool>:: +setFromString(char const *strval) { + if (strcasecmp(strval, "true")==0) + *this = true; + else + *this = false; +} + +//----------------- +// accessors +//----------------- +template<> +std::string Resource<TestEnum>:: +getString() { + switch (m_value) { + case TEST1: + return string("TEST1"); + case THIS_IS_TRUE: + return string("THIS_IS_TRUE"); + case USE_LOVE: + return string("USE_LOVE"); + } + return string(""); +} + +template<> +std::string Resource<bool>:: +getString() { + return std::string(**this == true ? "true" : "false"); +} + +template<> +std::string Resource<int>:: +getString() { + char strval[256]; + sprintf(strval, "%d", **this); + return std::string(strval); +} + +template<> +std::string Resource<string>:: +getString() { return **this; } + +int main(int argc, char **argv) { + + ResourceManager rm; + // resources + Resource<int> val(rm, 123, "session.test", "Session.Test"); + Resource<bool> boolval(rm, true, "session.bool", "Session.Bool"); + Resource<string> strval(rm, "none", "string", "String"); + Resource<TestEnum> enumval(rm, TEST1, "enumval", "EnumVal"); + + const char *defaultfile_open = "res", + *defaultfile_save = "res_save"; + + if (argc>1) { + if(!rm.load(argv[1])) + cerr<<"Faild to load database: "<<argv[1]<<endl; + } else { + if (!rm.load(defaultfile_open)) + cerr<<"Faild to load database: "<<defaultfile_open<<endl; + } + cerr<<"Value="<<*val<<endl; + cerr<<"boolValue="<<boolval.getString()<<endl; + cerr<<"strValue="<<*strval<<endl; + cerr<<"enumValue="<<enumval.getString()<<endl; + + if (!rm.save(defaultfile_save)) + cerr<<"Faild to save database to file:"<<defaultfile_save<<endl; + + if (!rm.save("I dont exist", "Not me either")) + cerr<<"faild to save and merge database."<<endl; + return 0; +} diff --git a/src/tests/StringUtiltest.cc b/src/tests/StringUtiltest.cc new file mode 100644 index 0000000..95dd5f0 --- /dev/null +++ b/src/tests/StringUtiltest.cc @@ -0,0 +1,140 @@ +// StringUtiltest.cc +// Copyright (c) 2001 - 2002 Henrik Kinnunen (fluxgen@linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +#include "../StringUtil.hh" + +#include <vector> +#include <iostream> +#include <memory> + +#ifdef UDS +#include <uds/init.hh> +#include <uds/uds.hh> +// configure UDS +uds::uds_flags_t uds::flags = uds::leak_check|uds::log_allocs; +#endif + +using namespace std; + +void testStringtok() { + vector<string> ls; + StringUtil::stringtok(ls, " arg1 arg2 \targ3\n arg4 arg5\t\t\t\targ6\n\n \n\n \t\t\narg7"); + cerr<<"Size: "<<ls.size()<<". Should be: 7."<<endl; + for (vector<string>::const_iterator i = ls.begin(); + i != ls.end(); ++i) { + cerr << ':' << (*i) << ":\n"; + } +} + +void testExpandFilename() { + auto_ptr<char> filename(StringUtil::expandFilename("~/filename/~filename2/file3~/file4")); + cerr<<"test "; + string test = string(getenv("HOME"))+"/filename/~filename2/file3~/file4"; + if (strcmp(test.c_str(), filename.get())==0) + cerr<<"ok."; + else + cerr<<"faild"; + cerr<<endl; +} + +void testStrcasestr() { + cerr<<"test1 "; + if (StringUtil::strcasestr("Test", "TEST") == strcasestr("Test", "TEST")) + cerr<<"ok."<<endl; + else + cerr<<"faild."<<endl; + + cerr<<"test2 "; + if (StringUtil::strcasestr("Test", "ESTabc") == strcasestr("Test", "ESTabc")) + cerr<<"ok."<<endl; + else + cerr<<"faild."<<endl; + + cerr<<"test3 "; + if (StringUtil::strcasestr("TeSt", "abcTEStabc") == strcasestr("TeSt", "abcTEStabc")) + cerr<<"ok."<<endl; + else + cerr<<"faild."<<endl; + + cerr<<"test4 "; + if (StringUtil::strcasestr("TEST", "_TEST;_") == strcasestr("TEST", "_TEST;_")) + cerr<<"ok."<<endl; + else + cerr<<"faild."<<endl; + +} + +void showError(int line, int pos, string& instr) { + + cerr<<"Error on line: "<<line<<endl; + cerr<<instr<<endl; + for (int c=0; c<pos; c++) { + if (instr[c]=='\t') + cerr<<'\t'; + else + cerr<<" "; + } + cerr<<"^ here"<<endl; + +} + +void testGetStringBetween() { + string out; + vector<string> stringlist; + stringlist.push_back(" \t\t\t \t[(in \\)\t haha )] \t\t "); + stringlist.push_back("(in\\)) {_ _ my_ _}"); + stringlist.push_back("(in) {_ _ my_ _}"); + stringlist.push_back("(in){_ _ my_ _}"); + stringlist.push_back("\t \t \t ( in ) {haha}"); + stringlist.push_back("\t \t \t (( in \\) ) {haha}"); + stringlist.push_back("\t \t \t (( in \\) ){hihi}"); + stringlist.push_back("\t \t \t (( in \\) )|{hihi}"); + for (unsigned int i=0; i<stringlist.size(); i++) { + int pos = StringUtil::getStringBetween(out, stringlist[i].c_str(), '(', ')'); + int total_pos = 0; + if (pos<0) { + showError(i+1, -pos, stringlist[i]); + continue; + } + cerr<<"string="<<stringlist[i]<<endl; + cerr<<"pos="<<pos<<" ::"<<out; + total_pos += pos; + pos = StringUtil::getStringBetween(out, stringlist[i].c_str()+total_pos, '{', '}'); + if (pos<=0) { + pos=-pos; + showError(i+1, total_pos+pos, stringlist[i]); + continue; + } + cerr<<"::"<<out<<"::"<<endl; + total_pos += pos; + } +} +int main() { + #ifdef UDS + uds::Init uds_init; + #endif + cerr<<"Testing stringtok."<<endl; + testStringtok(); + cerr<<"Testing expandFilename."<<endl; + testExpandFilename(); + cerr<<"Testing strcasestr."<<endl; + testStrcasestr(); +} diff --git a/src/tests/keys b/src/tests/keys new file mode 100644 index 0000000..9242708 --- /dev/null +++ b/src/tests/keys @@ -0,0 +1,30 @@ +#this line is a comment +#Mod1 B :NextWindow +# Mod1 C :PrevWindow + # Mod1 E :NextWindow + # Mod1 G :PrevWindow + +Mod1 F1 :Workspace1 +Mod1 F2 :Workspace2 +Mod1 F3 :Workspace3 + +Mod1 Tab :NextWindow +Mod1 Shift Tab :PrevWindow +Mod1 XX :MaximizeWindow +Mod1 F4 :Workspace4 +Mod1 F5 :Workspace5 +Mod1 F6 :Workspace6 +Mod1 F7 :Workspace7 +Mod1 F8 :Workspace8 +Mod1 F9 :Workspace9 +Mod1 F10 :Workspace10 +Mod1 F11 :Workspace11 +Mod1 F12 :Workspace12 +Mod1 Q :PrevWorkspace +Mod1 W :NextWorkspace +Mod1 Shift W :NextTab +Mod1 Shift Q :PrevTab +Mod3 Q Mod1 W :ExecCommand first xterm arg1 arg2 stuff +Mod2 Q Mod1 W :NextWindow +Mod3 Q :ExecCommand xterm arg1 arg2 stuff +None XF86AudioLowerVolume :ExecCommand aumix -w -5 diff --git a/src/tests/main.cc b/src/tests/main.cc new file mode 100644 index 0000000..1ffbfcd --- /dev/null +++ b/src/tests/main.cc @@ -0,0 +1,142 @@ +// main.cc +// Copyright (c) 2001 - 2002 Henrik Kinnunen (fluxgen@linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +#include "../StringUtil.hh" + +#include <iostream> +#include <fstream> +#include <string> +#include <vector> + +using namespace std; + +bool loadMenu(string filename); +bool loadMenu2(string filename); + +void showError(int line, int pos, string& instr) { + + cerr<<"Error on line: "<<line<<endl; + cerr<<instr<<endl; + for (int c=0; c<pos; c++) { + if (instr[c]=='\t') + cerr<<'\t'; + else + cerr<<" "; + } + cerr<<"^ here"<<endl; + +} + +int main(int argc, char **argv) { + string filename = "menu"; + if (argc>1) + filename = argv[1]; + if (loadMenu2(filename)) + cout<<"Load successfull"<<endl; + else + cout<<"Load failed"<<endl; + +/* + string out; + vector<string> stringlist; + stringlist.push_back(" \t\t\t \t[(in \\)\t haha )] \t\t "); + stringlist.push_back("(in\\)) {_ _ my_ _}"); + stringlist.push_back("(in) {_ _ my_ _}"); + stringlist.push_back("(in){_ _ my_ _}"); + stringlist.push_back("\t \t \t ( in ) {haha}"); + stringlist.push_back("\t \t \t (( in \\) ) {haha}"); + stringlist.push_back("\t \t \t (( in \\) ){hihi}"); + stringlist.push_back("\t \t \t (( in \\) )|{hihi}"); + for (unsigned int i=0; i<stringlist.size(); i++) { + int pos = StringUtil::getStringBetween(out, stringlist[i].c_str(), '(', ')'); + int total_pos = 0; + if (pos<0) { + showError(i+1, -pos, stringlist[i]); + continue; + } + cerr<<"string="<<stringlist[i]<<endl; + cerr<<"pos="<<pos<<" ::"<<out; + total_pos += pos; + pos = StringUtil::getStringBetween(out, stringlist[i].c_str()+total_pos, '{', '}'); + if (pos<=0) { + pos=-pos; + showError(i+1, total_pos+pos, stringlist[i]); + continue; + } + cerr<<"::"<<out<<"::"<<endl; + total_pos += pos; + } +*/ + return 0; +} + + + +bool loadMenu2(string filename) { + + if (!filename.size()) + return false; + + ifstream menufile(filename.c_str()); + + + if (menufile) { + string instr; + vector<string> args; + int line=0; + while (!menufile.eof()) { + //read a line + getline(menufile, instr); + line++; + string arg; + int pos = StringUtil::getStringBetween(arg, instr.c_str(), '[', ']'); + if (pos<=0) { + showError(line, -pos, instr); + continue; + } + + cerr<<"("<<line<<"):"<<arg<<"::"; + int total_pos = pos; + pos = StringUtil::getStringBetween(arg, instr.c_str()+pos, '(', ')'); + if (pos<=0) { + showError(line, total_pos+(-pos), instr); + continue; + } + cerr<<arg<<"::"; + + total_pos +=pos; + pos = StringUtil::getStringBetween(arg, instr.c_str()+total_pos, '{', '}'); + if (pos<=0) { + total_pos = total_pos+(-pos); + showError(line, total_pos, instr); + continue; + } + cerr<<arg<<":"<<endl; + + } + + + } else + return false; + + return true; +} + diff --git a/src/tests/testKeys.cc b/src/tests/testKeys.cc new file mode 100644 index 0000000..071eed3 --- /dev/null +++ b/src/tests/testKeys.cc @@ -0,0 +1,66 @@ +// testKeys.cc +// Copyright (c) 2001 - 2002 Henrik Kinnunen (fluxgen@linuxmail.org) +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +#include "../Keys.hh" +#include <iostream> +#include <X11/Xlib.h> +#include <uds/init.hh> + +#ifdef UDS +#include <uds/uds.hh> +// configure UDS +uds::uds_flags_t uds::flags = uds::leak_check|uds::log_allocs; +#endif + +using namespace std; + +void testKeys(int argc, char **argv) { + Display *display = XOpenDisplay(0); + + if (display==0) { + cerr<<"Cant open display."<<endl; + return; + } + + Keys *keys = new Keys(display); + const char default_keyfile[] = "keys"; + + if (argc>1) { + cerr<<"Loading file: "<<argv[1]<<endl; + keys->load(const_cast<char *>(argv[1])); + } else { + cerr<<"Using default file: "<<default_keyfile<<endl; + keys->load(const_cast<char *>(default_keyfile)); + } + + keys->load(const_cast<char *>(default_keyfile)); + + delete keys; + + XCloseDisplay(display); +} + +int main(int argc, char **argv) { + #ifdef UDS + uds::Init uds_init; + #endif + testKeys(argc, argv); +} -- cgit v0.11.2