1 /* -*- Mode: c++ -*- */ 2 /*************************************************************************** 3 * bytesizeparsertest.cc 4 * 5 * Sun Mar 05 11:44:23 CET 2017 6 * Copyright 2017 Goran Mekić 7 * meka@tilda.center 8 ****************************************************************************/ 9 10 /* 11 * This file is part of DrumGizmo. 12 * 13 * DrumGizmo is free software; you can redistribute it and/or modify 14 * it under the terms of the GNU Lesser General Public License as published by 15 * the Free Software Foundation; either version 3 of the License, or 16 * (at your option) any later version. 17 * 18 * DrumGizmo is distributed in the hope that it will be useful, 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 * GNU Lesser General Public License for more details. 22 * 23 * You should have received a copy of the GNU Lesser General Public License 24 * along with DrumGizmo; if not, write to the Free Software 25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 26 */ 27 #include <uunit.h> 28 29 #include "bytesizeparser.h" 30 31 32 class ByteSizeParserTest 33 : public uUnit 34 { 35 public: ByteSizeParserTest()36 ByteSizeParserTest() 37 { 38 uUNIT_TEST(ByteSizeParserTest::suffixTest); 39 uUNIT_TEST(ByteSizeParserTest::falseSuffixTest); 40 uUNIT_TEST(ByteSizeParserTest::falseNumberTest); 41 uUNIT_TEST(ByteSizeParserTest::tooBigNumberTest); 42 } 43 suffixTest()44 void suffixTest() 45 { 46 std::size_t computed_size, expected_size; 47 std::size_t kilo = 1024, mega = kilo * 1024, giga = mega * 1024; 48 computed_size = byteSizeParser("3"); 49 expected_size = 3; 50 uUNIT_ASSERT_EQUAL(expected_size, computed_size); 51 52 computed_size = byteSizeParser("3k"); 53 expected_size = 3 * kilo; 54 uUNIT_ASSERT_EQUAL(expected_size, computed_size); 55 56 computed_size = byteSizeParser("3M"); 57 expected_size = 3 * mega; 58 uUNIT_ASSERT_EQUAL(expected_size, computed_size); 59 60 computed_size = byteSizeParser("3G"); 61 expected_size = 3 * giga; 62 uUNIT_ASSERT_EQUAL(expected_size, computed_size); 63 } 64 falseSuffixTest()65 void falseSuffixTest() 66 { 67 std::size_t computed_size, expected_size = 0; 68 computed_size = byteSizeParser("3K"); 69 uUNIT_ASSERT_EQUAL(expected_size, computed_size); 70 71 computed_size = byteSizeParser("3m"); 72 uUNIT_ASSERT_EQUAL(expected_size, computed_size); 73 74 computed_size = byteSizeParser("3g"); 75 uUNIT_ASSERT_EQUAL(expected_size, computed_size); 76 77 computed_size = byteSizeParser("3ddDD"); 78 uUNIT_ASSERT_EQUAL(expected_size, computed_size); 79 } 80 falseNumberTest()81 void falseNumberTest() 82 { 83 std::size_t computed_size, expected_size = 0; 84 computed_size = byteSizeParser("K3k"); 85 uUNIT_ASSERT_EQUAL(expected_size, computed_size); 86 87 computed_size = byteSizeParser("-3"); 88 uUNIT_ASSERT_EQUAL(expected_size, computed_size); 89 90 computed_size = byteSizeParser("-3k"); 91 uUNIT_ASSERT_EQUAL(expected_size, computed_size); 92 93 computed_size = byteSizeParser("-3M"); 94 uUNIT_ASSERT_EQUAL(expected_size, computed_size); 95 96 computed_size = byteSizeParser("-3G"); 97 uUNIT_ASSERT_EQUAL(expected_size, computed_size); 98 99 computed_size = byteSizeParser("3-"); 100 uUNIT_ASSERT_EQUAL(expected_size, computed_size); 101 102 computed_size = byteSizeParser("3-k"); 103 uUNIT_ASSERT_EQUAL(expected_size, computed_size); 104 105 computed_size = byteSizeParser("k-3"); 106 uUNIT_ASSERT_EQUAL(expected_size, computed_size); 107 108 computed_size = byteSizeParser("3-1"); 109 uUNIT_ASSERT_EQUAL(expected_size, computed_size); 110 111 computed_size = byteSizeParser(" -3"); 112 uUNIT_ASSERT_EQUAL(expected_size, computed_size); 113 } 114 tooBigNumberTest()115 void tooBigNumberTest() 116 { 117 std::size_t computed_size, expected_size = 0; 118 computed_size = byteSizeParser("999999999999999999999999999999999999G"); 119 uUNIT_ASSERT_EQUAL(expected_size, computed_size); 120 } 121 }; 122 123 // Registers the fixture into the 'registry' 124 static ByteSizeParserTest test; 125