test/hotspot/gtest/utilities/test_utf8.cpp
author manc
Mon, 14 Oct 2019 18:48:10 -0700
changeset 58652 9b67dd88a931
parent 47216 71c04702a3d5
permissions -rw-r--r--
8232232: G1RemSetSummary::_rs_threads_vtimes is not initialized to zero Summary: Fix error in "Concurrent refinement threads times" in GC log and cleanup. Reviewed-by: tschatzl, kbarrett
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40901
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
     1
/*
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
     4
 *
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
     8
 *
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    13
 * accompanied this code).
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    14
 *
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    18
 *
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    21
 * questions.
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    22
 */
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    23
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    24
#include "precompiled.hpp"
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    25
#include "utilities/utf8.hpp"
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    26
#include "unittest.hpp"
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    27
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    28
TEST(utf8, length) {
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    29
  char res[60];
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    30
  jchar str[20];
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    31
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    32
  for (int i = 0; i < 20; i++) {
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    33
    str[i] = 0x0800; // char that is 2B in UTF-16 but 3B in UTF-8
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    34
  }
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    35
  str[19] = (jchar) '\0';
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    36
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    37
  // The resulting string in UTF-8 is 3*19 bytes long, but should be truncated
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    38
  UNICODE::as_utf8(str, 19, res, 10);
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    39
  ASSERT_EQ(strlen(res), (size_t) 9) << "string should be truncated here";
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    40
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    41
  UNICODE::as_utf8(str, 19, res, 18);
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    42
  ASSERT_EQ(strlen(res), (size_t) 15) << "string should be truncated here";
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    43
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    44
  UNICODE::as_utf8(str, 19, res, 20);
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    45
  ASSERT_EQ(strlen(res), (size_t) 18) << "string should be truncated here";
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    46
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    47
  // Test with an "unbounded" buffer
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    48
  UNICODE::as_utf8(str, 19, res, INT_MAX);
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    49
  ASSERT_EQ(strlen(res), (size_t) 3 * 19) << "string should end here";
0c83ed47db08 8164743: Convert TestAsUtf8 to GTest
kzhaldyb
parents:
diff changeset
    50
}