void test_streamReadBits_given_a_0x804040_should_return_97_and_257(){ CEXCEPTION_T e; InStream *in; int result, result2; Try{ in = openInStream("test/data/InputTest_6.txt", "r"); }Catch(e){ TEST_ASSERT_EQUAL(ERR_CANNOT_OPEN_FILE, e); } Try{ result = streamReadBits(in, 8); result2 = streamReadBits(in, 9); }Catch(e){ TEST_ASSERT_EQUAL(END_OF_STREAM, e); } TEST_ASSERT_EQUAL(1, in->bitIndex); TEST_ASSERT_EQUAL(0, in->currentByte); closeInStream(in); TEST_ASSERT_EQUAL(97, result); TEST_ASSERT_EQUAL(256, result2); }
void test_streamReadBits_given_a_0x06_0x20_0x61_0x06_0xe0_should_return_98_97_110(){ CEXCEPTION_T e; InStream *in; int result, result2, result3; tempCurrentByte = 0; Try{ in = openInStream("test/data/InputTest_7.txt", "r"); }Catch(e){ TEST_ASSERT_EQUAL(ERR_CANNOT_OPEN_FILE, e); } Try{ result = streamReadBits(in, 12); result2 = streamReadBits(in, 12); result3 = streamReadBits(in, 12); }Catch(e){ TEST_ASSERT_EQUAL(END_OF_STREAM, e); } TEST_ASSERT_EQUAL(4, in->bitIndex); TEST_ASSERT_EQUAL(0, in->currentByte); closeInStream(in); TEST_ASSERT_EQUAL(98, result); TEST_ASSERT_EQUAL(97, result2); TEST_ASSERT_EQUAL(110, result3); }
/* This function is used to determine the file size of * two input files * * Inputs: * infilename1 the filename for input file 1 * infilename2 the filename for input file 2 * * Return: * 1 if the two files have the same size * 0 if the two files have a different size */ int verifyDecompressedFile(char *infilename1, char *infilename2) { unsigned int value1, value2; int counter1 = -1, counter2 = -1; InStream *in1, *in2; in1 = initInStream(); in2 = initInStream(); in1 = openInStream(infilename1, "rb+" , in1); in2 = openInStream(infilename2, "rb+" , in2); do { value1 = streamReadBits(in1, 8); counter1++; }while(!checkEndOfFile(in1) ); do { value2 = streamReadBits(in2, 8); counter2++; }while(!checkEndOfFile(in2) ); closeInStream(in1); closeInStream(in2); freeInStream(in1); freeInStream(in2); if( counter2 == counter1) return 1; else return 0; }
void assertFile(char *Source, char *Target,int lineNumber) { long unsigned int srcFileSize, targetFileSize, position ; unsigned int srcData,targetData ,srcfileStatus = 0,targetFileStatus = 0; char ErrorMsg[1024] ={}; InStream *src = initInStream(); InStream *target = initInStream(); src = openInStream(Source, "rb" , src); target = openInStream(Target, "rb" , target); fseek(src->file,0,SEEK_END); fseek(target->file,0,SEEK_END); srcFileSize = ftell(src->file); targetFileSize = ftell(target->file); UNITY_TEST_ASSERT_EQUAL_UINT32(srcFileSize,targetFileSize,lineNumber,"The target file size does not match the source file size!"); rewind(src->file); rewind(target->file); while(1) { srcData = streamReadBits(src,8); targetData = streamReadBits(target,8); srcfileStatus = checkEndOfFile(src) ; targetFileStatus = checkEndOfFile(target); if(srcfileStatus) { closeInStream(src); freeInStream(src); } if(targetFileStatus) { closeInStream(target); freeInStream(target); } UNITY_TEST_ASSERT_EQUAL_UINT8(srcfileStatus,targetFileStatus,lineNumber,"Target file ended earlier or later than the source file!"); if (srcData != targetData) { position = ftell(target->file); sprintf(ErrorMsg,"At position 0x%x, target data does not match source data!",position); UNITY_TEST_ASSERT_EQUAL_UINT8(srcData,targetData,lineNumber,ErrorMsg); } if(srcfileStatus == 1 || targetFileStatus == 1) break; } }
/* Continuously read byte and find last match of the data in the dictionary * * Input : dictionary : dictionary is the pointer to the LZ78 dictionary which contains the data to be compared * in : in is the pointer to InStream for reading a byte purpose * dataString : dataString is used later to add the missing data into the dictionary * dataStringSize : dataStringSize is used to store the size of the string and will be recorded into dictionaryEntry * readByte : readByte is used to output the byte of data in LZ78_Output * returnedIndex : returnedIndex contains the first index of the match dictionaryEntry * EOFstate : EOFstate is used to remember EOF has been encountered * * Output : Return index of last match entry * */ int findLastMatchEntry(Dictionary *dictionary, InStream *in,unsigned char *dataString,int *dataStringSize,unsigned char *readByte, int returnedIndex, int *EOFstate) { int lastIndex = returnedIndex ; // store the index of first match in dictionaryEntry memset (dataString,0,1024); //clear dataString copy_DictionaryDataInputData(dataString,dictionary,lastIndex); //merge input character with data in dictionary *dataStringSize = dictionary->Entry[lastIndex].entrySize ; // get the size of dataString while(returnedIndex != -1) { readByte[0] = (unsigned char)(streamReadBits(in,8)) ;// read next character if (checkEndOfFile(in)) //if EOF detected { *EOFstate = 1 ; // use to remember EOF encountered for later uses returnedIndex = -1 ; //quit loop } else { memcpy( (dataString+*dataStringSize),readByte,1); //add next character to dataString *dataStringSize += 1; //increment dataStringSize returnedIndex = compare_DictionaryData(dataString,dictionary,*dataStringSize); //check again is there any matched data if (returnedIndex != -1 ) // if there is still existing a match in dictionaryEntry lastIndex = returnedIndex ; // store the index of last match in dictionaryEntry } } return lastIndex ; }
void test_streamReadBits_given_byteToRead_20_bitSize_5_should_return_20() { InStream *in = initInStream(); in->byteToRead = 20 ; in->bitIndex = 0 ; TEST_ASSERT_EQUAL(20,streamReadBits(in,5)); TEST_ASSERT_EQUAL(5,in->bitIndex); freeInStream(in); }
void test_streamReadBits_given_a_0x10_0x01_0x01_should_return_256_and_257(){ CEXCEPTION_T e; InStream *in; int result, result2; tempCurrentByte = 0; Try{ in = openInStream("test/data/InputTest_5.txt", "r"); }Catch(e){ TEST_ASSERT_EQUAL(ERR_CANNOT_OPEN_FILE, e); } result = streamReadBits(in, 12); result2 = streamReadBits(in, 12); TEST_ASSERT_EQUAL(0, in->bitIndex); TEST_ASSERT_EQUAL(0, in->currentByte); closeInStream(in); TEST_ASSERT_EQUAL(256, result); TEST_ASSERT_EQUAL(257, result2); }
void test_streamReadBits_given_bitSize_16_storageType_int_should_extract_data_from_the_file() { int storage; int i ; InStream *in = initInStream(); in = openInStream("test/support/test_streamReadBits.txt", "rb" , in); storage = streamReadBits(in,16) ; TEST_ASSERT_EQUAL(0x4241,storage); /*Text file in little endian format*/ closeInStream(in); freeInStream(in); }
void test_streamReadBits_given_bitSize_64_storageType_string_should_extract_data_from_the_file() { char storage[10] = {}; int i ; InStream *in = initInStream(); in = openInStream("test/support/test_streamReadBits.txt", "rb" , in); for ( i = 0 ; i < 8 ; i ++ ) storage[i] = (char)streamReadBits(in,8) ; TEST_ASSERT_EQUAL_STRING("ABCDEFGH",storage); closeInStream(in); freeInStream(in); }
HuffmanNode *huffmanCompress(InStream *in, OutStream *out){ HuffmanNode *returnedNewNode; HuffmanNode *NodeForCase = NULL; root = adaptiveHuffmanTreeInit(); root->order = Symbol; uint32 Symb = 0; HuffmanNode *arraySymbol[Symbol]; clearArraySymbol(arraySymbol); fflush(stdout); while(!feof(in->file)){ Symb = streamReadBits(in); if(!arraySymbol[Symb]){ if (!Symb){ //EOF, object file break; } if(!NodeForCase){ // First Unseen Symbol returnedNewNode = buildAndAddNewHuffmanTree(out,root,arraySymbol,Symb); fflush(out->file); NodeForCase = returnedNewNode; } else{ // Following Unseen Symbol returnedNewNode = buildAndAddNewHuffmanTree(out,returnedNewNode,arraySymbol,Symb); fflush(out->file); } } else{ // Symbol Seen before emitPathCode(out,arraySymbol[Symb]); huffmanUpdateAndRestructure(arraySymbol[Symb]); fflush(out->file); } } while (streamOut.bitIndex != 7){ //fill remaining with 0 streamWriteBit(out , 0); fflush(out->file); } clearArraySymbol(arraySymbol); fflush(stdout); fflush(out->file); fflush(in->file); return returnedNewNode; freeNodes(root); root = NULL; }
/* Modified LZ78 Compressor * * Index 0 is used to represent EOF for decompression usage * Index 1 is used to represent data not found in dictionary * * Input : dictionary : dictionary is the pointer to the LZ78 dictionary * in : in is the pointer to InStream * out : out is the pointer to OutStream * mode : Fixed -> fixed output dictionary index to 16bits * Variable -> use just sufficient number of bits to represent the dictionary index */ void LZ78_Compressor(Dictionary *dictionary, InStream *in, OutStream *out, int mode) { unsigned char readByte[2] ={}, dataString[1024] ; int returnedIndex, lastIndex, EOFstate = 0 , i = 0 ,dataStringSize; while (1) { readByte[0] = (unsigned char)(streamReadBits(in,8)); if (checkEndOfFile(in)) // if EOF encountered break; // break loop if(isDictionaryFull(dictionary)) refreshDictionaryEntryData(dictionary,dictionary->dictionarySize); if(isDictionaryEmpty(dictionary)) // if dictionary is empty { addEntryData(dictionary, readByte,1); // directly add it into dictionary LZ78_Output(dictionary,out,readByte[0],1,EOFstate,mode); // output (1,x) *without () } else // dictionary is not empty { returnedIndex = compare_DictionaryData(readByte,dictionary,1); //check is there any matched data in dictionaryEntry if ( returnedIndex >= 0 ) // if true { lastIndex = findLastMatchEntry(dictionary,in,dataString,&dataStringSize,readByte,returnedIndex, &EOFstate); if (EOFstate != 1)//prevent adding EOF into dictionary addEntryData(dictionary,dataString,dataStringSize); // add dataString into dictionary LZ78_Output(dictionary,out,readByte[0],lastIndex+2,EOFstate,mode); // produce output (lastIndex+1 , X) *without () } else // no matched data { addEntryData(dictionary,readByte,1); LZ78_Output(dictionary,out,readByte[0],1,EOFstate,mode); // output (1,x) *without () } } if (EOFstate == 1) //EOF encountered previously break; } }
void test_streamReadBits_given_a_0x06_0x10_should_return_a(){ CEXCEPTION_T e; InStream *in; int result; tempCurrentByte = 0; Try{ in = openInStream("test/data/InputTest_3.txt", "r"); }Catch(e){ TEST_ASSERT_EQUAL(ERR_CANNOT_OPEN_FILE, e); } Try{ result = streamReadBits(in, 12); }Catch(e){ TEST_ASSERT_EQUAL(END_OF_STREAM, e); } TEST_ASSERT_EQUAL(4, in->bitIndex); TEST_ASSERT_EQUAL(0, in->currentByte); closeInStream(in); TEST_ASSERT_EQUAL('a', result); }