| 1 | /* |
|---|
| 2 | Copyright (c) 2006-2018 Elmar Pruesse <elmar.pruesse@ucdenver.edu> |
|---|
| 3 | |
|---|
| 4 | This file is part of SINA. |
|---|
| 5 | SINA is free software: you can redistribute it and/or modify it under |
|---|
| 6 | the terms of the GNU General Public License as published by the Free |
|---|
| 7 | Software Foundation, either version 3 of the License, or (at your |
|---|
| 8 | option) any later version. |
|---|
| 9 | |
|---|
| 10 | SINA is distributed in the hope that it will be useful, but WITHOUT ANY |
|---|
| 11 | WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|---|
| 12 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|---|
| 13 | for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU General Public License |
|---|
| 16 | along with SINA. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 17 | |
|---|
| 18 | Additional permission under GNU GPL version 3 section 7 |
|---|
| 19 | |
|---|
| 20 | If you modify SINA, or any covered work, by linking or combining it |
|---|
| 21 | with components of ARB (or a modified version of that software), |
|---|
| 22 | containing parts covered by the terms of the |
|---|
| 23 | ARB-public-library-license, the licensors of SINA grant you additional |
|---|
| 24 | permission to convey the resulting work. Corresponding Source for a |
|---|
| 25 | non-source form of such a combination shall include the source code |
|---|
| 26 | for the parts of ARB used as well as that of the covered work. |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | # include "../progress.h" |
|---|
| 31 | |
|---|
| 32 | #define BOOST_TEST_MODULE progress |
|---|
| 33 | |
|---|
| 34 | #include <boost/test/unit_test.hpp> |
|---|
| 35 | #include <boost/test/data/test_case.hpp> |
|---|
| 36 | namespace utf = boost::unit_test; |
|---|
| 37 | namespace bdata = utf::data; |
|---|
| 38 | |
|---|
| 39 | #include <string> |
|---|
| 40 | #include <iostream> |
|---|
| 41 | #include "spdlog/spdlog.h" |
|---|
| 42 | |
|---|
| 43 | BOOST_AUTO_TEST_SUITE(progress_test); |
|---|
| 44 | |
|---|
| 45 | using namespace sina; |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | BOOST_TEST_DECORATOR(* utf::timeout(1)) |
|---|
| 49 | BOOST_DATA_TEST_CASE( |
|---|
| 50 | format_bar_to, |
|---|
| 51 | bdata::make({0, 1, 2, 3, 100, 10000}) * // totals |
|---|
| 52 | bdata::make({0u, 1u, 2u, 3u, 10u, 100u}) * // widths |
|---|
| 53 | bdata::make({0., 0.001, 0.01, .1, .5, .51, .9, .99, .999, 1.}), // fills |
|---|
| 54 | total, width, fill) |
|---|
| 55 | { |
|---|
| 56 | int expected_full = fill * width + 1./12; |
|---|
| 57 | int expected_remain = (fill * width - expected_full) * 12; |
|---|
| 58 | |
|---|
| 59 | FILE *devnull = fopen("/dev/null", "w"); |
|---|
| 60 | Progress p("description", total, /*ascii = */true, /*file = */devnull); |
|---|
| 61 | |
|---|
| 62 | fmt::memory_buffer buf; |
|---|
| 63 | p.format_bar_to(buf, width, fill); |
|---|
| 64 | BOOST_CHECK_EQUAL(buf.size(), width); |
|---|
| 65 | BOOST_CHECK_EQUAL(std::count(buf.begin(), buf.end(), '#'), expected_full); |
|---|
| 66 | BOOST_CHECK_EQUAL(std::count(buf.begin(), buf.end(), char('0' + expected_remain - 1)) |
|---|
| 67 | , expected_remain?1:0); |
|---|
| 68 | fclose(devnull); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | BOOST_DATA_TEST_CASE( |
|---|
| 72 | show_progress, |
|---|
| 73 | bdata::make({60u,43u,42u}) * |
|---|
| 74 | bdata::make({10,50,99}), |
|---|
| 75 | width, n) |
|---|
| 76 | { |
|---|
| 77 | fmt::memory_buffer buf; |
|---|
| 78 | buf.resize(500); |
|---|
| 79 | FILE * filebuf = fmemopen(buf.begin(), buf.size(), "w"); |
|---|
| 80 | |
|---|
| 81 | int total = 100; |
|---|
| 82 | Progress p("text", total, /*ascii=*/true, filebuf, width); |
|---|
| 83 | p.update(n); |
|---|
| 84 | fseek(filebuf, 0, SEEK_SET); |
|---|
| 85 | std::fill_n(buf.begin(), buf.size(), 0); |
|---|
| 86 | p.show_progress(); |
|---|
| 87 | fflush(filebuf); |
|---|
| 88 | std::string result(buf.begin()); |
|---|
| 89 | BOOST_TEST_INFO("result: '" << result << "'"); |
|---|
| 90 | std::string beg = fmt::format("text: {}% |", n); |
|---|
| 91 | std::string end = fmt::format("| {}/100 [00:00:00 / 00:00:00]\n\x1b[A", n); |
|---|
| 92 | BOOST_TEST_INFO("expected begin: '" << beg << "'"); |
|---|
| 93 | BOOST_TEST_INFO("expected end: '" << end << "'"); |
|---|
| 94 | BOOST_REQUIRE_LE(result.size(), width+4); |
|---|
| 95 | BOOST_REQUIRE_GE(result.size(), beg.size() + end.size()); |
|---|
| 96 | BOOST_CHECK_EQUAL(result.substr(0, beg.size()), beg); |
|---|
| 97 | BOOST_CHECK_EQUAL(result.substr(result.size()-end.size(), end.size()), end); |
|---|
| 98 | |
|---|
| 99 | fclose(filebuf); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | BOOST_AUTO_TEST_SUITE_END(); // progress_test |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | /* |
|---|
| 106 | Local Variables: |
|---|
| 107 | mode:c++ |
|---|
| 108 | c-file-style:"stroustrup" |
|---|
| 109 | c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) |
|---|
| 110 | indent-tabs-mode:nil |
|---|
| 111 | fill-column:99 |
|---|
| 112 | End: |
|---|
| 113 | */ |
|---|
| 114 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : |
|---|