PostgreSQL Source Code  git master
case_test.c File Reference
#include "postgres_fe.h"
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wctype.h>
#include "common/unicode_case.h"
#include "common/unicode_category.h"
#include "common/unicode_version.h"
Include dependency graph for case_test.c:

Go to the source code of this file.

Functions

static void test_strlower (const char *test_string, const char *expected)
 
static void test_convert_case ()
 
int main (int argc, char **argv)
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 183 of file case_test.c.

184 {
185  printf("case_test: Postgres Unicode version:\t%s\n", PG_UNICODE_VERSION);
186 #ifdef USE_ICU
187  printf("case_test: ICU Unicode version:\t\t%s\n", U_UNICODE_VERSION);
188  test_icu();
189 #else
190  printf("case_test: ICU not available; skipping\n");
191 #endif
192 
194  exit(0);
195 }
static void test_convert_case()
Definition: case_test.c:170
exit(1)
#define printf(...)
Definition: port.h:244
#define PG_UNICODE_VERSION

References exit(), PG_UNICODE_VERSION, printf, and test_convert_case().

◆ test_convert_case()

static void test_convert_case ( )
static

Definition at line 170 of file case_test.c.

171 {
172  /* test string with no case changes */
173  test_strlower("√∞", "√∞");
174  /* test string with case changes */
175  test_strlower("ABC", "abc");
176  /* test string with case changes and byte length changes */
177  test_strlower("ȺȺȺ", "ⱥⱥⱥ");
178 
179  printf("case_test: convert_case: success\n");
180 }
static void test_strlower(const char *test_string, const char *expected)
Definition: case_test.c:89

References printf, and test_strlower().

Referenced by main().

◆ test_strlower()

static void test_strlower ( const char *  test_string,
const char *  expected 
)
static

Definition at line 89 of file case_test.c.

90 {
91  size_t src1len = strlen(test_string);
92  size_t src2len = -1; /* NUL-terminated */
93  size_t dst1len = strlen(expected);
94  size_t dst2len = strlen(expected) + 1; /* NUL-terminated */
95  char *src1 = malloc(src1len);
96  char *dst1 = malloc(dst1len);
97  char *src2 = strdup(test_string);
98  char *dst2 = malloc(dst2len);
99  size_t needed;
100 
101  memcpy(src1, test_string, src1len); /* not NUL-terminated */
102 
103  /* neither source nor destination are NUL-terminated */
104  memset(dst1, 0x7F, dst1len);
105  needed = unicode_strlower(dst1, dst1len, src1, src1len);
106  if (needed != strlen(expected))
107  {
108  printf("case_test: convert_case test1 FAILURE: needed %zu\n", needed);
109  exit(1);
110  }
111  if (memcmp(dst1, expected, dst1len) != 0)
112  {
113  printf("case_test: convert_case test1 FAILURE: test: '%s' result: '%.*s' expected: '%s'\n",
114  test_string, (int) dst1len, dst1, expected);
115  exit(1);
116  }
117 
118  /* destination is NUL-terminated and source is not */
119  memset(dst2, 0x7F, dst2len);
120  needed = unicode_strlower(dst2, dst2len, src1, src1len);
121  if (needed != strlen(expected))
122  {
123  printf("case_test: convert_case test2 FAILURE: needed %zu\n", needed);
124  exit(1);
125  }
126  if (strcmp(dst2, expected) != 0)
127  {
128  printf("case_test: convert_case test2 FAILURE: test: '%s' result: '%s' expected: '%s'\n",
129  test_string, dst2, expected);
130  exit(1);
131  }
132 
133  /* source is NUL-terminated and destination is not */
134  memset(dst1, 0x7F, dst1len);
135  needed = unicode_strlower(dst1, dst1len, src2, src2len);
136  if (needed != strlen(expected))
137  {
138  printf("case_test: convert_case test3 FAILURE: needed %zu\n", needed);
139  exit(1);
140  }
141  if (memcmp(dst1, expected, dst1len) != 0)
142  {
143  printf("case_test: convert_case test3 FAILURE: test: '%s' result: '%.*s' expected: '%s'\n",
144  test_string, (int) dst1len, dst1, expected);
145  exit(1);
146  }
147 
148  /* both source and destination are NUL-terminated */
149  memset(dst2, 0x7F, dst2len);
150  needed = unicode_strlower(dst2, dst2len, src2, src2len);
151  if (needed != strlen(expected))
152  {
153  printf("case_test: convert_case test4 FAILURE: needed %zu\n", needed);
154  exit(1);
155  }
156  if (strcmp(dst2, expected) != 0)
157  {
158  printf("case_test: convert_case test4 FAILURE: test: '%s' result: '%s' expected: '%s'\n",
159  test_string, dst2, expected);
160  exit(1);
161  }
162 
163  free(src1);
164  free(dst1);
165  free(src2);
166  free(dst2);
167 }
#define free(a)
Definition: header.h:65
#define malloc(a)
Definition: header.h:50
size_t unicode_strlower(char *dst, size_t dstsize, const char *src, ssize_t srclen)
Definition: unicode_case.c:69

References exit(), free, malloc, printf, and unicode_strlower().

Referenced by test_convert_case().