Skip to content

Instantly share code, notes, and snippets.

@nemequ
Created September 6, 2013 05:56
Show Gist options
  • Save nemequ/6460041 to your computer and use it in GitHub Desktop.
Save nemequ/6460041 to your computer and use it in GitHub Desktop.
Squash random data unit test
From 690e3fd92c73d6e8da391529b5cc5e81d81513ea Mon Sep 17 00:00:00 2001
From: Evan Nemerson <evan@coeus-group.com>
Date: Thu, 5 Sep 2013 22:53:15 -0700
Subject: [PATCH] tests: add random data unit test
Signed-off-by: Evan Nemerson <evan@coeus-group.com>
---
tests/check-plugins.c | 36 ++++++++++++++++++++++++++++++++++++
1 file changed, 36 insertions(+)
diff --git a/tests/check-plugins.c b/tests/check-plugins.c
index 9d75b4f..e1a5d53 100644
--- a/tests/check-plugins.c
+++ b/tests/check-plugins.c
@@ -268,6 +268,38 @@ check_stream_decompress (gconstpointer user_data) {
free (decompressed);
}
+static void
+check_random_data (gconstpointer user_data) {
+ SquashCodec* codec = (SquashCodec*) user_data;
+ size_t uncompressed_length = g_test_rand_int_range (32, 4096);
+ uint8_t* uncompressed_data = (uint8_t*) malloc (uncompressed_length);
+ size_t compressed_length = squash_codec_get_max_compressed_size (codec, uncompressed_length);
+ uint8_t* compressed_data = (uint8_t*) malloc (compressed_length);
+ size_t decompressed_length = uncompressed_length;
+ uint8_t* decompressed_data = (uint8_t*) malloc (decompressed_length);
+ unsigned int i;
+ SquashStatus res;
+
+ for ( i = 0 ; i < uncompressed_length ; i++ ) {
+ uncompressed_data[i] = (uint8_t) g_test_rand_int_range (0x00, 0xff);
+ }
+
+ fprintf (stderr, "squash_codec_get_max_compressed_size (%zu) == %zu\n", uncompressed_length, compressed_length);
+ res = squash_codec_compress_with_options (codec, compressed_data, &compressed_length, (uint8_t*) uncompressed_data, uncompressed_length, NULL);
+ SQUASH_ASSERT_OK(res);
+ fprintf (stderr, "Actual compressed length: %zu\n", compressed_length);
+
+ res = squash_codec_decompress_with_options (codec, decompressed_data, &decompressed_length, compressed_data, compressed_length, NULL);
+ SQUASH_ASSERT_OK(res);
+ g_assert (decompressed_length == uncompressed_length);
+
+ g_assert (memcmp (uncompressed_data, decompressed_data, uncompressed_length) == 0);
+
+ free (uncompressed_data);
+ free (compressed_data);
+ free (decompressed_data);
+}
+
static void squash_check_codec_enumerator_codec_cb (SquashCodec* codec, void* data) {
gchar* test_name = NULL;
@@ -289,6 +321,10 @@ static void squash_check_codec_enumerator_codec_cb (SquashCodec* codec, void* da
test_name = g_strdup_printf ("/plugins/%s/stream/decompress", squash_codec_get_name (codec));
g_test_add_data_func (test_name, codec, check_stream_decompress);
g_free (test_name);
+
+ test_name = g_strdup_printf ("/plugins/%s/random-data", squash_codec_get_name (codec));
+ g_test_add_data_func (test_name, codec, check_random_data);
+ g_free (test_name);
}
static void squash_check_codec_enumerator_plugin_cb (SquashPlugin* plugin, void* data) {
--
1.8.3.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment