add more spacing
[personal-kdebase.git] / workspace / libs / ksysguard / tests / processtest.cpp
blobab3d4042b8bfbd49e6c55a64505691b621f689a9
1 /* This file is part of the KDE project
2 Copyright (C) 2007 John Tapsell <tapsell@kde.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
21 #include <QtTest>
22 #include <QtCore>
24 #include <klocale.h>
25 #include <qtest_kde.h>
26 #include <kdebug.h>
28 #include "processcore/processes.h"
29 #include "processcore/process.h"
31 #include "processtest.h"
33 void testProcess::testProcesses() {
34 KSysGuard::Processes *processController = KSysGuard::Processes::getInstance();
35 processController->updateAllProcesses();
36 QList<KSysGuard::Process *> processes = processController->getAllProcesses();
37 QSet<long> pids;
38 foreach( KSysGuard::Process *process, processes) {
39 if(process->pid == 0) continue;
40 QVERIFY(process->pid > 0);
41 QVERIFY(!process->name.isEmpty());
43 //test all the pids are unique
44 QVERIFY(!pids.contains(process->pid));
45 pids.insert(process->pid);
47 processController->updateAllProcesses();
48 QList<KSysGuard::Process *> processes2 = processController->getAllProcesses();
49 foreach( KSysGuard::Process *process, processes2) {
50 if(process->pid == 0) continue;
51 QVERIFY(process->pid > 0);
52 QVERIFY(!process->name.isEmpty());
54 //test all the pids are unique
55 if(!pids.contains(process->pid)) {
56 kDebug() << process->pid << " not found. " << process->name;
58 pids.remove(process->pid);
61 QVERIFY(processes2.size() == processes.size());
62 QCOMPARE(processes, processes2); //Make sure calling it twice gives the same results. The difference in time is so small that it really shouldn't have changed
66 unsigned long testProcess::countNumChildren(KSysGuard::Process *p) {
67 unsigned long total = p->children.size();
68 for(int i =0; i < p->children.size(); i++) {
69 total += countNumChildren(p->children[i]);
71 return total;
74 void testProcess::testProcessesTreeStructure() {
75 KSysGuard::Processes *processController = KSysGuard::Processes::getInstance();
76 processController->updateAllProcesses();
77 QList<KSysGuard::Process *> processes = processController->getAllProcesses();
79 foreach( KSysGuard::Process *process, processes) {
80 QCOMPARE(countNumChildren(process), process->numChildren);
82 for(int i =0; i < process->children.size(); i++) {
83 QVERIFY(process->children[i]->parent);
84 QCOMPARE(process->children[i]->parent, process);
90 void testProcess::testProcessesModification() {
91 //We will modify the tree, then re-call getProcesses and make sure that it fixed everything we modified
92 KSysGuard::Processes *processController = KSysGuard::Processes::getInstance();
93 processController->updateAllProcesses();
94 KSysGuard::Process *initProcess = processController->getProcess(1);
96 if(!initProcess || initProcess->numChildren < 3)
97 return;
99 QVERIFY(initProcess);
100 QVERIFY(initProcess->children[0]);
101 QVERIFY(initProcess->children[1]);
102 kDebug() << initProcess->numChildren;
103 initProcess->children[0]->parent = initProcess->children[1];
104 initProcess->children[1]->children.append(initProcess->children[0]);
105 initProcess->children[1]->numChildren++;
106 initProcess->numChildren--;
107 initProcess->children.removeAt(0);
110 void testProcess::testTime() {
111 //See how long it takes to get proccess information
112 KSysGuard::Processes *processController = KSysGuard::Processes::getInstance();
113 QTime t;
114 t.start();
115 for(int i =0; i < 100; i++)
116 processController->updateAllProcesses();
117 kDebug() << "Time elapsed: "<< t.elapsed() <<" ms, so " << t.elapsed()/100 << "ms" << endl;
118 QVERIFY(t.elapsed()/100 < 300); //It should take less than about 100ms. Anything longer than 300ms even on a slow system really needs to be optimised
121 QTEST_KDEMAIN_CORE(testProcess)
123 #include "processtest.moc"