Blender V4.3
BLI_string_utf8_test.cc File Reference
#include "testing/testing.h"
#include "BLI_rand.h"
#include "BLI_string.h"
#include "BLI_string_cursor_utf8.h"
#include "BLI_string_utf8.h"
#include "BLI_utildefines.h"

Go to the source code of this file.

Macros

#define STR_MB_ALPHA_1   "\x41"
 
#define STR_MB_ALPHA_2   "\xc2\xaa"
 
#define STR_MB_ALPHA_3   "\xe0\xa0\x80"
 
#define STR_MB_ALPHA_4   "\xf0\x90\x80\x80"
 
#define STR_MB_ALPHA_5   "\xf8\x80\x80\x80\x80"
 
#define STR_MB_ALPHA_6   "\xfc\x80\x80\x80\x80\x80"
 
#define EXPECT_BYTE_OFFSET(truncate_ofs, expect_nchars)
 

Functions

Test #BLI_strnlen_utf8
 TEST (string, StringNLenUTF8)
 
 TEST (string, StringNLenUTF8_Incomplete)
 
Test #BLI_str_utf8_offset_from_index
 TEST (string, Utf8OffsetFromIndex_ClampedIndex)
 
Test #BLI_str_utf8_as_unicode_step_safe
static size_t utf8_as_char32 (const char *str, const char str_len, char32_t *r_result)
 
template<size_t Size, size_t SizeWithPadding>
void utf8_as_char32_test_compare_with_pad_bytes (const char utf8_src[Size])
 
template<size_t Size>
void utf8_as_char32_test_compare (const char utf8_src[Size])
 
template<size_t Size>
void utf8_as_char32_test_at_buffer_size ()
 
 TEST (string, Utf8AsUnicodeStep)
 
Test #BLI_str_cursor_step_next_utf32_empty
 TEST (string, StrCursorStepNextUtf32Empty)
 
Test #BLI_str_cursor_step_next_utf32_single
 TEST (string, StrCursorStepNextUtf32Single)
 
Test #BLI_str_cursor_step_next_utf32_simple
 TEST (string, StrCursorStepNextUtf32Simple)
 
Test #BLI_str_cursor_step_next_utf32_allcombining
 TEST (string, StrCursorStepNextUtf32AllCombining)
 
Test #BLI_str_cursor_step_next_utf32_complex
 TEST (string, StrCursorStepNextUtf32Complex)
 
Test #BLI_str_cursor_step_next_utf32_invalid
 TEST (string, StrCursorStepNextUtf32Invalid)
 
Test #BLI_str_cursor_step_prev_utf32_empty
 TEST (string, StrCursorStepPrevUtf32Empty)
 
Test #BLI_str_cursor_step_prev_utf32_single
 TEST (string, StrCursorStepPrevUtf32Single)
 
Test #BLI_str_cursor_step_prev_utf32_simple
 TEST (string, StrCursorStepPrevUtf32Simple)
 
Test #BLI_str_cursor_step_prev_utf32_allcombining
 TEST (string, StrCursorStepPrevUtf32AllCombining)
 
Test #BLI_str_cursor_step_prev_utf32_complex
 TEST (string, StrCursorStepPrevUtf32Complex)
 
Test #BLI_str_cursor_step_prev_utf32_invalid
 TEST (string, StrCursorStepPrevUtf32Invalid)
 
Test #BLI_str_cursor_step_next_utf8_empty
 TEST (string, StrCursorStepNextUtf8Empty)
 
Test #BLI_str_cursor_step_next_utf8_single
 TEST (string, StrCursorStepNextUtf8Single)
 
Test #BLI_str_cursor_step_next_utf8_simple
 TEST (string, StrCursorStepNextUtf8Simple)
 
Test #BLI_str_cursor_step_next_utf8_allcombining
 TEST (string, StrCursorStepNextUtf8AllCombining)
 
Test #BLI_str_cursor_step_next_utf8_complex
 TEST (string, StrCursorStepNextUtf8AllComplex)
 
Test #BLI_str_cursor_step_next_utf8_invalid
 TEST (string, StrCursorStepNextUtf8Invalid)
 
Test #BLI_str_cursor_step_prev_utf8_empty
 TEST (string, StrCursorStepPrevUtf8Empty)
 
Test #BLI_str_cursor_step_prev_utf8_single
 TEST (string, StrCursorStepPrevUtf8Single)
 
 TEST (string, StrCursorStepPrevUtf8Simple)
 
Test #BLI_str_cursor_step_prev_utf8_allcombining
 TEST (string, StrCursorStepPrevUtf8AllCombining)
 
