Skip to content

Commit 8c983e4

Browse files
author
Kimmo
committed
test
1 parent d5da935 commit 8c983e4

File tree

9 files changed

+232
-0
lines changed

9 files changed

+232
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
TEMPLATE = subdirs
2+
SUBDIRS += \
3+
src \
4+
test_runner
5+
6+
test_runner.depends = src
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "container.h"
2+
#include <QDebug>
3+
4+
Container::Container()
5+
{
6+
7+
}
8+
9+
// 1) Make a function listPractice(QList<int> list) that
10+
// uses a mutable iterator to multiply all numbers divisible by 5 by 3,
11+
// and then returns the list.
12+
// 2) Make a function heights(QMap<QString, float> heights) that
13+
// returns a QList<float> of heights of people whose name starts with 'A'.
14+
// Return them in the order QMap holds them (by key).
15+
// 3) Make a function names(QMap<QString, float> heights) that returns
16+
// a QList<QString> of names of people who are taller than 1.80m. You
17+
// can assume that heights are unique. Use alphabetical order here as well.
18+
19+
// BEGIN SOLUTION
20+
QList<int> Container::listPractise(QList<int> list)
21+
{
22+
QMutableListIterator<int> i(list);
23+
while (i.hasNext()) {
24+
int j = i.next();
25+
if (list[j] % 5 == 0) {
26+
list[j] = list[j] * 3;
27+
}
28+
}
29+
return list;
30+
}
31+
32+
QList<float> Container::heights(QMap<QString, float> heights)
33+
{
34+
QList<float> heightList;
35+
for (QMap<QString, float>::iterator i = heights.begin(); i != heights.end(); i++) {
36+
if (i.key().startsWith('A'))
37+
heightList.append(heights.value(i.key()));
38+
}
39+
return heightList;
40+
}
41+
42+
QList<QString> Container::names(QMap<QString, float> heights)
43+
{
44+
QList<QString> nameList;
45+
for (QMap<QString, float>::iterator i = heights.begin(); i != heights.end(); i++) {
46+
if (i.value() > 1.80f)
47+
nameList.append(heights.key(i.value()));
48+
}
49+
return nameList;
50+
}
51+
// END SOLUTION
52+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef CONTAINER_H
2+
#define CONTAINER_H
3+
#include <QList>
4+
#include <QMap>
5+
#include <QString>
6+
7+
class Container
8+
{
9+
10+
public:
11+
12+
Container();
13+
14+
QList<int> listPractise(QList<int> list);
15+
QList<float> heights(QMap<QString, float> heights);
16+
QList<QString> names(QMap<QString, float> heights);
17+
};
18+
19+
#endif //CONTAINER_H

Part1/1.02_containers/src/main.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <QCoreApplication>
2+
#include <QList>
3+
#include "container.h"
4+
5+
using namespace std;
6+
7+
// You can manually test your code in main.
8+
int main()
9+
{
10+
}

Part1/1.02_containers/src/src.pro

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
QT -= gui
2+
3+
TARGET = container
4+
5+
CONFIG += c++11 console
6+
CONFIG -= app_bundle
7+
8+
win32 {
9+
CONFIG -= debug_and_release debug_and_release_target
10+
}
11+
12+
DEFINES += QT_DEPRECATED_WARNINGS
13+
14+
SOURCES += \
15+
main.cpp \
16+
container.cpp
17+
18+
HEADERS += \
19+
container.h
20+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include <QtTest/QTest>
2+
#include "test_runner.h"
3+
4+
QTEST_APPLESS_MAIN (test_runner)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <QTest>
2+
#include <QList>
3+
#include <QDebug>
4+
#include "test_runner.h"
5+
#include "container.h"
6+
7+
#define POINT(test_name, point) qInfo("TMC:"#test_name"."#point)
8+
9+
test_runner::test_runner(QObject *parent)
10+
: QObject(parent)
11+
{
12+
class Container;
13+
14+
}
15+
16+
void test_runner::list_modified_correctly()
17+
{
18+
Container test_useful;
19+
POINT(list_modified_correctly, 3.1);
20+
21+
QList<int> list;
22+
QList<int> correctList;
23+
for (int i = 0; i < 1000; i++) {
24+
list.append(i);
25+
if (i % 5 == 0) {
26+
correctList.append(i*3);
27+
} else {
28+
correctList.append(i);
29+
}
30+
}
31+
list = test_useful.listPractise(list);
32+
for (int i = 0; i < 1000; i++) {
33+
QVERIFY(list[i] == correctList[i]);
34+
}
35+
}
36+
37+
void test_runner::correct_heights_returned()
38+
{
39+
Container test_useful;
40+
POINT(correct_heights_returned, 3.2);
41+
QMap<QString, float> map;
42+
map.insert("Mark", 1.82f);
43+
map.insert("Anton", 1.90f);
44+
map.insert("Liisa", 1.59f);
45+
map.insert("Anna", 1.64f);
46+
QList<float> correctHeights;
47+
correctHeights.append(1.64f);
48+
correctHeights.append(1.90f);
49+
QList<float> returned = test_useful.heights(map);
50+
QVERIFY(correctHeights.size() == returned.size());
51+
for (int i = 0; i < returned.size(); i++)
52+
QVERIFY(qFuzzyCompare(returned[i], correctHeights[i]));
53+
}
54+
55+
void test_runner::correct_names_returned()
56+
{
57+
Container test_useful;
58+
POINT(correct_names_returned, 3.3);
59+
QMap<QString, float> map;
60+
map.insert("Mark", 1.82f);
61+
map.insert("Anton", 1.90f);
62+
map.insert("Liisa", 1.59f);
63+
map.insert("Anna", 1.64f);
64+
QList<QString> correctNames;
65+
correctNames.append("Anton");
66+
correctNames.append("Mark");
67+
QList<QString> returned = test_useful.names(map);
68+
QVERIFY(correctNames.size() == returned.size());
69+
for (int i = 0; i < returned.size(); i++) {
70+
QVERIFY(returned[i] == correctNames[i]);
71+
}
72+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef TEST_RUNNER_H
2+
#define TEST_RUNNER_H
3+
4+
#include <QObject>
5+
6+
class test_runner : public QObject
7+
{
8+
9+
Q_OBJECT
10+
11+
public:
12+
explicit test_runner(QObject *parent = nullptr);
13+
14+
signals:
15+
16+
public slots:
17+
18+
private slots:
19+
void list_modified_correctly();
20+
void correct_heights_returned();
21+
void correct_names_returned();
22+
23+
};
24+
25+
#endif
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
QT += core testlib
2+
QT -= gui
3+
4+
CONFIG += c++11 console testcase
5+
CONFIG -= app_bundle
6+
7+
win32 {
8+
CONFIG -= debug_and_release debug_and_release_target
9+
}
10+
11+
DEFINES += QT_DEPRECATED_WARNINGS
12+
13+
SOURCES += main.cpp \
14+
test_runner.cpp \
15+
$$PWD/../src/container.cpp
16+
17+
HEADERS += \
18+
test_runner.h \
19+
$$PWD/../src/container.h
20+
21+
22+
INCLUDEPATH += $$PWD/../src
23+
24+

0 commit comments

Comments
 (0)