Commit 0f66bd98 authored by Samikshan Bairagya's avatar Samikshan Bairagya

Merge branch 'master' into gsoc2012-samxan

Conflicts:
	kstars/CMakeLists.txt
parents 4f59db70 07c5336f
......@@ -18,7 +18,7 @@ macro_bool_to_01(CFITSIO_FOUND HAVE_CFITSIO_H)
macro_log_feature(CFITSIO_FOUND "libcfitsio0" "Support for the FITS (Flexible Image Transport System) data format in KStars." "http://indi.sf.net" FALSE "3.09" "")
# INDI is a Linux-specific addon
set(INDI_MINIMUM_VERSION 0.9.5)
set(INDI_MINIMUM_VERSION 0.9.6)
IF(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
macro_optional_find_package(INDI ${INDI_MINIMUM_VERSION})
macro_bool_to_01(INDI_FOUND HAVE_INDI_H)
......@@ -38,6 +38,9 @@ macro_bool_to_01(OPENGL_FOUND HAVE_OPENGL)
macro_log_feature(OPENGL_FOUND "OpenGL" "Support for hardware rendering in KStars." "http://www.opengl.org/" FALSE "" "OpenGL rendering is generally faster on hardware with graphics acceleration.")
add_definitions(-DQT_USE_FAST_CONCATENATION -DQT_USE_FAST_OPERATOR_PLUS)
enable_testing(true)
if (EIGEN2_FOUND)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config-kstars.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-kstars.h )
......@@ -47,6 +50,12 @@ if (EIGEN2_FOUND)
endif (EIGEN2_FOUND)
add_subdirectory(datahandlers)
add_subdirectory(Tests)
if (${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_SOURCE_DIR})
macro_display_feature_log()
endif (${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_SOURCE_DIR})
include_directories( ${kstars_SOURCE_DIR} )
# QT4_AUTOMOC( ${kstars_SRCS} )
# add_library( KStarsAll SHARED ${kstars_extra_SRCS})
FIND_PACKAGE(Qt4 REQUIRED)
SET( TEST_LIBRARIES ${QT_QTTEST_LIBRARY} ${QT_LIBRARIES}
LibKSDataHandlers
${KDE4_KDECORE_LIBS}
${KDE4_KNEWSTUFF3_LIBS}
${KDE4_KIO_LIBS}
htmesh
${ZLIB_LIBRARIES}
${QT_QTSQL_LIBRARY}
KStarsLib
)
SET( KSParserTests_SRCS testcsvparser.cpp testfwparser.cpp )
QT4_AUTOMOC( ${KSParserTests_SRCS} )
QT4_AUTOMOC( testfwparser.cpp )
ADD_EXECUTABLE( testcsvparser testcsvparser.cpp )
TARGET_LINK_LIBRARIES( testcsvparser ${TEST_LIBRARIES} ${QT_QTTEST_LIBRARY})
ADD_TEST( NAME CSVParserTest COMMAND testcsvparser )
ADD_EXECUTABLE( testfwparser testfwparser.cpp )
TARGET_LINK_LIBRARIES( testfwparser ${TEST_LIBRARIES} ${QT_QTTEST_LIBRARY})
ADD_TEST( NAME FixedWidthParserTest COMMAND testfwparser )
\ No newline at end of file
/***************************************************************************
TestCSVParser.cpp - K Desktop Planetarium
-------------------
begin : 2012/23/07
copyright : (C) 2012 by Rishab Arora
email : ra.rishab@gmail.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/*
* Justification for not testing CombineQuoteParts separately:
* Changing the structure of KSParser would solve this (by removing the
* segments to be tested into separate classes) but would unnecessarily
* complicate things resulting in a large number of small classes.
* This is good from an OOD perspective, but would make the code unmanageable
*/
#include "testcsvparser.h"
TestCSVParser::TestCSVParser(): QObject() {
/*
* Justification for doing this instead of simply creating a file:
* To add/change tests, we'll need to modify 2 places. The file and this class.
* So we write the file from the class.
*/
test_cases_.append("\n");
test_cases_.append(QString(","
"isn't,"
"it,"
"\"amusing\","
"how,"
"3,"
"\"isn't, pi\","
"and,"
"\"\","
"-3.141,"
"isn't,"
"either\n"));
test_cases_.append(QString(","
"isn't,"
"it,"
"\"amusing\","
"how,"
"3,"
"\"isn't, pi\","
"and,"
"\"\",")); // less than required fields
test_cases_.append(QString(","
"isn't,"
"it,"
"\"amusing\","
"how,"
"3,"
"\"isn't, pi\","
"and,"
"\"," // no matching "
"-3.141,"
"isn't,"
"either\n"));
test_cases_.append(",,,,,,,,,,,\n");
test_cases_.append("\n");
QString file_name("TestCSV.txt");
file_name = KStandardDirs::locateLocal("appdata", file_name);
if (!file_name.isNull()) {
test_csv_file_.setFileName(file_name);
if (!test_csv_file_.open(QIODevice::WriteOnly)) {
kWarning() << QString("Couldn't open(%1)").arg(file_name);
}
}
QTextStream out_stream(&test_csv_file_);
foreach(const QString &test_case, test_cases_)
out_stream << test_case;
test_csv_file_.close();
//Building the sequence to be used. Includes all available types.
sequence_.clear();
sequence_.append(qMakePair(QString("field1"), KSParser::D_QSTRING));
sequence_.append(qMakePair(QString("field2"), KSParser::D_QSTRING));
sequence_.append(qMakePair(QString("field3"), KSParser::D_QSTRING));
sequence_.append(qMakePair(QString("field4"), KSParser::D_QSTRING));
sequence_.append(qMakePair(QString("field5"), KSParser::D_QSTRING));
sequence_.append(qMakePair(QString("field6"), KSParser::D_INT));
sequence_.append(qMakePair(QString("field7"), KSParser::D_QSTRING));
sequence_.append(qMakePair(QString("field8"), KSParser::D_QSTRING));
sequence_.append(qMakePair(QString("field9"), KSParser::D_QSTRING));
sequence_.append(qMakePair(QString("field10"), KSParser::D_FLOAT));
sequence_.append(qMakePair(QString("field11"), KSParser::D_QSTRING));
sequence_.append(qMakePair(QString("field12"), KSParser::D_QSTRING));
QString fname = KStandardDirs::locate( "appdata", file_name );
test_parser_ = new KSParser(fname, '#', sequence_);
}
TestCSVParser::~TestCSVParser()
{
delete test_parser_;
}
/*
* The following tests checks for the following cases for CSV files
* 1. Mixed inputs (See test case for description)
* 2. Empty Row
* 3. No row (only a newline character)
* 4. Truncated row
* 5. Row with no matching quote
* 6. Attempt to read missing file
*
*/
void TestCSVParser::CSVMixedInputs() {
/*
* Test 1. Includes input of the form:
*
* It starts with a newline char which should be skipped by virtue
* of the design of the parser
*
* Then a row with the following types of inputs:
* 1. empty column
* 2. simple single word
* 3. single word in quotes
* 4. multiple words with , in quotes
* 5. integer
* 6. float
*/
QHash<QString, QVariant> row_content = test_parser_->ReadNextRow();
qDebug() << row_content["field1"];
QVERIFY(row_content["field1"] == QString(""));
QVERIFY(row_content["field2"] == QString("isn't"));
QVERIFY(row_content["field3"] == QString("it"));
QVERIFY(row_content["field4"] == QString("amusing"));
QVERIFY(row_content["field5"] == QString("how"));
QVERIFY(row_content["field6"].toInt() == 3);
QVERIFY(row_content["field7"] == QString("isn't, pi"));
QVERIFY(row_content["field8"] == QString("and"));
QVERIFY(row_content["field9"] == QString(""));
QVERIFY(row_content["field10"].toFloat() + 3.141 < 0.1);
QVERIFY(row_content["field11"] == QString("isn't"));
QVERIFY(row_content["field12"] == QString("either"));
}
void TestCSVParser::CSVEmptyRow() {
/* Test 2. Row with less rows than required (to be skipped)
* Test 3. Row with truncated \" i.e. no matching "
* (should be skipped)
*/
/*
* Test 4. Attempt to read an empty but valid row
*
* Also includes test for:
* 1. missing integer
* 2. missing float
* 3. missing string
*/
QHash<QString, QVariant> row_content = test_parser_->ReadNextRow();
qDebug() << row_content["field1"];
QVERIFY(row_content["field1"] == QString(""));
QVERIFY(row_content["field2"] == QString(""));
QVERIFY(row_content["field3"] == QString(""));
QVERIFY(row_content["field4"] == QString(""));
QVERIFY(row_content["field5"] == QString(""));
QVERIFY(row_content["field6"].toInt() == 0);
QVERIFY(row_content["field7"] == QString(""));
QVERIFY(row_content["field8"] == QString(""));
QVERIFY(row_content["field9"] == QString(""));
QVERIFY(row_content["field10"].toFloat() == 0.0);
QVERIFY(row_content["field11"] == QString(""));
QVERIFY(row_content["field12"] == QString(""));
}
void TestCSVParser::CSVNoRow() {
/*
* Test 3. Attempt to read a newline char instead of a row
* The parser is designed to skip an empty row so we can
* test this for a boundary case. i.e. newline at the end.
*/
QHash<QString, QVariant> row_content = test_parser_->ReadNextRow();
qDebug() << row_content["field1"];
QVERIFY(row_content["field1"] == QString("Null"));
QVERIFY(row_content["field2"] == QString("Null"));
QVERIFY(row_content["field3"] == QString("Null"));
QVERIFY(row_content["field4"] == QString("Null"));
QVERIFY(row_content["field5"] == QString("Null"));
QVERIFY(row_content["field6"].toInt() == 0);
QVERIFY(row_content["field7"] == QString("Null"));
QVERIFY(row_content["field8"] == QString("Null"));
QVERIFY(row_content["field9"] == QString("Null"));
QVERIFY(row_content["field10"].toFloat() == 0.0);
QVERIFY(row_content["field11"] == QString("Null"));
QVERIFY(row_content["field12"] == QString("Null"));
}
void TestCSVParser::CSVIgnoreHasNextRow() {
QHash<QString, QVariant> row_content;
for (int times = 0; times < 20; times++) {
row_content = test_parser_->ReadNextRow();
QVERIFY(row_content["field1"] == QString("Null"));
QVERIFY(row_content["field2"] == QString("Null"));
QVERIFY(row_content["field3"] == QString("Null"));
QVERIFY(row_content["field4"] == QString("Null"));
QVERIFY(row_content["field5"] == QString("Null"));
QVERIFY(row_content["field6"].toInt() == 0);
QVERIFY(row_content["field7"] == QString("Null"));
QVERIFY(row_content["field8"] == QString("Null"));
QVERIFY(row_content["field9"] == QString("Null"));
QVERIFY(row_content["field10"].toFloat() == 0.0);
QVERIFY(row_content["field11"] == QString("Null"));
QVERIFY(row_content["field12"] == QString("Null"));
}
}
void TestCSVParser::CSVReadMissingFile() {
/*
* Test 6. Attempt to read a missing file repeatedly
*/
QFile::remove(KStandardDirs::locateLocal("appdata","TestCSV.txt"));
KSParser missing_parser(QString("TestCSV.txt"), '#', sequence_);
QHash<QString, QVariant> row_content = missing_parser.ReadNextRow();
for (int times = 0; times < 20; times++) {
row_content = missing_parser.ReadNextRow();
QVERIFY(row_content["field1"] == QString("Null"));
QVERIFY(row_content["field2"] == QString("Null"));
QVERIFY(row_content["field3"] == QString("Null"));
QVERIFY(row_content["field4"] == QString("Null"));
QVERIFY(row_content["field5"] == QString("Null"));
QVERIFY(row_content["field6"].toInt() == 0);
QVERIFY(row_content["field7"] == QString("Null"));
QVERIFY(row_content["field8"] == QString("Null"));
QVERIFY(row_content["field9"] == QString("Null"));
QVERIFY(row_content["field10"].toFloat() == 0.0);
QVERIFY(row_content["field11"] == QString("Null"));
QVERIFY(row_content["field12"] == QString("Null"));
}
}
QTEST_MAIN(TestCSVParser)
#include "testcsvparser.moc"
/***************************************************************************
TestCSVParser.h - K Desktop Planetarium
-------------------
begin : 2012/23/07
copyright : (C) 2012 by Rishab Arora
email : ra.rishab@gmail.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef TESTCSVPARSER_H
#define TESTCSVPARSER_H
#include <QtTest/QtTest>
#include <KDebug>
#include "datahandlers/ksparser.h"
#include "kstars/ksfilereader.h"
class TestCSVParser: public QObject {
Q_OBJECT
public:
TestCSVParser();
~TestCSVParser();
private slots:
void CSVMixedInputs();
void CSVEmptyRow();
void CSVNoRow();
void CSVIgnoreHasNextRow();
void CSVReadMissingFile();
private:
QStringList test_cases_;
QList< QPair<QString, KSParser::DataTypes> > sequence_;
QFile test_csv_file_;
KSParser *test_parser_;
};
#endif // TESTKSPARSER_H
/***************************************************************************
TestFWParser.cpp - K Desktop Planetarium
-------------------
begin : 2012/24/07
copyright : (C) 2012 by Rishab Arora
email : ra.rishab@gmail.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "testfwparser.h"
TestFWParser::TestFWParser(): QObject() {
test_cases_.append(
"this is an exam ple of 256 cases being tested -3.14 times\n");
test_cases_.append(
" \n");
test_cases_.append("this is an ex\n\n");
QString file_name("TestFW.txt");
file_name = KStandardDirs::locateLocal("appdata", file_name);
if (!file_name.isNull()) {
test_file_.setFileName(file_name);
if (!test_file_.open(QIODevice::WriteOnly)) {
kWarning() << QString("Couldn't open(%1)").arg(file_name);
}
}
QTextStream out_stream(&test_file_);
foreach(const QString &test_case, test_cases_)
out_stream << test_case;
test_file_.close();
//Building the sequence to be used. Includes all available types.
sequence_.clear();
sequence_.append(qMakePair(QString("field1"), KSParser::D_QSTRING));
sequence_.append(qMakePair(QString("field2"), KSParser::D_QSTRING));
sequence_.append(qMakePair(QString("field3"), KSParser::D_QSTRING));
sequence_.append(qMakePair(QString("field4"), KSParser::D_QSTRING));
sequence_.append(qMakePair(QString("field5"), KSParser::D_QSTRING));
sequence_.append(qMakePair(QString("field6"), KSParser::D_INT));
sequence_.append(qMakePair(QString("field7"), KSParser::D_QSTRING));
sequence_.append(qMakePair(QString("field8"), KSParser::D_QSTRING));
sequence_.append(qMakePair(QString("field9"), KSParser::D_QSTRING));
sequence_.append(qMakePair(QString("field10"), KSParser::D_FLOAT));
sequence_.append(qMakePair(QString("field11"), KSParser::D_QSTRING));
sequence_.append(qMakePair(QString("field12"), KSParser::D_QSTRING));
widths_.append(5);
widths_.append(3);
widths_.append(3);
widths_.append(9);
widths_.append(3);
widths_.append(4);
widths_.append(6);
widths_.append(6);
widths_.append(7);
widths_.append(6);
widths_.append(6); //'repeatedly' doesn't need a width
QString fname = KStandardDirs::locate( "appdata", file_name );
test_parser_ = new KSParser(fname, '#', sequence_, widths_);
}
TestFWParser::~TestFWParser()
{
delete test_parser_;
}
void TestFWParser::MixedInputs() {
/*
* Test 1: Checks all conversions are working as expected
*/
QHash<QString, QVariant> row_content = test_parser_->ReadNextRow();
QVERIFY(row_content["field1"] == QString("this"));
QVERIFY(row_content["field2"] == QString("is"));
QVERIFY(row_content["field3"] == QString("an"));
QVERIFY(row_content["field4"] == QString("exam ple"));
QVERIFY(row_content["field5"] == QString("of"));
QVERIFY(row_content["field6"].toInt() == 256);
QVERIFY(row_content["field7"] == QString("cases"));
QVERIFY(row_content["field8"] == QString("being"));
QVERIFY(row_content["field9"] == QString("tested"));
QVERIFY(row_content["field10"].toFloat() + 3.141 < 0.1);
QVERIFY(row_content["field11"] == QString(""));
QVERIFY(row_content["field12"] == QString("times"));
}
void TestFWParser::OnlySpaceRow() {
/*
* Test 2: Checks what happens in case of reading an empty space row
*/
QHash<QString, QVariant> row_content = test_parser_->ReadNextRow();
QVERIFY(row_content["field1"] == QString(""));
QVERIFY(row_content["field2"] == QString(""));
QVERIFY(row_content["field3"] == QString(""));
QVERIFY(row_content["field4"] == QString(""));
QVERIFY(row_content["field5"] == QString(""));
QVERIFY(row_content["field6"].toInt() == 0);
QVERIFY(row_content["field7"] == QString(""));
QVERIFY(row_content["field8"] == QString(""));
QVERIFY(row_content["field9"] == QString(""));
QVERIFY(row_content["field10"].toFloat() == 0.0);
QVERIFY(row_content["field11"] == QString(""));
QVERIFY(row_content["field12"] == QString(""));
}
void TestFWParser::NoRow() {
/*
* Test 3:
* This test also tests what happens if we have a partial row or a
* truncated row. It is simply skipped.
*
* It then reaches a point where the file ends.
* We attempt reading a file after EOF 20 times
*/
QHash<QString, QVariant> row_content;
qDebug() << row_content["field12"];
for (int times = 0; times < 20; times++) {
row_content = test_parser_->ReadNextRow();
QVERIFY(row_content["field1"] == QString("Null"));
QVERIFY(row_content["field2"] == QString("Null"));
QVERIFY(row_content["field3"] == QString("Null"));
QVERIFY(row_content["field4"] == QString("Null"));
QVERIFY(row_content["field5"] == QString("Null"));
QVERIFY(row_content["field6"].toInt() == 0);
QVERIFY(row_content["field7"] == QString("Null"));
QVERIFY(row_content["field8"] == QString("Null"));
QVERIFY(row_content["field9"] == QString("Null"));
QVERIFY(row_content["field10"].toFloat() == 0.0);
QVERIFY(row_content["field11"] == QString("Null"));
QVERIFY(row_content["field12"] == QString("Null"));
}
}
void TestFWParser::FWReadMissingFile()
{
/*
* Test 4:
* This tests how the parser reacts if there is no file with the
* given path.
*/
QFile::remove(KStandardDirs::locateLocal("appdata","TestFW.txt"));
KSParser missing_parser(QString("TestFW.txt"), '#', sequence_, widths_);
QHash<QString, QVariant> row_content = missing_parser.ReadNextRow();
for (int times = 0; times < 20; times++) {
row_content = missing_parser.ReadNextRow();
QVERIFY(row_content["field1"] == QString("Null"));
QVERIFY(row_content["field2"] == QString("Null"));
QVERIFY(row_content["field3"] == QString("Null"));
QVERIFY(row_content["field4"] == QString("Null"));
QVERIFY(row_content["field5"] == QString("Null"));
QVERIFY(row_content["field6"].toInt() == 0);
QVERIFY(row_content["field7"] == QString("Null"));
QVERIFY(row_content["field8"] == QString("Null"));
QVERIFY(row_content["field9"] == QString("Null"));
QVERIFY(row_content["field10"].toFloat() == 0.0);
QVERIFY(row_content["field11"] == QString("Null"));
QVERIFY(row_content["field12"] == QString("Null"));
}
}
QTEST_MAIN(TestFWParser)
#include "testfwparser.moc"
\ No newline at end of file
/***************************************************************************
KSParser.cpp - K Desktop Planetarium
-------------------
begin : 2012/15/08
copyright : (C) 2012 by Rishab Arora
email : ra.rishab@gmail.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef TESTFWPARSER_H
#define TESTFWPARSER_H
#include <QtTest/QtTest>
#include <KDebug>
#include "datahandlers/ksparser.h"
#include "kstars/ksfilereader.h"
class TestFWParser: public QObject {
Q_OBJECT
public:
TestFWParser();
~TestFWParser();
private slots:
void MixedInputs();
void OnlySpaceRow();
void NoRow();
void FWReadMissingFile();
private:
QStringList test_cases_;
QList<int> widths_;
QList< QPair<QString, KSParser::DataTypes> > sequence_;
QFile test_file_;
KSParser *test_parser_;
};