Test #BLI_str_cursor_step_prev_utf8_complex
 TEST (string, StrCursorStepPrevUtf8Complex)
 
Test #BLI_str_cursor_step_prev_utf8_invalid
 TEST (string, StrCursorStepPrevUtf8Invalid)
 

Test #BLI_str_utf8_invalid_strip

static const char * utf8_invalid_tests [][3]
 
 TEST (string, Utf8InvalidBytes)
 

Macro Definition Documentation

◆ EXPECT_BYTE_OFFSET

#define EXPECT_BYTE_OFFSET ( truncate_ofs,
expect_nchars )
Value:
{ \
size_t buf_ofs = 0; \
strcpy(buf, ref_str); \
buf[truncate_ofs] = '\0'; \
EXPECT_EQ(BLI_strnlen_utf8_ex(buf, ref_str_len, &buf_ofs), expect_nchars); \
EXPECT_EQ(buf_ofs, truncate_ofs); \
}
size_t BLI_strnlen_utf8_ex(const char *strc, size_t strc_maxlen, size_t *r_len_bytes) ATTR_NONNULL(1

Referenced by TEST().

◆ STR_MB_ALPHA_1

#define STR_MB_ALPHA_1   "\x41"

Multi byte defies, use when the exact character isn't important, and when proper handling of multi-byte sequences is needed.

Define the first text character found in 1-4 bytes. We might want to generate other string tables too.

unicode_max = 0x110000
bytes_max = 4
sample_chars = dict()
for i in range(unicode_max):
ch = chr(i)
try:
byte = ch.encode('utf-8')
byte_len = len(byte)
except UnicodeEncodeError:
continue
if byte_len not in sample_chars:
# Not essential but useful if the character is printable.
if ch.isalpha():
sample_chars[byte_len] = byte
if byte_len == bytes_max:
break
assert tuple(sorted(sample_chars.keys())) == tuple(range(1, bytes_max + 1))
for i in range(1, bytes_max + 1):
byte = sample_chars[i]
print("#define STR_MB_ALPHA_%d \"" % i, end="")
for x in byte:
print("\\x%02x" % x, end="")
print("\"")
int len

Definition at line 52 of file BLI_string_utf8_test.cc.

Referenced by TEST(), and TEST().

◆ STR_MB_ALPHA_2

#define STR_MB_ALPHA_2   "\xc2\xaa"

Definition at line 53 of file BLI_string_utf8_test.cc.

Referenced by TEST(), and TEST().

◆ STR_MB_ALPHA_3

#define STR_MB_ALPHA_3   "\xe0\xa0\x80"

Definition at line 54 of file BLI_string_utf8_test.cc.

Referenced by TEST(), and TEST().

◆ STR_MB_ALPHA_4

#define STR_MB_ALPHA_4   "\xf0\x90\x80\x80"

Definition at line 55 of file BLI_string_utf8_test.cc.

Referenced by TEST(), and TEST().

◆ STR_MB_ALPHA_5

#define STR_MB_ALPHA_5   "\xf8\x80\x80\x80\x80"

Definition at line 61 of file BLI_string_utf8_test.cc.

Referenced by TEST().

◆ STR_MB_ALPHA_6

#define STR_MB_ALPHA_6   "\xfc\x80\x80\x80\x80\x80"

Definition at line 62 of file BLI_string_utf8_test.cc.

Referenced by TEST(), and TEST().

Function Documentation

◆ TEST() [1/29]

TEST ( string ,
StrCursorStepNextUtf32AllCombining  )

Definition at line 589 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_next_utf32(), len, and pos.

◆ TEST() [2/29]

TEST ( string ,
StrCursorStepNextUtf32Complex  )

Definition at line 609 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_next_utf32(), len, and pos.

◆ TEST() [3/29]

TEST ( string ,
StrCursorStepNextUtf32Empty  )

Definition at line 539 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_next_utf32(), len, and pos.

◆ TEST() [4/29]

TEST ( string ,
StrCursorStepNextUtf32Invalid  )

Definition at line 634 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_next_utf32(), len, and pos.

◆ TEST() [5/29]

TEST ( string ,
StrCursorStepNextUtf32Simple  )

Definition at line 571 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_next_utf32(), len, and pos.

◆ TEST() [6/29]

TEST ( string ,
StrCursorStepNextUtf32Single  )

Definition at line 555 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_next_utf32(), len, and pos.

◆ TEST() [7/29]

TEST ( string ,
StrCursorStepNextUtf8AllCombining  )

Definition at line 834 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_next_utf8(), len, and pos.

◆ TEST() [8/29]

TEST ( string ,
StrCursorStepNextUtf8AllComplex  )

Definition at line 860 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_next_utf8(), len, and pos.

◆ TEST() [9/29]

TEST ( string ,
StrCursorStepNextUtf8Empty  )

Definition at line 786 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_next_utf8(), len, and pos.

◆ TEST() [10/29]

TEST ( string ,
StrCursorStepNextUtf8Invalid  )

Definition at line 895 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_next_utf8(), EXPECT_EQ(), len, and pos.

◆ TEST() [11/29]

TEST ( string ,
StrCursorStepNextUtf8Simple  )

Definition at line 816 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_next_utf8(), len, and pos.

◆ TEST() [12/29]

TEST ( string ,
StrCursorStepNextUtf8Single  )

Definition at line 801 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_next_utf8(), len, and pos.

◆ TEST() [13/29]

TEST ( string ,
StrCursorStepPrevUtf32AllCombining  )

Definition at line 711 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_prev_utf32(), len, and pos.

◆ TEST() [14/29]

TEST ( string ,
StrCursorStepPrevUtf32Complex  )

Definition at line 731 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_prev_utf32(), len, and pos.

◆ TEST() [15/29]

TEST ( string ,
StrCursorStepPrevUtf32Empty  )

Definition at line 665 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_prev_utf32(), len, and pos.

◆ TEST() [16/29]

TEST ( string ,
StrCursorStepPrevUtf32Invalid  )

Definition at line 756 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_prev_utf32(), len, and pos.

◆ TEST() [17/29]

TEST ( string ,
StrCursorStepPrevUtf32Simple  )

Definition at line 694 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_prev_utf32(), len, and pos.

◆ TEST() [18/29]

TEST ( string ,
StrCursorStepPrevUtf32Single  )

Definition at line 679 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_prev_utf32(), len, and pos.

◆ TEST() [19/29]

TEST ( string ,
StrCursorStepPrevUtf8AllCombining  )

Definition at line 974 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_prev_utf8(), len, and pos.

◆ TEST() [20/29]

TEST ( string ,
StrCursorStepPrevUtf8Complex  )

Definition at line 1000 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_prev_utf8(), len, and pos.

◆ TEST() [21/29]

TEST ( string ,
StrCursorStepPrevUtf8Empty  )

Definition at line 926 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_prev_utf8(), len, and pos.

◆ TEST() [22/29]

TEST ( string ,
StrCursorStepPrevUtf8Invalid  )

Definition at line 1035 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_prev_utf8(), len, and pos.

◆ TEST() [23/29]

TEST ( string ,
StrCursorStepPrevUtf8Simple  )

Definition at line 957 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_prev_utf8(), len, and pos.

◆ TEST() [24/29]

TEST ( string ,
StrCursorStepPrevUtf8Single  )

Definition at line 942 of file BLI_string_utf8_test.cc.

References BLI_str_cursor_step_prev_utf8(), len, and pos.

◆ TEST() [25/29]

TEST ( string ,
StringNLenUTF8  )

Definition at line 346 of file BLI_string_utf8_test.cc.

References BLI_strnlen_utf8(), EXPECT_EQ(), and STR_MB_ALPHA_6.

◆ TEST() [26/29]

TEST ( string ,
StringNLenUTF8_Incomplete  )

◆ TEST() [27/29]

TEST ( string ,
Utf8AsUnicodeStep  )

Definition at line 515 of file BLI_string_utf8_test.cc.

References utf8_as_char32_test_at_buffer_size().

◆ TEST() [28/29]

TEST ( string ,
Utf8InvalidBytes  )

◆ TEST() [29/29]

TEST ( string ,
Utf8OffsetFromIndex_ClampedIndex  )

◆ utf8_as_char32()

static size_t utf8_as_char32 ( const char * str,
const char str_len,
char32_t * r_result )
static

◆ utf8_as_char32_test_at_buffer_size()

template<size_t Size>
void utf8_as_char32_test_at_buffer_size ( )

◆ utf8_as_char32_test_compare()

template<size_t Size>
void utf8_as_char32_test_compare ( const char utf8_src[Size])

◆ utf8_as_char32_test_compare_with_pad_bytes()

template<size_t Size, size_t SizeWithPadding>
void utf8_as_char32_test_compare_with_pad_bytes ( const char utf8_src[Size])

Definition at line 447 of file BLI_string_utf8_test.cc.

References EXPECT_EQ(), and utf8_as_char32().

Referenced by utf8_as_char32_test_compare().

Variable Documentation

◆ utf8_invalid_tests

const char* utf8_invalid_tests[][3]
static

Definition at line 77 of file BLI_string_utf8_test.cc.

Referenced by TEST().