#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <string.h>

/* 
 * This program concatenates two wide-character strings using
 * the wcscat function, and then manually compares the result
 * to the expected result.
 */

#define S1LENGTH 10
#define S2LENGTH 8

int main(void) {
	int i;
	wchar_t s1buf[S1LENGTH + S2LENGTH];
	wchar_t s2buf[S2LENGTH];
	wchar_t test1[S1LENGTH + S2LENGTH];
	
	/* Initialize the three wide-character strings */
	
	if (mbstowcs(s1buf, "abcmnexyz", S1LENGTH) == (size_t)-1) {
	   perror("mbstowcs");
	   exit(-1);
	}
	
	if (mbstowcs(s2buf, " orthis", S2LENGTH) == (size_t)-1) {	
	   perror("mbstowcs");
	   exit(-1);
	}
	
	if (mbstowcs(test1, "abcmnexyz orthis", S1LENGTH + S2LENGTH)  == (size_t)-1) {
	   perror("mbstowcs");
	   exit(-1);
	}
	
	/* 
	 * Concatenate s1buf with s2buf, placing the result into
	 * s1buf.  Then compare s1buf with the expected result 
	 * in test1.
	 */	
	wcscat(s1buf, s2buf);
	
	for (i = 0; i < S1LENGTH + S2LENGTH - 2; i++) {
	   /* Check that each character is correct */
	   if (test1[i] != s1buf[i]) {
		   printf("Error in wcscat\n");
		   exit(-1);
	   }
	}
	
	printf("Concatenated string: <%S>\n", s1buf);
}