aboutsummaryrefslogtreecommitdiff
path: root/src/tests
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/Makefile33
-rw-r--r--src/tests/Resourcetest.cc139
-rw-r--r--src/tests/StringUtiltest.cc140
-rw-r--r--src/tests/keys30
-rw-r--r--src/tests/main.cc142
-rw-r--r--src/tests/testKeys.cc66
6 files changed, 550 insertions, 0 deletions
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 @@
1OBJ=StringUtiltest.o ../StringUtil.o
2CXX=g++
3CXXFLAGS= -I.. -DDEBUG -Wall -g -O2
4LIBS=
5XFLAGS= -I/usr/X11R6/include
6XLIBS= -L/usr/X11R6/lib -lX11
7all: testStringUtil testKeys testResource
8
9.cc.o:
10 $(CXX) -c $(CXXFLAGS) $<
11
12StringUtil.o: ../StringUtil.cc ../StringUtil.hh
13 $(CXX) -c $(CXXFLAGS) ../StringUtil.cc -o StringUtil.o
14
15Keys.o: ../Keys.cc ../Keys.hh
16 $(CXX) -c $(CXXFLAGS) $(XFLAGS) ../Keys.cc -o Keys.o
17
18Resource.o: ../Resource.cc ../Resource.hh
19 $(CXX) -c $(CXXFLAGS) $(XFLAGS) ../Resource.cc -o Resource.o
20
21testStringUtil: StringUtiltest.o StringUtil.o
22 $(CXX) $(LIBS) StringUtiltest.o StringUtil.o -o testStringUtil
23testKeys: Keys.o testKeys.o StringUtil.o
24 $(CXX) $(LIBS) $(XLIBS) StringUtil.o Keys.o testKeys.o -o testKeys
25testResource: Resourcetest.o Resource.o
26 ${CXX} ${LIBS} ${XLIBS} Resourcetest.o Resource.o -o testResource
27
28run: testResource testKeys testStringUtil
29 ./testKeys
30 ./testStringUtil
31 ./testResource
32clean:
33 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 @@
1// Resourcetest.cc
2// Copyright (c) 2001 - 2002 Henrik Kinnunen (fluxgen@linuxmail.org)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22#include "Resource.hh"
23
24//use of strcasecmp
25#ifndef _GNU_SOURCE
26#define _GNU_SOURCE
27#endif // _GNU_SOURCE
28
29#include <string>
30#include <iostream>
31
32using namespace std;
33
34enum TestEnum{TEST1, THIS_IS_TRUE, USE_LOVE};
35
36//----------------
37// Manipulators
38//-----------------
39
40template<>
41void Resource<TestEnum>::
42setFromString(const char *str) {
43 if (strcasecmp(str, "TEST1")==0)
44 *this = TEST1;
45 else if (strcasecmp(str, "THIS_IS_TRUE")==0)
46 *this = THIS_IS_TRUE;
47 else if (strcasecmp(str, "USE_LOVE")==0)
48 *this = USE_LOVE;
49}
50
51template<>
52void Resource<int>::
53setFromString(const char* strval) {
54 int val;
55 if (sscanf(strval, "%d", &val)==1)
56 *this = val;
57}
58
59template<>
60void Resource<std::string>::
61setFromString(const char *strval) {
62 *this = strval;
63}
64
65template<>
66void Resource<bool>::
67setFromString(char const *strval) {
68 if (strcasecmp(strval, "true")==0)
69 *this = true;
70 else
71 *this = false;
72}
73
74//-----------------
75// accessors
76//-----------------
77template<>
78std::string Resource<TestEnum>::
79getString() {
80 switch (m_value) {
81 case TEST1:
82 return string("TEST1");
83 case THIS_IS_TRUE:
84 return string("THIS_IS_TRUE");
85 case USE_LOVE:
86 return string("USE_LOVE");
87 }
88 return string("");
89}
90
91template<>
92std::string Resource<bool>::
93getString() {
94 return std::string(**this == true ? "true" : "false");
95}
96
97template<>
98std::string Resource<int>::
99getString() {
100 char strval[256];
101 sprintf(strval, "%d", **this);
102 return std::string(strval);
103}
104
105template<>
106std::string Resource<string>::
107getString() { return **this; }
108
109int main(int argc, char **argv) {
110
111 ResourceManager rm;
112 // resources
113 Resource<int> val(rm, 123, "session.test", "Session.Test");
114 Resource<bool> boolval(rm, true, "session.bool", "Session.Bool");
115 Resource<string> strval(rm, "none", "string", "String");
116 Resource<TestEnum> enumval(rm, TEST1, "enumval", "EnumVal");
117
118 const char *defaultfile_open = "res",
119 *defaultfile_save = "res_save";
120
121 if (argc>1) {
122 if(!rm.load(argv[1]))
123 cerr<<"Faild to load database: "<<argv[1]<<endl;
124 } else {
125 if (!rm.load(defaultfile_open))
126 cerr<<"Faild to load database: "<<defaultfile_open<<endl;
127 }
128 cerr<<"Value="<<*val<<endl;
129 cerr<<"boolValue="<<boolval.getString()<<endl;
130 cerr<<"strValue="<<*strval<<endl;
131 cerr<<"enumValue="<<enumval.getString()<<endl;
132
133 if (!rm.save(defaultfile_save))
134 cerr<<"Faild to save database to file:"<<defaultfile_save<<endl;
135
136 if (!rm.save("I dont exist", "Not me either"))
137 cerr<<"faild to save and merge database."<<endl;
138 return 0;
139}
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 @@
1// StringUtiltest.cc
2// Copyright (c) 2001 - 2002 Henrik Kinnunen (fluxgen@linuxmail.org)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22#include "../StringUtil.hh"
23
24#include <vector>
25#include <iostream>
26#include <memory>
27
28#ifdef UDS
29#include <uds/init.hh>
30#include <uds/uds.hh>
31// configure UDS
32uds::uds_flags_t uds::flags = uds::leak_check|uds::log_allocs;
33#endif
34
35using namespace std;
36
37void testStringtok() {
38 vector<string> ls;
39 StringUtil::stringtok(ls, " arg1 arg2 \targ3\n arg4 arg5\t\t\t\targ6\n\n \n\n \t\t\narg7");
40 cerr<<"Size: "<<ls.size()<<". Should be: 7."<<endl;
41 for (vector<string>::const_iterator i = ls.begin();
42 i != ls.end(); ++i) {
43 cerr << ':' << (*i) << ":\n";
44 }
45}
46
47void testExpandFilename() {
48 auto_ptr<char> filename(StringUtil::expandFilename("~/filename/~filename2/file3~/file4"));
49 cerr<<"test ";
50 string test = string(getenv("HOME"))+"/filename/~filename2/file3~/file4";
51 if (strcmp(test.c_str(), filename.get())==0)
52 cerr<<"ok.";
53 else
54 cerr<<"faild";
55 cerr<<endl;
56}
57
58void testStrcasestr() {
59 cerr<<"test1 ";
60 if (StringUtil::strcasestr("Test", "TEST") == strcasestr("Test", "TEST"))
61 cerr<<"ok."<<endl;
62 else
63 cerr<<"faild."<<endl;
64
65 cerr<<"test2 ";
66 if (StringUtil::strcasestr("Test", "ESTabc") == strcasestr("Test", "ESTabc"))
67 cerr<<"ok."<<endl;
68 else
69 cerr<<"faild."<<endl;
70
71 cerr<<"test3 ";
72 if (StringUtil::strcasestr("TeSt", "abcTEStabc") == strcasestr("TeSt", "abcTEStabc"))
73 cerr<<"ok."<<endl;
74 else
75 cerr<<"faild."<<endl;
76
77 cerr<<"test4 ";
78 if (StringUtil::strcasestr("TEST", "_TEST;_") == strcasestr("TEST", "_TEST;_"))
79 cerr<<"ok."<<endl;
80 else
81 cerr<<"faild."<<endl;
82
83}
84
85void showError(int line, int pos, string& instr) {
86
87 cerr<<"Error on line: "<<line<<endl;
88 cerr<<instr<<endl;
89 for (int c=0; c<pos; c++) {
90 if (instr[c]=='\t')
91 cerr<<'\t';
92 else
93 cerr<<" ";
94 }
95 cerr<<"^ here"<<endl;
96
97}
98
99void testGetStringBetween() {
100 string out;
101 vector<string> stringlist;
102 stringlist.push_back(" \t\t\t \t[(in \\)\t haha )] \t\t ");
103 stringlist.push_back("(in\\)) {_ _ my_ _}");
104 stringlist.push_back("(in) {_ _ my_ _}");
105 stringlist.push_back("(in){_ _ my_ _}");
106 stringlist.push_back("\t \t \t ( in ) {haha}");
107 stringlist.push_back("\t \t \t (( in \\) ) {haha}");
108 stringlist.push_back("\t \t \t (( in \\) ){hihi}");
109 stringlist.push_back("\t \t \t (( in \\) )|{hihi}");
110 for (unsigned int i=0; i<stringlist.size(); i++) {
111 int pos = StringUtil::getStringBetween(out, stringlist[i].c_str(), '(', ')');
112 int total_pos = 0;
113 if (pos<0) {
114 showError(i+1, -pos, stringlist[i]);
115 continue;
116 }
117 cerr<<"string="<<stringlist[i]<<endl;
118 cerr<<"pos="<<pos<<" ::"<<out;
119 total_pos += pos;
120 pos = StringUtil::getStringBetween(out, stringlist[i].c_str()+total_pos, '{', '}');
121 if (pos<=0) {
122 pos=-pos;
123 showError(i+1, total_pos+pos, stringlist[i]);
124 continue;
125 }
126 cerr<<"::"<<out<<"::"<<endl;
127 total_pos += pos;
128 }
129}
130int main() {
131 #ifdef UDS
132 uds::Init uds_init;
133 #endif
134 cerr<<"Testing stringtok."<<endl;
135 testStringtok();
136 cerr<<"Testing expandFilename."<<endl;
137 testExpandFilename();
138 cerr<<"Testing strcasestr."<<endl;
139 testStrcasestr();
140}
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 @@
1#this line is a comment
2#Mod1 B :NextWindow
3# Mod1 C :PrevWindow
4 # Mod1 E :NextWindow
5 # Mod1 G :PrevWindow
6
7Mod1 F1 :Workspace1
8Mod1 F2 :Workspace2
9Mod1 F3 :Workspace3
10
11Mod1 Tab :NextWindow
12Mod1 Shift Tab :PrevWindow
13Mod1 XX :MaximizeWindow
14Mod1 F4 :Workspace4
15Mod1 F5 :Workspace5
16Mod1 F6 :Workspace6
17Mod1 F7 :Workspace7
18Mod1 F8 :Workspace8
19Mod1 F9 :Workspace9
20Mod1 F10 :Workspace10
21Mod1 F11 :Workspace11
22Mod1 F12 :Workspace12
23Mod1 Q :PrevWorkspace
24Mod1 W :NextWorkspace
25Mod1 Shift W :NextTab
26Mod1 Shift Q :PrevTab
27Mod3 Q Mod1 W :ExecCommand first xterm arg1 arg2 stuff
28Mod2 Q Mod1 W :NextWindow
29Mod3 Q :ExecCommand xterm arg1 arg2 stuff
30None 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 @@
1// main.cc
2// Copyright (c) 2001 - 2002 Henrik Kinnunen (fluxgen@linuxmail.org)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22#include "../StringUtil.hh"
23
24#include <iostream>
25#include <fstream>
26#include <string>
27#include <vector>
28
29using namespace std;
30
31bool loadMenu(string filename);
32bool loadMenu2(string filename);
33
34void showError(int line, int pos, string& instr) {
35
36 cerr<<"Error on line: "<<line<<endl;
37 cerr<<instr<<endl;
38 for (int c=0; c<pos; c++) {
39 if (instr[c]=='\t')
40 cerr<<'\t';
41 else
42 cerr<<" ";
43 }
44 cerr<<"^ here"<<endl;
45
46}
47
48int main(int argc, char **argv) {
49 string filename = "menu";
50 if (argc>1)
51 filename = argv[1];
52 if (loadMenu2(filename))
53 cout<<"Load successfull"<<endl;
54 else
55 cout<<"Load failed"<<endl;
56
57/*
58 string out;
59 vector<string> stringlist;
60 stringlist.push_back(" \t\t\t \t[(in \\)\t haha )] \t\t ");
61 stringlist.push_back("(in\\)) {_ _ my_ _}");
62 stringlist.push_back("(in) {_ _ my_ _}");
63 stringlist.push_back("(in){_ _ my_ _}");
64 stringlist.push_back("\t \t \t ( in ) {haha}");
65 stringlist.push_back("\t \t \t (( in \\) ) {haha}");
66 stringlist.push_back("\t \t \t (( in \\) ){hihi}");
67 stringlist.push_back("\t \t \t (( in \\) )|{hihi}");
68 for (unsigned int i=0; i<stringlist.size(); i++) {
69 int pos = StringUtil::getStringBetween(out, stringlist[i].c_str(), '(', ')');
70 int total_pos = 0;
71 if (pos<0) {
72 showError(i+1, -pos, stringlist[i]);
73 continue;
74 }
75 cerr<<"string="<<stringlist[i]<<endl;
76 cerr<<"pos="<<pos<<" ::"<<out;
77 total_pos += pos;
78 pos = StringUtil::getStringBetween(out, stringlist[i].c_str()+total_pos, '{', '}');
79 if (pos<=0) {
80 pos=-pos;
81 showError(i+1, total_pos+pos, stringlist[i]);
82 continue;
83 }
84 cerr<<"::"<<out<<"::"<<endl;
85 total_pos += pos;
86 }
87*/
88 return 0;
89}
90
91
92
93bool loadMenu2(string filename) {
94
95 if (!filename.size())
96 return false;
97
98 ifstream menufile(filename.c_str());
99
100
101 if (menufile) {
102 string instr;
103 vector<string> args;
104 int line=0;
105 while (!menufile.eof()) {
106 //read a line
107 getline(menufile, instr);
108 line++;
109 string arg;
110 int pos = StringUtil::getStringBetween(arg, instr.c_str(), '[', ']');
111 if (pos<=0) {
112 showError(line, -pos, instr);
113 continue;
114 }
115
116 cerr<<"("<<line<<"):"<<arg<<"::";
117 int total_pos = pos;
118 pos = StringUtil::getStringBetween(arg, instr.c_str()+pos, '(', ')');
119 if (pos<=0) {
120 showError(line, total_pos+(-pos), instr);
121 continue;
122 }
123 cerr<<arg<<"::";
124
125 total_pos +=pos;
126 pos = StringUtil::getStringBetween(arg, instr.c_str()+total_pos, '{', '}');
127 if (pos<=0) {
128 total_pos = total_pos+(-pos);
129 showError(line, total_pos, instr);
130 continue;
131 }
132 cerr<<arg<<":"<<endl;
133
134 }
135
136
137 } else
138 return false;
139
140 return true;
141}
142
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 @@
1// testKeys.cc
2// Copyright (c) 2001 - 2002 Henrik Kinnunen (fluxgen@linuxmail.org)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22#include "../Keys.hh"
23#include <iostream>
24#include <X11/Xlib.h>
25#include <uds/init.hh>
26
27#ifdef UDS
28#include <uds/uds.hh>
29// configure UDS
30uds::uds_flags_t uds::flags = uds::leak_check|uds::log_allocs;
31#endif
32
33using namespace std;
34
35void testKeys(int argc, char **argv) {
36 Display *display = XOpenDisplay(0);
37
38 if (display==0) {
39 cerr<<"Cant open display."<<endl;
40 return;
41 }
42
43 Keys *keys = new Keys(display);
44 const char default_keyfile[] = "keys";
45
46 if (argc>1) {
47 cerr<<"Loading file: "<<argv[1]<<endl;
48 keys->load(const_cast<char *>(argv[1]));
49 } else {
50 cerr<<"Using default file: "<<default_keyfile<<endl;
51 keys->load(const_cast<char *>(default_keyfile));
52 }
53
54 keys->load(const_cast<char *>(default_keyfile));
55
56 delete keys;
57
58 XCloseDisplay(display);
59}
60
61int main(int argc, char **argv) {
62 #ifdef UDS
63 uds::Init uds_init;
64 #endif
65 testKeys(argc, argv);
66}