Ok, the long awaited config wizard is here (at least in a very basic state). There...
[quassel.git] / src / contrib / libqxt-2007-10-24 / tests / QxtFileLock / src / main.cpp
1
2
3 #include <QCoreApplication>
4 #include <QFile>
5 #include <QxtFileLock>
6 #include <QDebug>
7
8 #include <windows.h>
9
10
11 #include "threadtestcontroller.h"
12 #include "HelperClass.h"
13
14
15 /*
16 Needed Test:
17 1.    one thread test
18        - open the same file twice
19        - lock fileregion with readlock (handle 1)
20        - try to lock the same region with a read lock -> should work
21        - try to lock the same region with a write lock -> should fail
22        - remove all locks
23        - create a writelock on handle 1
24        - create a writelock on handle 2 ->>fail
25        - try to lock totally different regions --> should work
26        
27 2.    multiple threadstest:
28        - spawn two threads
29        - open the same file twice
30        - let thread 1 lock (READLOCK) a region of a file
31        - let thread 2 do the same lock ---> should work
32        - let thread 1 upgrade its lock to a WRITELOCK -->should fail (because thread 2 holds the readlock)
33        - remove all locks
34        - try to lock totally different regions of the file -> should work
35 */
36
37 int main(int argc, char *argv[])
38 {
39       QCoreApplication app(argc, argv);
40       
41       if(1)
42       {
43         QFile file1("lock.file");
44         QFile file2("lock.file");
45         
46         if(file1.open(QIODevice::ReadWrite) && file2.open(QIODevice::ReadWrite))
47         {
48
49             if(1)
50             {
51                 qDebug()<<"----Starting first test----";
52                 qDebug()<<"Trying to create some locks without collison";
53
54                 QxtFileLock lock1(&file1,0x10,20,QxtFileLock::WriteLock);
55                 if(lock1.lock())
56                     qDebug()<<"---- Write Lock Test passed----";
57                 else
58                     qDebug()<<"---- Write Lock Test failed----";
59
60                 lock1.unlock();
61
62                 QxtFileLock lock2(&file2,0x10,20,QxtFileLock::ReadLock);
63                 if(lock2.lock())
64                     qDebug()<<"---- Read Lock Test passed----";
65                 else
66                     qDebug()<<"---- Read Lock Test failed----";
67
68                 lock2.unlock();
69
70             }
71             
72             if(1)
73             {
74                 qDebug()<<"----Starting next test-----";
75                 qDebug()<<"Trying to readlock the same region with DIFFERENT handles ";
76                 QxtFileLock *lock1 = new QxtFileLock(&file1,0x10,20,QxtFileLock::ReadLock);
77                 QxtFileLock *lock2 = new QxtFileLock(&file2,0x10,20,QxtFileLock::ReadLock);
78                             
79                 if(lock1->lock() && lock2->lock())
80                     qDebug()<<"----Test passed----";
81                 else
82                     qDebug()<<"----Test failed----";
83                 
84                 delete lock1;
85                 delete lock2;
86             }
87             
88             if(1)
89             {
90                 qDebug()<<"----Starting next test-----";
91                 qDebug()<<"Trying to lock the same region with DIFFERENT handles and different locks";
92                 QxtFileLock *lock1 = new QxtFileLock(&file1,0x10,20,QxtFileLock::ReadLock);
93                 QxtFileLock *lock2 = new QxtFileLock(&file2,0x10,20,QxtFileLock::WriteLock);
94                             
95                 if(lock1->lock() && !lock2->lock())
96                     qDebug()<<"----Test passed----";
97                 else
98                     qDebug()<<"----Test failed----";
99                 
100                 delete lock1;
101                 delete lock2;
102             }
103             
104             if(1)
105             {
106                 qDebug()<<"----Starting next test-----";
107                 qDebug()<<"Trying to writelock the same region with DIFFERENT handles";
108                 QxtFileLock *lock1 = new QxtFileLock(&file1,0x10,20,QxtFileLock::WriteLock);
109                 QxtFileLock *lock2 = new QxtFileLock(&file2,0x10,20,QxtFileLock::WriteLock);
110                             
111                 if(lock1->lock() && !lock2->lock())
112                     qDebug()<<"----Test passed----";
113                 else
114                     qDebug()<<"----Test failed----";
115                 
116                 delete lock1;
117                 delete lock2;
118             }
119             
120             if(1)
121             {
122                 qDebug()<<"----Starting next test-----";
123                 qDebug()<<"Trying to writelock the different regions with DIFFERENT handles";
124                 QxtFileLock *lock1 = new QxtFileLock(&file1,0x10,20,QxtFileLock::WriteLock);
125                 QxtFileLock *lock2 = new QxtFileLock(&file2,0x10+21,20,QxtFileLock::WriteLock);
126                             
127                 if(lock1->lock() && lock2->lock())
128                     qDebug()<<"----Test passed----";
129                 else
130                     qDebug()<<"----Test failed----";
131                 
132                 delete lock1;
133                 delete lock2;
134             }
135         }
136       }
137       
138       qDebug()<<"All base tests are finished, now starting the threaded tests";
139       
140       ThreadTestController controller;
141       
142       if(controller.startTests())
143       {
144           HelperClass *testClient = new HelperClass();
145           testClient->start();
146           return app.exec();    
147       }
148       
149       return 0;
150 }
151