Skip to content

Commit

Permalink
[ProgressBar] Add progress bar unit tests
Browse files Browse the repository at this point in the history
Other changes:
* Disable exception which is thrown when the screen size is not
  enough (since it brokes the tests).
* Add BSD license.
  • Loading branch information
geektoni authored and vigsterkr committed Jun 2, 2017
1 parent 01c5aa5 commit 176fdd0
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 16 deletions.
71 changes: 58 additions & 13 deletions src/shogun/base/progress.h
@@ -1,5 +1,36 @@
/*
* BSD 3-Clause License
*
* Copyright (c) 2017, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Written (W) 2017 Giovanni De Toni
*
*/

#ifndef __SG_PROGRESS_H__
Expand Down Expand Up @@ -100,9 +131,11 @@ namespace shogun
// time displayed (decimals and integer).
int32_t progress_bar_space =
(m_columns_num - 50 - m_prefix.length()) * 0.9;
REQUIRE(
progress_bar_space > 0,
"Not enough terminal space to show the progress bar!\n")

// TODO: this guy here brokes testing
// REQUIRE(
// progress_bar_space > 0,
// "Not enough terminal space to show the progress bar!\n")

char str[1000];
float64_t runtime = CTime::get_curtime();
Expand All @@ -112,7 +145,7 @@ namespace shogun
(m_max_value - m_min_value + 1);

// Set up chunk size
size_chunk = difference / (double_t)progress_bar_space;
size_chunk = difference / (float64_t)progress_bar_space;

if (m_last_progress == 0)
{
Expand Down Expand Up @@ -201,8 +234,13 @@ namespace shogun
print_progress(m_max_value);
}

private:
/** @return last progress as a percentage */
inline float64_t get_last_progress() const
{
return m_last_progress;
}

private:
std::string get_pb_char() const
{
switch (m_mode)
Expand Down Expand Up @@ -233,6 +271,10 @@ namespace shogun

/** IO object */
SGIO m_io;
/** Maxmimum value */
float64_t m_max_value;
/** Minimum value */
float64_t m_min_value;
/** Prefix which will be printed before the progress bar */
std::string m_prefix;
/** Progres bar's char mode */
Expand All @@ -245,16 +287,12 @@ namespace shogun
mutable int32_t m_columns_num;
/* Screen row number*/
mutable int32_t m_rows_num;
/** Maxmimum value */
float64_t m_max_value;
/** Minimum value */
float64_t m_min_value;
/** Last progress */
mutable float64_t m_last_progress;
/** Last progress time */
mutable float64_t m_last_progress_time;
/** Progress start time */
mutable float64_t m_progress_start_time;
/** Last progress */
mutable float64_t m_last_progress;
};

/** @class Helper class to show a progress bar given a range.
Expand Down Expand Up @@ -373,6 +411,12 @@ namespace shogun
return PIterator(m_range.end(), m_printer);
}

/** @return last progress as a percentage */
inline float64_t get_last_progress() const
{
return m_printer->get_last_progress();
}

