glib/tests/base64-test.c
Hans Breuer 770b29bad2 define PCRE_STATIC to reflect the inclusion of pcre as LIB, not
2007-03-17  Hans Breuer  <hans@breuer.org>

	* glib/makefile.msc.in glib/pcre/makefile.msc 
	  glib/update-pcre/update.sh : define PCRE_STATIC to reflect the 
	inclusion of pcre as LIB, not stand-alone DLL. Also set NEWLINE=-1
	to match any newline by default, use of ../../build/win32/make.msc

	* glib/gregex.h : minimal includes of <glib/*.H> instead of <glib.h>

	* glib/gnulib/makefile.msc : make use of ../../build/win32/make.msc

	* tests/regex-test.c(verbose): don't pass a string containing '%' 
	as first parameter to g_print ()
	(test_match) : for the unexpected case output pattern and string
	escaped

	* tests/child-test.c tests/slice-color.c : fix c99ism
	* tests/slice-test.c : fix c99ism and gccism
	* tests/mapping-test.c tests/base-64-tests.c : don't 
	#include <unistd.h> unconditionally
	* tests/option-test.c : use G_GINT64_CONSTANT() instead of direct LL

	* tests/makefile.msc.in : more tests build


svn path=/trunk/; revision=5423
2007-03-17 09:49:09 +00:00

132 lines
2.5 KiB
C

#include "config.h"
#include <glib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdlib.h>
#define DATA_SIZE 1024
#define BLOCK_SIZE 32
#define NUM_BLOCKS 32
static guchar data[DATA_SIZE];
static void
test_incremental (gboolean line_break,
gint length)
{
char *p;
gsize len, decoded_len, max, input_len, block_size;
int state, save;
guint decoder_save;
char *text;
guchar *data2;
data2 = g_malloc (length);
text = g_malloc (length * 2);
len = 0;
state = 0;
save = 0;
input_len = 0;
while (input_len < length)
{
block_size = MIN (BLOCK_SIZE, length - input_len);
len += g_base64_encode_step (data + input_len, block_size,
line_break, text + len, &state, &save);
input_len += block_size;
}
len += g_base64_encode_close (line_break, text + len, &state, &save);
if (line_break)
max = length * 4 / 3 + length * 4 / (3 * 72) + 7;
else
max = length * 4 / 3 + 6;
if (len > max)
{
g_print ("Too long encoded length: got %d, expected max %d\n",
len, max);
exit (1);
}
decoded_len = 0;
state = 0;
decoder_save = 0;
p = text;
while (len > 0)
{
int chunk_len = MIN (BLOCK_SIZE, len);
decoded_len += g_base64_decode_step (p,
chunk_len,
data2 + decoded_len,
&state, &decoder_save);
p += chunk_len;
len -= chunk_len;
}
if (decoded_len != length)
{
g_print ("Wrong decoded length: got %d, expected %d\n",
decoded_len, length);
exit (1);
}
if (memcmp (data, data2, length) != 0)
{
g_print ("Wrong decoded base64 data\n");
exit (1);
}
g_free (text);
g_free (data2);
}
static void
test_full (void)
{
char *text;
guchar *data2;
gsize len;
text = g_base64_encode (data, DATA_SIZE);
data2 = g_base64_decode (text, &len);
g_free (text);
if (len != DATA_SIZE)
{
g_print ("Wrong decoded length: got %d, expected %d\n",
len, DATA_SIZE);
exit (1);
}
if (memcmp (data, data2, DATA_SIZE) != 0)
{
g_print ("Wrong decoded base64 data\n");
exit (1);
}
g_free (data2);
}
int
main (int argc, char *argv[])
{
int i;
for (i = 0; i < DATA_SIZE; i++)
data[i] = (guchar)i;
test_full ();
test_incremental (FALSE, DATA_SIZE);
test_incremental (TRUE, DATA_SIZE);
test_incremental (FALSE, DATA_SIZE - 1);
test_incremental (TRUE, DATA_SIZE - 1);
test_incremental (FALSE, DATA_SIZE - 2);
test_incremental (TRUE, DATA_SIZE - 2);
return 0;
}