1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "runtime/arguments.hpp"
  26 #include "unittest.hpp"
  27 #include "utilities/globalDefinitions.hpp"
  28 
  29 TEST(arguments, atojulong) {
  30   char ullong_max[32];
  31   int ret = jio_snprintf(ullong_max, sizeof(ullong_max), JULONG_FORMAT, ULLONG_MAX);
  32   ASSERT_NE(-1, ret);
  33 
  34   julong value;
  35   const char* invalid_strings[] = {
  36     "", "-1", "-100", " 1", "2 ", "3 2", "1.0",
  37     "0x4.5", "0x", "0x0x1" "0.001", "4e10", "e"
  38     "K", "M", "G", "1MB", "1KM", "AA", "0B",
  39     "18446744073709551615K", "17179869184G",
  40     "999999999999999999999999999999"
  41   };
  42   for (uint i = 0; i < ARRAY_SIZE(invalid_strings); i++) {
  43     ASSERT_FALSE(Arguments::atojulong(invalid_strings[i], &value))
  44         << "Invalid string '" << invalid_strings[i] << "' parsed without error.";
  45   }
  46 
  47   struct {
  48     const char* str;
  49     julong expected_value;
  50   } valid_strings[] = {
  51       { "0", 0 },
  52       { "4711", 4711 },
  53       { "1K", 1ULL * K },
  54       { "1k", 1ULL * K },
  55       { "2M", 2ULL * M },
  56       { "2m", 2ULL * M },
  57       { "4G", 4ULL * G },
  58       { "4g", 4ULL * G },
  59       { "0K", 0 },
  60       { ullong_max, ULLONG_MAX },
  61       { "0xcafebabe", 0xcafebabe },
  62       { "0XCAFEBABE", 0xcafebabe },
  63       { "0XCAFEbabe", 0xcafebabe },
  64       { "0x10K", 0x10 * K }
  65   };
  66   for (uint i = 0; i < ARRAY_SIZE(valid_strings); i++) {
  67     ASSERT_TRUE(Arguments::atojulong(valid_strings[i].str, &value))
  68         << "Valid string '" << valid_strings[i].str << "' did not parse.";
  69     ASSERT_EQ(valid_strings[i].expected_value, value);
  70   }
  71 }