1 /*
   2  * Copyright (c) 2016, 2017, 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 "prims/jvm.h"
  26 #include "runtime/arguments.hpp"
  27 #include "unittest.hpp"
  28 #include "utilities/globalDefinitions.hpp"
  29 
  30 TEST(arguments, atojulong) {
  31   char ullong_max[32];
  32   int ret = jio_snprintf(ullong_max, sizeof(ullong_max), JULONG_FORMAT, ULLONG_MAX);
  33   ASSERT_NE(-1, ret);
  34 
  35   julong value;
  36   const char* invalid_strings[] = {
  37     "", "-1", "-100", " 1", "2 ", "3 2", "1.0",
  38     "0x4.5", "0x", "0x0x1" "0.001", "4e10", "e"
  39     "K", "M", "G", "1MB", "1KM", "AA", "0B",
  40     "18446744073709551615K", "17179869184G",
  41     "999999999999999999999999999999"
  42   };
  43   for (uint i = 0; i < ARRAY_SIZE(invalid_strings); i++) {
  44     ASSERT_FALSE(Arguments::atojulong(invalid_strings[i], &value))
  45         << "Invalid string '" << invalid_strings[i] << "' parsed without error.";
  46   }
  47 
  48   struct {
  49     const char* str;
  50     julong expected_value;
  51   } valid_strings[] = {
  52       { "0", 0 },
  53       { "4711", 4711 },
  54       { "1K", 1ULL * K },
  55       { "1k", 1ULL * K },
  56       { "2M", 2ULL * M },
  57       { "2m", 2ULL * M },
  58       { "4G", 4ULL * G },
  59       { "4g", 4ULL * G },
  60       { "0K", 0 },
  61       { ullong_max, ULLONG_MAX },
  62       { "0xcafebabe", 0xcafebabe },
  63       { "0XCAFEBABE", 0xcafebabe },
  64       { "0XCAFEbabe", 0xcafebabe },
  65       { "0x10K", 0x10 * K }
  66   };
  67   for (uint i = 0; i < ARRAY_SIZE(valid_strings); i++) {
  68     ASSERT_TRUE(Arguments::atojulong(valid_strings[i].str, &value))
  69         << "Valid string '" << valid_strings[i].str << "' did not parse.";
  70     ASSERT_EQ(valid_strings[i].expected_value, value);
  71   }
  72 }