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 "utilities/utf8.hpp"
26#include "unittest.hpp"
27
28TEST(utf8, length) {
29 char res[60];
30 jchar str[20];
31
32 for (int i = 0; i < 20; i++) {
33 str[i] = 0x0800; // char that is 2B in UTF-16 but 3B in UTF-8
34 }
35 str[19] = (jchar) '\0';
36
37 // The resulting string in UTF-8 is 3*19 bytes long, but should be truncated
38 UNICODE::as_utf8(str, 19, res, 10);
39 ASSERT_EQ(strlen(res), (size_t) 9) << "string should be truncated here";
40
41 UNICODE::as_utf8(str, 19, res, 18);
42 ASSERT_EQ(strlen(res), (size_t) 15) << "string should be truncated here";
43
44 UNICODE::as_utf8(str, 19, res, 20);
45 ASSERT_EQ(strlen(res), (size_t) 18) << "string should be truncated here";
46
47 // Test with an "unbounded" buffer
48 UNICODE::as_utf8(str, 19, res, INT_MAX);
49 ASSERT_EQ(strlen(res), (size_t) 3 * 19) << "string should end here";
50}
51