Skip to content

Commit

Permalink
Add SGIO::progress unit tests and enhance SGIO::progress.
Browse files Browse the repository at this point in the history
Add a getter to retrieve the last_progress (for testing purpose).
  • Loading branch information
geektoni committed Mar 16, 2017
1 parent 05777e0 commit b128f0e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/shogun/io/SGIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ char SGIO::directory_name[FBUFSIZE];

SGIO::SGIO()
: target(stdout), last_progress_time(0), progress_start_time(0),
last_progress(-1), show_progress(false), location_info(MSG_NONE),
last_progress(0), show_progress(false), location_info(MSG_NONE),
syntax_highlight(true), loglevel(MSG_WARN)
{
m_refcount = new RefCount();
}

SGIO::SGIO(const SGIO& orig)
: target(orig.get_target()), last_progress_time(0),
progress_start_time(0), last_progress(1),
progress_start_time(0), last_progress(0),
show_progress(orig.get_show_progress()),
location_info(orig.get_location_info()),
syntax_highlight(orig.get_syntax_highlight()),
Expand Down Expand Up @@ -166,21 +166,23 @@ void SGIO::progress(

if (max_val-min_val>0.0)
v=100*(current_val-min_val+1)/(max_val-min_val+1);
else
return;

v=CMath::clamp(v,1e-5,100.0);

if (decimals < 1)
decimals = 1;

if (last_progress==-1)
if (last_progress==0)
{
last_progress_time = runtime;
progress_start_time = runtime;
last_progress = v;
}
else
{
v=CMath::clamp(v,1e-5,100.0);
last_progress = v-1e-6;

if ((v!=100.0) && (runtime - last_progress_time<0.5))
{

Expand All @@ -189,6 +191,7 @@ void SGIO::progress(
if (current_val >= max_val-1)
{
v = 100;
last_progress=v-1e-6;
snprintf(str, sizeof(str), "%%s %%%d.%df%%%% %%1.1f seconds remaining %%1.1f seconds total ",decimals+3, decimals);
message(MSG_MESSAGEONLY, "", "", -1, str, prefix, v, estimate, total_estimate);
message(MSG_MESSAGEONLY, "", "", -1, "\n");
Expand Down
6 changes: 6 additions & 0 deletions src/shogun/io/SGIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,12 @@ class SGIO
return location_info;
}

/** @return last progress as percentage */
inline float64_t get_last_progress() const
{
return last_progress;
}

/** get syntax highlight
*
* @return if syntax highlighting is enabled
Expand Down
72 changes: 72 additions & 0 deletions tests/unit/io/SGIO_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <cmath>
#include <thread>
#include <chrono>
#include <shogun/io/SGIO.h>

#include <gtest/gtest.h>

using namespace shogun;

const int millis= 10;

TEST(SGIOTest, progress_correct_bounds_positive)
{
SGIO tmp;
tmp.enable_progress();
for (int i=0; i<100; i++) {
std::this_thread::sleep_for(std::chrono::milliseconds(millis));
tmp.progress(i, 0, 100);
EXPECT_EQ(std::ceil(tmp.get_last_progress()), i+1);
}
}

TEST(SGIOTest, progress_correct_bounds_negative)
{
SGIO tmp2;
tmp2.enable_progress();
for (int i=-100; i>0; i++) {
std::this_thread::sleep_for(std::chrono::milliseconds(millis));
tmp2.progress(i, -100, 0);
EXPECT_EQ(std::ceil(tmp2.get_last_progress()), i+1);
}
}

TEST(SGIOTest, progress_incorrect_bounds_positive)
{
SGIO tmp2;
tmp2.enable_progress();
tmp2.progress(0, 100, 1);
EXPECT_EQ(tmp2.get_last_progress(), (float64_t)0);
}

TEST(SGIOTest, progress_incorrect_bounds_negative)
{
SGIO tmp;
tmp.enable_progress();
tmp.progress(0, -1, -2);
EXPECT_EQ(tmp.get_last_progress(), (float64_t)0);
}

TEST(SGIOTest, progress_incorrect_bounds_equal)
{
SGIO tmp3;
tmp3.enable_progress();
tmp3.progress(0, 1, 1);
EXPECT_EQ(tmp3.get_last_progress(), (float64_t)0);
}

TEST(SGIOTest, progress_current_val_out_of_bounds_lower)
{
SGIO tmp;
tmp.enable_progress();
tmp.progress(-1, 0, 100);
EXPECT_EQ(tmp.get_last_progress(), (float64_t)1e-5);
}

TEST(SGIOTest, progress_current_val_out_of_bounds_higher)
{
SGIO tmp2;
tmp2.enable_progress();
tmp2.progress(1001, 0, 100);
EXPECT_EQ(tmp2.get_last_progress(), (float64_t)100);
}

0 comments on commit b128f0e

Please sign in to comment.