Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf4f50a

Browse files
author
Edward Thomson
committed
clar: wide character comparisons
1 parent 18714e8 commit bf4f50a

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

tests/clar.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <string.h>
1212
#include <math.h>
1313
#include <stdarg.h>
14+
#include <wchar.h>
1415

1516
/* required for sandboxing */
1617
#include <sys/types.h>
@@ -525,6 +526,41 @@ void clar__assert_equal(
525526
}
526527
}
527528
}
529+
else if (!strcmp("%ls", fmt)) {
530+
const wchar_t *wcs1 = va_arg(args, const wchar_t *);
531+
const wchar_t *wcs2 = va_arg(args, const wchar_t *);
532+
is_equal = (!wcs1 || !wcs2) ? (wcs1 == wcs2) : !wcscmp(wcs1, wcs2);
533+
534+
if (!is_equal) {
535+
if (wcs1 && wcs2) {
536+
int pos;
537+
for (pos = 0; wcs1[pos] == wcs2[pos] && wcs1[pos] && wcs2[pos]; ++pos)
538+
/* find differing byte offset */;
539+
p_snprintf(buf, sizeof(buf), "'%ls' != '%ls' (at byte %d)",
540+
wcs1, wcs2, pos);
541+
} else {
542+
p_snprintf(buf, sizeof(buf), "'%ls' != '%ls'", wcs1, wcs2);
543+
}
544+
}
545+
}
546+
else if(!strcmp("%.*ls", fmt)) {
547+
const wchar_t *wcs1 = va_arg(args, const wchar_t *);
548+
const wchar_t *wcs2 = va_arg(args, const wchar_t *);
549+
int len = va_arg(args, int);
550+
is_equal = (!wcs1 || !wcs2) ? (wcs1 == wcs2) : !wcsncmp(wcs1, wcs2, len);
551+
552+
if (!is_equal) {
553+
if (wcs1 && wcs2) {
554+
int pos;
555+
for (pos = 0; wcs1[pos] == wcs2[pos] && pos < len; ++pos)
556+
/* find differing byte offset */;
557+
p_snprintf(buf, sizeof(buf), "'%.*ls' != '%.*ls' (at byte %d)",
558+
len, wcs1, len, wcs2, pos);
559+
} else {
560+
p_snprintf(buf, sizeof(buf), "'%.*ls' != '%.*ls'", len, wcs1, len, wcs2);
561+
}
562+
}
563+
}
528564
else if (!strcmp("%"PRIuZ, fmt) || !strcmp("%"PRIxZ, fmt)) {
529565
size_t sz1 = va_arg(args, size_t), sz2 = va_arg(args, size_t);
530566
is_equal = (sz1 == sz2);

tests/clar.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,15 @@ void cl_fixture_cleanup(const char *fixture_name);
7474
#define cl_assert_equal_s(s1,s2) clar__assert_equal(__FILE__,__LINE__,"String mismatch: " #s1 " != " #s2, 1, "%s", (s1), (s2))
7575
#define cl_assert_equal_s_(s1,s2,note) clar__assert_equal(__FILE__,__LINE__,"String mismatch: " #s1 " != " #s2 " (" #note ")", 1, "%s", (s1), (s2))
7676

77+
#define cl_assert_equal_wcs(wcs1,wcs2) clar__assert_equal(__FILE__,__LINE__,"String mismatch: " #wcs1 " != " #wcs2, 1, "%ls", (wcs1), (wcs2))
78+
#define cl_assert_equal_wcs_(wcs1,wcs2,note) clar__assert_equal(__FILE__,__LINE__,"String mismatch: " #wcs1 " != " #wcs2 " (" #note ")", 1, "%ls", (wcs1), (wcs2))
79+
7780
#define cl_assert_equal_strn(s1,s2,len) clar__assert_equal(__FILE__,__LINE__,"String mismatch: " #s1 " != " #s2, 1, "%.*s", (s1), (s2), (int)(len))
7881
#define cl_assert_equal_strn_(s1,s2,len,note) clar__assert_equal(__FILE__,__LINE__,"String mismatch: " #s1 " != " #s2 " (" #note ")", 1, "%.*s", (s1), (s2), (int)(len))
7982

83+
#define cl_assert_equal_wcsn(wcs1,wcs2,len) clar__assert_equal(__FILE__,__LINE__,"String mismatch: " #wcs1 " != " #wcs2, 1, "%.*ls", (wcs1), (wcs2), (int)(len))
84+
#define cl_assert_equal_wcsn_(wcs1,wcs2,len,note) clar__assert_equal(__FILE__,__LINE__,"String mismatch: " #wcs1 " != " #wcs2 " (" #note ")", 1, "%.*ls", (wcs1), (wcs2), (int)(len))
85+
8086
#define cl_assert_equal_i(i1,i2) clar__assert_equal(__FILE__,__LINE__,#i1 " != " #i2, 1, "%d", (int)(i1), (int)(i2))
8187
#define cl_assert_equal_i_(i1,i2,note) clar__assert_equal(__FILE__,__LINE__,#i1 " != " #i2 " (" #note ")", 1, "%d", (i1), (i2))
8288
#define cl_assert_equal_i_fmt(i1,i2,fmt) clar__assert_equal(__FILE__,__LINE__,#i1 " != " #i2, 1, (fmt), (int)(i1), (int)(i2))

0 commit comments

Comments
 (0)