private:
void set_up_range()
{
Expand All @@ -398,7 +442,8 @@ namespace shogun
* @param io SGIO object
*/
template <typename T>
inline PRange<T> progress(Range<T> range, const SGIO& io, SG_PRG_MODE mode=UTF8)
inline PRange<T>
progress(Range<T> range, const SGIO& io, SG_PRG_MODE mode = UTF8)
{
return PRange<T>(range, io, mode);
}
Expand All @@ -412,7 +457,7 @@ namespace shogun
* @param range range used
*/
template <typename T>
inline PRange<T> progress(Range<T> range, SG_PRG_MODE mode=UTF8)
inline PRange<T> progress(Range<T> range, SG_PRG_MODE mode = UTF8)
{
return PRange<T>(range, *sg_io, mode);
}
Expand Down
35 changes: 35 additions & 0 deletions src/shogun/base/range.h
@@ -1,3 +1,38 @@
/*
* BSD 3-Clause License
*
* Copyright (c) 2017, Shogun-Toolbox e.V. <shogun-team@shogun-toolbox.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Written (W) 2016 Sergey Lisitsyn
*
*/

#ifndef __SG_RANGE_H__
#define __SG_RANGE_H__

Expand Down
115 changes: 115 additions & 0 deletions tests/unit/base/PRange_unittest.cc
@@ -0,0 +1,115 @@
#include <cmath>
#include <gtest/gtest.h>
#include <shogun/base/progress.h>
#include <shogun/io/SGIO.h>
#include <thread>

using namespace shogun;

SGIO range_io;
const int millis = 10;
auto range_test = progress(range(0, 100), range_io);

TEST(PRange, basic_upper)
{
int other_i = 0;
int count = 10;
for (auto i : progress(range(count), range_io))
{
EXPECT_EQ(i, other_i);
other_i++;
}
EXPECT_EQ(count, other_i);
}

TEST(PRange, basic_lower_upper)
{
int count = 10;
int start = std::rand();
int other_i = start;
for (auto i : progress(range(start, start + count), range_io))
{
EXPECT_EQ(i, other_i);
other_i++;
}
EXPECT_EQ(start + count, other_i);
}

TEST(PRange, zero)
{
int actual_count = 0;
int count = 0;
for (auto i : progress(range(count), range_io))
{
(void)i;
actual_count++;
}
EXPECT_EQ(count, actual_count);
}

TEST(PRange, identical_bounds)
{
int actual_count = 0;
int b = std::rand();
for (auto i : progress(range(b, b), range_io))
{
(void)i;
actual_count++;
}
EXPECT_EQ(0, actual_count);
}

TEST(PRange, progress_correct_bounds_positive)
{
range_io.enable_progress();
range_test = progress(range(0, 100), range_io);
for (auto i : range_test)
{
std::this_thread::sleep_for(std::chrono::milliseconds(millis));
EXPECT_EQ(std::ceil(range_test.get_last_progress()), i);
}
}

TEST(PRange, progress_correct_bounds_negative)
{
range_io.enable_progress();
range_test = progress(range(-100, 0), range_io);
for (auto i : range_test)
{
std::this_thread::sleep_for(std::chrono::milliseconds(millis));
EXPECT_EQ(std::ceil(range_test.get_last_progress()), (100 + i));
}
}

TEST(PRange, DISABLED_progress_incorrect_bounds_positive)
{
range_io.enable_progress();
range_test = progress(range(100, 0), range_io);
for (auto i : range_test)
{
std::this_thread::sleep_for(std::chrono::milliseconds(millis));
}
EXPECT_FLOAT_EQ(range_test.get_last_progress(), (float64_t)0);
}

TEST(PRange, DISABLED_progress_incorrect_bounds_negative)
{
range_io.enable_progress();
range_test = progress(range(100, 0), range_io);
for (auto i : range_test)
{
std::this_thread::sleep_for(std::chrono::milliseconds(millis));
}
EXPECT_FLOAT_EQ(range_test.get_last_progress(), (float64_t)0);
}

TEST(PRange, DISABLED_progress_incorrect_bounds_equal)
{
range_io.enable_progress();
range_test = progress(range(1, 1), range_io);
for (auto i : range_test)
{
std::this_thread::sleep_for(std::chrono::milliseconds(millis));
}
EXPECT_FLOAT_EQ(range_test.get_last_progress(), (float64_t)0);
}
3 changes: 0 additions & 3 deletions tests/unit/base/Range_unittest.cc
@@ -1,8 +1,6 @@
#include <shogun/base/range.h>
#include <shogun/lib/config.h>
#include <gtest/gtest.h>

#ifdef HAVE_CXX11
using namespace shogun;

TEST(Range, basic_upper)
Expand Down Expand Up @@ -53,4 +51,3 @@ TEST(Range, identical_bounds)
}
EXPECT_EQ(0, actual_count);
}
#endif

0 comments on commit 176fdd0

Please sign in to comment.