Commit a6d2f914 authored by Mike Iovine's avatar Mike Iovine
Browse files

Update Huffman tests

parent 55059111
No related merge requests found
Pipeline #30561 failed with stage
in 0 seconds
Showing with 76 additions and 4 deletions
+76 -4
No preview for this file type
......@@ -25,8 +25,11 @@ typedef struct huffman {
/* Reset the position state variable */
void reset_pos();
/* Create a Huffman mapping from a sequence of lengths. */
huffman_t *make_huffman(int *lens, int *alphabet, int n_symbols);
/*
* Create a Huffman mapping from a sequence of lengths.
* The alphabet used is (0, 1, ..., n_symbols - 1)
*/
huffman_t *make_huffman(int *lens, int n_symbols);
/* Clean up a heap allocated Huffman map */
void destroy_huffman(huffman_t *hf);
......
......@@ -78,10 +78,79 @@ TEST (BufferTests, RevStreamOrderTest) {
ASSERT_EQ (get_n_bits((char *) buf, 6, true), 36);
}
/* Test making a huffman tree from a sequence of lengths */
TEST (HuffmanTests, TestMakeHuffman) {
int n_symbols = 8;
int lens[8] = {3, 3, 3, 3, 3, 2, 4, 4};
huffman_t *hf = make_huffman(lens, n_symbols);
int bl_counts[MAX_LENGTH + 1] = {0, 0, 1, 5, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
/* Check bl_counts */
for (int i = 0; i < MAX_LENGTH + 1; i++) {
ASSERT_EQ (hf->bl_counts[i], bl_counts[i]);
}
/*
* Check min_codes for entries with non-zero bl_count
* It doesn't really matter what the other entries are.
*/
ASSERT_EQ (hf->min_codes[2], 0);
ASSERT_EQ (hf->min_codes[3], 2);
ASSERT_EQ (hf->min_codes[4], 14);
/* Check that the alphabet arrays were populated correctly. */
ASSERT_EQ (hf->alphabet[2][0], 5);
for (int i = 0; i < 5; i++) ASSERT_EQ (hf->alphabet[3][i], i);
ASSERT_EQ (hf->alphabet[4][0], 6);
ASSERT_EQ (hf->alphabet[4][1], 7);
destroy_huffman(hf);
}
/*
* Test that make_huffman does not assign a code to symbols with
* a length of 0.
*/
TEST (HuffmanTests, TestMakeHuffmanZeroLens) {
int n_symbols = 8;
int lens[8] = {1, 0, 2, 3, 3, 0, 4, 3};
huffman_t *hf = make_huffman(lens, n_symbols);
int bl_counts[MAX_LENGTH + 1] = {0, 1, 1, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
/* Check bl_counts */
for (int i = 0; i < MAX_LENGTH + 1; i++) {
ASSERT_EQ (hf->bl_counts[i], bl_counts[i]);
}
/* Check min_codes */
ASSERT_EQ (hf->min_codes[1], 0);
ASSERT_EQ (hf->min_codes[2], 2);
ASSERT_EQ (hf->min_codes[3], 6);
ASSERT_EQ (hf->min_codes[4], 18);
/* Check alphabet. */
ASSERT_EQ (hf->alphabet[1][0], 0);
/* 1 and 5 are not used! (length = 0) */
ASSERT_EQ (hf->alphabet[2][0], 2);
ASSERT_EQ (hf->alphabet[3][0], 3);
ASSERT_EQ (hf->alphabet[3][1], 4);
ASSERT_EQ (hf->alphabet[3][2], 7);
ASSERT_EQ (hf->alphabet[4][0], 6);
destroy_huffman(hf);
}
// TODO
TEST (HuffmanTests, TestMakeHuffman) {}
TEST (HuffmanTests, TestReadChunk) {
TEST (HuffmanTests, TestReadChunk) {}
}
TEST (HuffmanTests, TestReadLens) {}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment