Add base::string16 support (issue #1336).

git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@1779 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt 2014-07-15 22:07:25 +00:00
parent b25766ed0c
commit 18f634c11f
7 changed files with 492 additions and 142 deletions

View File

@ -27,6 +27,7 @@
'include/base/cef_platform_thread.h',
'include/base/cef_ref_counted.h',
'include/base/cef_scoped_ptr.h',
'include/base/cef_string16.h',
'include/base/cef_template_util.h',
'include/base/cef_thread_checker.h',
'include/base/cef_thread_collision_warner.h',
@ -111,6 +112,7 @@
'libcef_dll/base/cef_lock_impl.cc',
'libcef_dll/base/cef_logging.cc',
'libcef_dll/base/cef_ref_counted.cc',
'libcef_dll/base/cef_string16.cc',
'libcef_dll/base/cef_thread_checker_impl.cc',
'libcef_dll/base/cef_thread_collision_warner.cc',
'libcef_dll/base/cef_weak_ptr.cc',

View File

@ -110,7 +110,26 @@
#define ARCH_CPU_32_BITS 1
#define ARCH_CPU_LITTLE_ENDIAN 1
#else
#error Please add support for your architecture in build/build_config.h
#error Please add support for your architecture in cef_build.h
#endif
// Type detection for wchar_t.
#if defined(OS_WIN)
#define WCHAR_T_IS_UTF16
#elif defined(OS_POSIX) && defined(COMPILER_GCC) && \
defined(__WCHAR_MAX__) && \
(__WCHAR_MAX__ == 0x7fffffff || __WCHAR_MAX__ == 0xffffffff)
#define WCHAR_T_IS_UTF32
#elif defined(OS_POSIX) && defined(COMPILER_GCC) && \
defined(__WCHAR_MAX__) && \
(__WCHAR_MAX__ == 0x7fff || __WCHAR_MAX__ == 0xffff)
// On Posix, we'll detect short wchar_t, but projects aren't guaranteed to
// compile in this mode (in particular, Chrome doesn't). This is intended for
// other projects using base who manage their own dependencies and make sure
// short wchar works for them.
#define WCHAR_T_IS_UTF16
#else
#error Please add support for your compiler in cef_build.h
#endif
// Annotate a virtual method indicating it must be overriding a virtual

225
include/base/cef_string16.h Normal file
View File

@ -0,0 +1,225 @@
// Copyright (c) 2014 Marshall A. Greenblatt. Portions copyright (c) 2013
// Google Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the name Chromium Embedded
// Framework nor the names of its contributors may be used to endorse
// or promote products derived from this software without specific prior
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef CEF_INCLUDE_BASE_CEF_STRING16_H_
#define CEF_INCLUDE_BASE_CEF_STRING16_H_
#pragma once
#if defined(BASE_STRINGS_STRING16_H_)
// Do nothing if the Chromium header has already been included.
// This can happen in cases where Chromium code is used directly by the
// client application. When using Chromium code directly always include
// the Chromium header first to avoid type conflicts.
#elif defined(BUILDING_CEF_SHARED)
// When building CEF include the Chromium header directly.
#include "base/strings/string16.h"
#else // !BUILDING_CEF_SHARED
// The following is substantially similar to the Chromium implementation.
// If the Chromium implementation diverges the below implementation should be
// updated to match.
// WHAT:
// A version of std::basic_string that provides 2-byte characters even when
// wchar_t is not implemented as a 2-byte type. You can access this class as
// string16. We also define char16, which string16 is based upon.
//
// WHY:
// On Windows, wchar_t is 2 bytes, and it can conveniently handle UTF-16/UCS-2
// data. Plenty of existing code operates on strings encoded as UTF-16.
//
// On many other platforms, sizeof(wchar_t) is 4 bytes by default. We can make
// it 2 bytes by using the GCC flag -fshort-wchar. But then std::wstring fails
// at run time, because it calls some functions (like wcslen) that come from
// the system's native C library -- which was built with a 4-byte wchar_t!
// It's wasteful to use 4-byte wchar_t strings to carry UTF-16 data, and it's
// entirely improper on those systems where the encoding of wchar_t is defined
// as UTF-32.
//
// Here, we define string16, which is similar to std::wstring but replaces all
// libc functions with custom, 2-byte-char compatible routines. It is capable
// of carrying UTF-16-encoded data.
#include <stdio.h>
#include <string>
#include "include/base/cef_basictypes.h"
#if defined(WCHAR_T_IS_UTF16)
namespace base {
typedef wchar_t char16;
typedef std::wstring string16;
typedef std::char_traits<wchar_t> string16_char_traits;
} // namespace base
#elif defined(WCHAR_T_IS_UTF32)
#include "include/base/cef_macros.h"
namespace base {
typedef uint16_t char16;
// char16 versions of the functions required by string16_char_traits; these
// are based on the wide character functions of similar names ("w" or "wcs"
// instead of "c16").
int c16memcmp(const char16* s1, const char16* s2, size_t n);
size_t c16len(const char16* s);
const char16* c16memchr(const char16* s, char16 c, size_t n);
char16* c16memmove(char16* s1, const char16* s2, size_t n);
char16* c16memcpy(char16* s1, const char16* s2, size_t n);
char16* c16memset(char16* s, char16 c, size_t n);
struct string16_char_traits {
typedef char16 char_type;
typedef int int_type;
// int_type needs to be able to hold each possible value of char_type, and in
// addition, the distinct value of eof().
COMPILE_ASSERT(sizeof(int_type) > sizeof(char_type), unexpected_type_width);
typedef std::streamoff off_type;
typedef mbstate_t state_type;
typedef std::fpos<state_type> pos_type;
static void assign(char_type& c1, const char_type& c2) {
c1 = c2;
}
static bool eq(const char_type& c1, const char_type& c2) {
return c1 == c2;
}
static bool lt(const char_type& c1, const char_type& c2) {
return c1 < c2;
}
static int compare(const char_type* s1, const char_type* s2, size_t n) {
return c16memcmp(s1, s2, n);
}
static size_t length(const char_type* s) {
return c16len(s);
}
static const char_type* find(const char_type* s, size_t n,
const char_type& a) {
return c16memchr(s, a, n);
}
static char_type* move(char_type* s1, const char_type* s2, int_type n) {
return c16memmove(s1, s2, n);
}
static char_type* copy(char_type* s1, const char_type* s2, size_t n) {
return c16memcpy(s1, s2, n);
}
static char_type* assign(char_type* s, size_t n, char_type a) {
return c16memset(s, a, n);
}
static int_type not_eof(const int_type& c) {
return eq_int_type(c, eof()) ? 0 : c;
}
static char_type to_char_type(const int_type& c) {
return char_type(c);
}
static int_type to_int_type(const char_type& c) {
return int_type(c);
}
static bool eq_int_type(const int_type& c1, const int_type& c2) {
return c1 == c2;
}
static int_type eof() {
return static_cast<int_type>(EOF);
}
};
typedef std::basic_string<char16, base::string16_char_traits> string16;
extern std::ostream& operator<<(std::ostream& out, const string16& str);
// This is required by googletest to print a readable output on test failures.
extern void PrintTo(const string16& str, std::ostream* out);
} // namespace base
// The string class will be explicitly instantiated only once, in string16.cc.
//
// std::basic_string<> in GNU libstdc++ contains a static data member,
// _S_empty_rep_storage, to represent empty strings. When an operation such
// as assignment or destruction is performed on a string, causing its existing
// data member to be invalidated, it must not be freed if this static data
// member is being used. Otherwise, it counts as an attempt to free static
// (and not allocated) data, which is a memory error.
//
// Generally, due to C++ template magic, _S_empty_rep_storage will be marked
// as a coalesced symbol, meaning that the linker will combine multiple
// instances into a single one when generating output.
//
// If a string class is used by multiple shared libraries, a problem occurs.
// Each library will get its own copy of _S_empty_rep_storage. When strings
// are passed across a library boundary for alteration or destruction, memory
// errors will result. GNU libstdc++ contains a configuration option,
// --enable-fully-dynamic-string (_GLIBCXX_FULLY_DYNAMIC_STRING), which
// disables the static data member optimization, but it's a good optimization
// and non-STL code is generally at the mercy of the system's STL
// configuration. Fully-dynamic strings are not the default for GNU libstdc++
// libstdc++ itself or for the libstdc++ installations on the systems we care
// about, such as Mac OS X and relevant flavors of Linux.
//
// See also http://gcc.gnu.org/bugzilla/show_bug.cgi?id=24196 .
//
// To avoid problems, string classes need to be explicitly instantiated only
// once, in exactly one library. All other string users see it via an "extern"
// declaration. This is precisely how GNU libstdc++ handles
// std::basic_string<char> (string) and std::basic_string<wchar_t> (wstring).
//
// This also works around a Mac OS X linker bug in ld64-85.2.1 (Xcode 3.1.2),
// in which the linker does not fully coalesce symbols when dead code
// stripping is enabled. This bug causes the memory errors described above
// to occur even when a std::basic_string<> does not cross shared library
// boundaries, such as in statically-linked executables.
//
// TODO(mark): File this bug with Apple and update this note with a bug number.
extern template
class std::basic_string<base::char16, base::string16_char_traits>;
#endif // WCHAR_T_IS_UTF32
#endif // !BUILDING_CEF_SHARED
#endif // CEF_INCLUDE_BASE_CEF_STRING16_H_

View File

@ -33,13 +33,10 @@
#include <memory.h>
#include <string>
#include "include/base/cef_string16.h"
#include "include/internal/cef_string_types.h"
#ifdef BUILDING_CEF_SHARED
#include "base/strings/string16.h"
#endif
///
// Traits implementation for wide character strings.
///
@ -87,7 +84,6 @@ struct CefStringTraitsWide {
return cef_string_wide_set(str.c_str(), str.length(), s, true) ?
true : false;
}
#if defined(BUILDING_CEF_SHARED)
#if defined(WCHAR_T_IS_UTF32)
static inline base::string16 to_string16(const struct_type *s) {
cef_string_utf16_t cstr;
@ -113,7 +109,6 @@ struct CefStringTraitsWide {
true : false;
}
#endif // WCHAR_T_IS_UTF32
#endif // BUILDING_CEF_SHARED
};
///
@ -162,7 +157,6 @@ struct CefStringTraitsUTF8 {
static inline bool from_wstring(const std::wstring& str, struct_type* s) {
return cef_string_wide_to_utf8(str.c_str(), str.length(), s) ? true : false;
}
#if defined(BUILDING_CEF_SHARED)
static inline base::string16 to_string16(const struct_type* s) {
cef_string_utf16_t cstr;
memset(&cstr, 0, sizeof(cstr));
@ -177,7 +171,6 @@ struct CefStringTraitsUTF8 {
return cef_string_utf16_to_utf8(str.c_str(), str.length(), s) ?
true : false;
}
#endif // BUILDING_CEF_SHARED
};
///
@ -245,7 +238,6 @@ struct CefStringTraitsUTF16 {
true : false;
}
#endif // WCHAR_T_IS_UTF32
#if defined(BUILDING_CEF_SHARED)
static inline base::string16 to_string16(const struct_type* s) {
return base::string16(s->str, s->length);
}
@ -253,7 +245,6 @@ struct CefStringTraitsUTF16 {
return cef_string_utf16_set(str.c_str(), str.length(), s, true) ?
true : false;
}
#endif // BUILDING_CEF_SHARED
};
///
@ -334,7 +325,7 @@ class CefStringBase {
FromWString(std::wstring(src));
}
#if (defined(BUILDING_CEF_SHARED) && defined(WCHAR_T_IS_UTF32))
#if defined(WCHAR_T_IS_UTF32)
///
// Create a new string from an existing string16. Data will be always
// copied. Translation will occur if necessary based on the underlying string
@ -349,7 +340,7 @@ class CefStringBase {
if (src)
FromString16(base::string16(src));
}
#endif // BUILDING_CEF_SHARED && WCHAR_T_IS_UTF32
#endif // WCHAR_T_IS_UTF32
///
// Create a new string from an existing character array. If |copy| is true
@ -612,7 +603,7 @@ class CefStringBase {
AllocIfNeeded();
return traits::from_wstring(str, string_);
}
#if defined(BUILDING_CEF_SHARED)
///
// Return this string's data as a string16. Translation will occur if
// necessary based on the underlying string type.
@ -636,7 +627,6 @@ class CefStringBase {
AllocIfNeeded();
return traits::from_string16(str, string_);
}
#endif // BUILDING_CEF_SHARED
///
// Comparison operator overloads.
@ -689,7 +679,7 @@ class CefStringBase {
FromWString(std::wstring(str));
return *this;
}
#if (defined(BUILDING_CEF_SHARED) && defined(WCHAR_T_IS_UTF32))
#if defined(WCHAR_T_IS_UTF32)
operator base::string16() const {
return ToString16();
}
@ -701,7 +691,7 @@ class CefStringBase {
FromString16(base::string16(str));
return *this;
}
#endif // BUILDING_CEF_SHARED && WCHAR_T_IS_UTF32
#endif // WCHAR_T_IS_UTF32
private:
// Allocate the string structure if it doesn't already exist.

View File

@ -0,0 +1,89 @@
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "include/base/cef_string16.h"
#if defined(OS_POSIX)
#if defined(WCHAR_T_IS_UTF16)
#error This file should not be used on 2-byte wchar_t systems
// If this winds up being needed on 2-byte wchar_t systems, either the
// definitions below can be used, or the host system's wide character
// functions like wmemcmp can be wrapped.
#elif defined(WCHAR_T_IS_UTF32)
#include <cstring>
#include <ostream>
#include "include/internal/cef_string_types.h"
namespace base {
int c16memcmp(const char16* s1, const char16* s2, size_t n) {
// We cannot call memcmp because that changes the semantics.
while (n-- > 0) {
if (*s1 != *s2) {
// We cannot use (*s1 - *s2) because char16 is unsigned.
return ((*s1 < *s2) ? -1 : 1);
}
++s1;
++s2;
}
return 0;
}
size_t c16len(const char16* s) {
const char16 *s_orig = s;
while (*s) {
++s;
}
return s - s_orig;
}
const char16* c16memchr(const char16* s, char16 c, size_t n) {
while (n-- > 0) {
if (*s == c) {
return s;
}
++s;
}
return 0;
}
char16* c16memmove(char16* s1, const char16* s2, size_t n) {
return static_cast<char16*>(memmove(s1, s2, n * sizeof(char16)));
}
char16* c16memcpy(char16* s1, const char16* s2, size_t n) {
return static_cast<char16*>(memcpy(s1, s2, n * sizeof(char16)));
}
char16* c16memset(char16* s, char16 c, size_t n) {
char16 *s_orig = s;
while (n-- > 0) {
*s = c;
++s;
}
return s_orig;
}
std::ostream& operator<<(std::ostream& out, const string16& str) {
cef_string_utf8_t cef_str = {0};
cef_string_utf16_to_utf8(str.c_str(), str.size(), &cef_str);
out << cef_str.str;
cef_string_utf8_clear(&cef_str);
return out;
}
void PrintTo(const string16& str, std::ostream* out) {
*out << str;
}
} // namespace base
template class std::basic_string<base::char16, base::string16_char_traits>;
#endif // WCHAR_T_IS_UTF32
#endif // OS_POSIX

View File

@ -8,6 +8,7 @@
// Include some Chromium headers first to avoid type conflicts with CEF headers.
#include "base/bind.h"
#include "base/strings/string16.h"
#include "base/synchronization/lock.h"
#endif // CEF_TESTS_UNITTESTS_CHROMIUM_INCLUDES_H_

View File

@ -8,6 +8,7 @@
// Include this first to avoid type conflicts with CEF headers.
#include "tests/unittests/chromium_includes.h"
#include "include/base/cef_string16.h"
#include "include/internal/cef_string.h"
#include "include/internal/cef_string_list.h"
#include "include/internal/cef_string_map.h"
@ -17,115 +18,138 @@
// Test UTF8 strings.
TEST(StringTest, UTF8) {
CefStringUTF8 str1("Test String");
ASSERT_EQ(str1.length(), (size_t)11);
ASSERT_FALSE(str1.empty());
ASSERT_TRUE(str1.IsOwner());
EXPECT_EQ(str1.length(), (size_t)11);
EXPECT_FALSE(str1.empty());
EXPECT_TRUE(str1.IsOwner());
// Test equality.
CefStringUTF8 str2("Test String");
ASSERT_EQ(str1, str2);
ASSERT_LE(str1, str2);
ASSERT_GE(str1, str2);
EXPECT_EQ(str1, str2);
EXPECT_LE(str1, str2);
EXPECT_GE(str1, str2);
str2 = "Test Test";
ASSERT_LT(str1, str2);
ASSERT_GT(str2, str1);
EXPECT_LT(str1, str2);
EXPECT_GT(str2, str1);
// When strings are the same but of unequal length, the longer string is
// greater.
str2 = "Test";
ASSERT_LT(str2, str1);
ASSERT_GT(str1, str2);
EXPECT_LT(str2, str1);
EXPECT_GT(str1, str2);
// Test conversions.
str2 = str1.ToString();
ASSERT_EQ(str1, str2);
EXPECT_EQ(str1, str2);
str2 = str1.ToWString();
ASSERT_EQ(str1, str2);
EXPECT_EQ(str1, str2);
// Test userfree assignment.
cef_string_userfree_utf8_t uf = str2.DetachToUserFree();
ASSERT_TRUE(uf != NULL);
ASSERT_TRUE(str2.empty());
EXPECT_TRUE(uf != NULL);
EXPECT_TRUE(str2.empty());
str2.AttachToUserFree(uf);
ASSERT_FALSE(str2.empty());
ASSERT_EQ(str1, str2);
EXPECT_FALSE(str2.empty());
EXPECT_EQ(str1, str2);
}
// Test UTF16 strings.
TEST(StringTest, UTF16) {
CefStringUTF16 str1("Test String");
ASSERT_EQ(str1.length(), (size_t)11);
ASSERT_FALSE(str1.empty());
ASSERT_TRUE(str1.IsOwner());
EXPECT_EQ(str1.length(), (size_t)11);
EXPECT_FALSE(str1.empty());
EXPECT_TRUE(str1.IsOwner());
// Test equality.
CefStringUTF16 str2("Test String");
ASSERT_EQ(str1, str2);
ASSERT_LE(str1, str2);
ASSERT_GE(str1, str2);
EXPECT_EQ(str1, str2);
EXPECT_LE(str1, str2);
EXPECT_GE(str1, str2);
str2 = "Test Test";
ASSERT_LT(str1, str2);
ASSERT_GT(str2, str1);
EXPECT_LT(str1, str2);
EXPECT_GT(str2, str1);
// When strings are the same but of unequal length, the longer string is
// greater.
str2 = "Test";
ASSERT_LT(str2, str1);
ASSERT_GT(str1, str2);
EXPECT_LT(str2, str1);
EXPECT_GT(str1, str2);
// Test conversions.
str2 = str1.ToString();
ASSERT_EQ(str1, str2);
EXPECT_EQ(str1, str2);
str2 = str1.ToWString();
ASSERT_EQ(str1, str2);
EXPECT_EQ(str1, str2);
// Test userfree assignment.
cef_string_userfree_utf16_t uf = str2.DetachToUserFree();
ASSERT_TRUE(uf != NULL);
ASSERT_TRUE(str2.empty());
EXPECT_TRUE(uf != NULL);
EXPECT_TRUE(str2.empty());
str2.AttachToUserFree(uf);
ASSERT_FALSE(str2.empty());
ASSERT_EQ(str1, str2);
EXPECT_FALSE(str2.empty());
EXPECT_EQ(str1, str2);
}
// Test wide strings.
TEST(StringTest, Wide) {
CefStringWide str1("Test String");
ASSERT_EQ(str1.length(), (size_t)11);
ASSERT_FALSE(str1.empty());
ASSERT_TRUE(str1.IsOwner());
EXPECT_EQ(str1.length(), (size_t)11);
EXPECT_FALSE(str1.empty());
EXPECT_TRUE(str1.IsOwner());
// Test equality.
CefStringWide str2("Test String");
ASSERT_EQ(str1, str2);
ASSERT_LE(str1, str2);
ASSERT_GE(str1, str2);
EXPECT_EQ(str1, str2);
EXPECT_LE(str1, str2);
EXPECT_GE(str1, str2);
str2 = "Test Test";
ASSERT_LT(str1, str2);
ASSERT_GT(str2, str1);
EXPECT_LT(str1, str2);
EXPECT_GT(str2, str1);
// When strings are the same but of unequal length, the longer string is
// greater.
str2 = "Test";
ASSERT_LT(str2, str1);
ASSERT_GT(str1, str2);
EXPECT_LT(str2, str1);
EXPECT_GT(str1, str2);
// Test conversions.
str2 = str1.ToString();
ASSERT_EQ(str1, str2);
EXPECT_EQ(str1, str2);
str2 = str1.ToWString();
ASSERT_EQ(str1, str2);
EXPECT_EQ(str1, str2);
// Test userfree assignment.
cef_string_userfree_wide_t uf = str2.DetachToUserFree();
ASSERT_TRUE(uf != NULL);
ASSERT_TRUE(str2.empty());
EXPECT_TRUE(uf != NULL);
EXPECT_TRUE(str2.empty());
str2.AttachToUserFree(uf);
ASSERT_FALSE(str2.empty());
ASSERT_EQ(str1, str2);
EXPECT_FALSE(str2.empty());
EXPECT_EQ(str1, str2);
}
// Test base::string16 convertion to/from CefString types.
TEST(StringTest, string16) {
CefStringUTF8 str8("Test String 1"), str8b;
CefStringUTF16 str16("Test String 2"), str16b;
CefStringWide strwide("Test String 3"), strwideb;
base::string16 base_str;
base_str = str8;
str8b = base_str;
EXPECT_EQ(str8, base_str);
EXPECT_EQ(str8, str8b);
base_str = str16;
str16b = base_str;
EXPECT_EQ(str16, base_str);
EXPECT_EQ(str16, str16b);
base_str = strwide;
strwideb = base_str;
EXPECT_EQ(strwide, base_str);
EXPECT_EQ(strwide, strwideb);
}
// Test string lists.
@ -136,12 +160,12 @@ TEST(StringTest, List) {
list.push_back("String 2");
list.push_back("String 3");
ASSERT_EQ(list[0], "String 1");
ASSERT_EQ(list[1], "String 2");
ASSERT_EQ(list[2], "String 3");
EXPECT_EQ(list[0], "String 1");
EXPECT_EQ(list[1], "String 2");
EXPECT_EQ(list[2], "String 3");
cef_string_list_t listPtr = cef_string_list_alloc();
ASSERT_TRUE(listPtr != NULL);
EXPECT_TRUE(listPtr != NULL);
ListType::const_iterator it = list.begin();
for (; it != list.end(); ++it)
cef_string_list_append(listPtr, it->GetStruct());
@ -149,34 +173,34 @@ TEST(StringTest, List) {
CefString str;
int ret;
ASSERT_EQ(cef_string_list_size(listPtr), 3);
EXPECT_EQ(cef_string_list_size(listPtr), 3);
ret = cef_string_list_value(listPtr, 0, str.GetWritableStruct());
ASSERT_TRUE(ret);
ASSERT_EQ(str, "String 1");
EXPECT_TRUE(ret);
EXPECT_EQ(str, "String 1");
ret = cef_string_list_value(listPtr, 1, str.GetWritableStruct());
ASSERT_TRUE(ret);
ASSERT_EQ(str, "String 2");
EXPECT_TRUE(ret);
EXPECT_EQ(str, "String 2");
ret = cef_string_list_value(listPtr, 2, str.GetWritableStruct());
ASSERT_TRUE(ret);
ASSERT_EQ(str, "String 3");
EXPECT_TRUE(ret);
EXPECT_EQ(str, "String 3");
cef_string_list_t listPtr2 = cef_string_list_copy(listPtr);
cef_string_list_clear(listPtr);
ASSERT_EQ(cef_string_list_size(listPtr), 0);
EXPECT_EQ(cef_string_list_size(listPtr), 0);
cef_string_list_free(listPtr);
ASSERT_EQ(cef_string_list_size(listPtr2), 3);
EXPECT_EQ(cef_string_list_size(listPtr2), 3);
ret = cef_string_list_value(listPtr2, 0, str.GetWritableStruct());
ASSERT_TRUE(ret);
ASSERT_EQ(str, "String 1");
EXPECT_TRUE(ret);
EXPECT_EQ(str, "String 1");
ret = cef_string_list_value(listPtr2, 1, str.GetWritableStruct());
ASSERT_TRUE(ret);
ASSERT_EQ(str, "String 2");
EXPECT_TRUE(ret);
EXPECT_EQ(str, "String 2");
ret = cef_string_list_value(listPtr2, 2, str.GetWritableStruct());
ASSERT_TRUE(ret);
ASSERT_EQ(str, "String 3");
EXPECT_TRUE(ret);
EXPECT_EQ(str, "String 3");
cef_string_list_free(listPtr2);
}
@ -192,18 +216,18 @@ TEST(StringTest, Map) {
MapType::const_iterator it;
it = map.find("Key 2");
ASSERT_TRUE(it != map.end());
ASSERT_EQ(it->first, "Key 2");
ASSERT_EQ(it->second, "String 2");
EXPECT_TRUE(it != map.end());
EXPECT_EQ(it->first, "Key 2");
EXPECT_EQ(it->second, "String 2");
it = map.find(L"Key 2");
ASSERT_TRUE(it != map.end());
ASSERT_EQ(it->first, L"Key 2");
ASSERT_EQ(it->second, L"String 2");
EXPECT_TRUE(it != map.end());
EXPECT_EQ(it->first, L"Key 2");
EXPECT_EQ(it->second, L"String 2");
ASSERT_EQ(map["Key 1"], "String 1");
ASSERT_EQ(map["Key 2"], "String 2");
ASSERT_EQ(map["Key 3"], "String 3");
EXPECT_EQ(map["Key 1"], "String 1");
EXPECT_EQ(map["Key 2"], "String 2");
EXPECT_EQ(map["Key 3"], "String 3");
cef_string_map_t mapPtr = cef_string_map_alloc();
@ -216,37 +240,37 @@ TEST(StringTest, Map) {
CefString str;
int ret;
ASSERT_EQ(cef_string_map_size(mapPtr), 3);
EXPECT_EQ(cef_string_map_size(mapPtr), 3);
ret = cef_string_map_key(mapPtr, 0, str.GetWritableStruct());
ASSERT_TRUE(ret);
ASSERT_EQ(str, "Key 1");
EXPECT_TRUE(ret);
EXPECT_EQ(str, "Key 1");
ret = cef_string_map_value(mapPtr, 0, str.GetWritableStruct());
ASSERT_TRUE(ret);
ASSERT_EQ(str, "String 1");
EXPECT_TRUE(ret);
EXPECT_EQ(str, "String 1");
ret = cef_string_map_key(mapPtr, 1, str.GetWritableStruct());
ASSERT_TRUE(ret);
ASSERT_EQ(str, "Key 2");
EXPECT_TRUE(ret);
EXPECT_EQ(str, "Key 2");
ret = cef_string_map_value(mapPtr, 1, str.GetWritableStruct());
ASSERT_TRUE(ret);
ASSERT_EQ(str, "String 2");
EXPECT_TRUE(ret);
EXPECT_EQ(str, "String 2");
ret = cef_string_map_key(mapPtr, 2, str.GetWritableStruct());
ASSERT_TRUE(ret);
ASSERT_EQ(str, "Key 3");
EXPECT_TRUE(ret);
EXPECT_EQ(str, "Key 3");
ret = cef_string_map_value(mapPtr, 2, str.GetWritableStruct());
ASSERT_TRUE(ret);
ASSERT_EQ(str, "String 3");
EXPECT_TRUE(ret);
EXPECT_EQ(str, "String 3");
CefString key;
key.FromASCII("Key 2");
ret = cef_string_map_find(mapPtr, key.GetStruct(), str.GetWritableStruct());
ASSERT_TRUE(ret);
ASSERT_EQ(str, "String 2");
EXPECT_TRUE(ret);
EXPECT_EQ(str, "String 2");
cef_string_map_clear(mapPtr);
ASSERT_EQ(cef_string_map_size(mapPtr), 0);
EXPECT_EQ(cef_string_map_size(mapPtr), 0);
cef_string_map_free(mapPtr);
}
@ -263,23 +287,23 @@ TEST(StringTest, Multimap) {
MapType::const_iterator it;
it = map.find("Key 2");
ASSERT_TRUE(it != map.end());
ASSERT_EQ(it->first, "Key 2");
ASSERT_EQ(it->second, "String 2");
EXPECT_TRUE(it != map.end());
EXPECT_EQ(it->first, "Key 2");
EXPECT_EQ(it->second, "String 2");
std::pair<MapType::const_iterator, MapType::const_iterator>
range_it = map.equal_range("Key 2");
ASSERT_TRUE(range_it.first != range_it.second);
EXPECT_TRUE(range_it.first != range_it.second);
MapType::const_iterator same_key_it = range_it.first;
// Either of "String 2" or "String 2.1" is fine since
// std::multimap provides no guarantee wrt the order of
// values with the same key.
ASSERT_EQ(same_key_it->second.ToString().find("String 2"), (size_t)0);
ASSERT_EQ((++same_key_it)->second.ToString().find("String 2"), (size_t)0);
ASSERT_EQ(map.count("Key 2"), (size_t)2);
EXPECT_EQ(same_key_it->second.ToString().find("String 2"), (size_t)0);
EXPECT_EQ((++same_key_it)->second.ToString().find("String 2"), (size_t)0);
EXPECT_EQ(map.count("Key 2"), (size_t)2);
ASSERT_EQ(map.find("Key 1")->second, "String 1");
ASSERT_EQ(map.find("Key 3")->second, "String 3");
EXPECT_EQ(map.find("Key 1")->second, "String 1");
EXPECT_EQ(map.find("Key 3")->second, "String 3");
cef_string_multimap_t mapPtr = cef_string_multimap_alloc();
@ -292,53 +316,53 @@ TEST(StringTest, Multimap) {
CefString str;
int ret;
ASSERT_EQ(cef_string_multimap_size(mapPtr), 4);
EXPECT_EQ(cef_string_multimap_size(mapPtr), 4);
ret = cef_string_multimap_key(mapPtr, 0, str.GetWritableStruct());
ASSERT_TRUE(ret);
ASSERT_EQ(str, "Key 1");
EXPECT_TRUE(ret);
EXPECT_EQ(str, "Key 1");
ret = cef_string_multimap_value(mapPtr, 0, str.GetWritableStruct());
ASSERT_TRUE(ret);
ASSERT_EQ(str, "String 1");
EXPECT_TRUE(ret);
EXPECT_EQ(str, "String 1");
ret = cef_string_multimap_key(mapPtr, 1, str.GetWritableStruct());
ASSERT_TRUE(ret);
ASSERT_EQ(str, "Key 2");
EXPECT_TRUE(ret);
EXPECT_EQ(str, "Key 2");
ret = cef_string_multimap_value(mapPtr, 1, str.GetWritableStruct());
ASSERT_TRUE(ret);
ASSERT_EQ(str.ToString().find("String 2"), (size_t)0);
EXPECT_TRUE(ret);
EXPECT_EQ(str.ToString().find("String 2"), (size_t)0);
ret = cef_string_multimap_key(mapPtr, 2, str.GetWritableStruct());
ASSERT_TRUE(ret);
ASSERT_EQ(str, "Key 2");
EXPECT_TRUE(ret);
EXPECT_EQ(str, "Key 2");
ret = cef_string_multimap_value(mapPtr, 2, str.GetWritableStruct());
ASSERT_TRUE(ret);
ASSERT_EQ(str.ToString().find("String 2"), (size_t)0);
EXPECT_TRUE(ret);
EXPECT_EQ(str.ToString().find("String 2"), (size_t)0);
ret = cef_string_multimap_key(mapPtr, 3, str.GetWritableStruct());
ASSERT_TRUE(ret);
ASSERT_EQ(str, "Key 3");
EXPECT_TRUE(ret);
EXPECT_EQ(str, "Key 3");
ret = cef_string_multimap_value(mapPtr, 3, str.GetWritableStruct());
ASSERT_TRUE(ret);
ASSERT_EQ(str, "String 3");
EXPECT_TRUE(ret);
EXPECT_EQ(str, "String 3");
CefString key;
key.FromASCII("Key 2");
ret = cef_string_multimap_find_count(mapPtr, key.GetStruct());
ASSERT_EQ(ret, 2);
EXPECT_EQ(ret, 2);
ret = cef_string_multimap_enumerate(mapPtr,
key.GetStruct(), 0, str.GetWritableStruct());
ASSERT_TRUE(ret);
ASSERT_EQ(str.ToString().find("String 2"), (size_t)0);
EXPECT_TRUE(ret);
EXPECT_EQ(str.ToString().find("String 2"), (size_t)0);
ret = cef_string_multimap_enumerate(mapPtr,
key.GetStruct(), 1, str.GetWritableStruct());
ASSERT_TRUE(ret);
ASSERT_EQ(str.ToString().find("String 2"), (size_t)0);
EXPECT_TRUE(ret);
EXPECT_EQ(str.ToString().find("String 2"), (size_t)0);
cef_string_multimap_clear(mapPtr);
ASSERT_EQ(cef_string_multimap_size(mapPtr), 0);
EXPECT_EQ(cef_string_multimap_size(mapPtr), 0);
cef_string_multimap_free(mapPtr);
}