/* Produce output for LZ78 Compressor
 *
 *
 * Input :	out			:	out is the pointer to OutStream
 *			outputByte	:	outputByte is the byte of data to be output
 *			index		: 	index is the dictionary index
 *			EOFstate	: 	EOFstate is used to prevent writing invalid data into the file
 *			mode 		:	Fixed -> fixed output dictionary index to 16bits
 *							Variable -> use just sufficient number of bits to represent the dictionary index
 */
void LZ78_Output(Dictionary *dictionary,OutStream *out,unsigned char outputByte,int index,int EOFstate, int mode)
{
    int bitsRequired = 0 ;

    if (mode == Fixed)
        bitsRequired = 16 ;
    else if (mode == Variable)
        bitsRequired = determineNumberOfBitsRequired(index,dictionary->currentIndex);

    streamWriteBits(out,index,bitsRequired);
    if (EOFstate == 0 ) // prevent writing EOF to file
        streamWriteBits(out,(unsigned int)(outputByte),8);
}
HuffmanNode *buildAndAddNewHuffmanTree(OutStream *out, HuffmanNode *node, HuffmanNode *arraySymbol[], uint32 symb){
  HuffmanNode *NewNode = node;
  emitPathCode(out,NewNode);
  streamWriteBits(out,(unsigned char)symb);
  NewNode = adaptiveHuffmanTreeBuild(NewNode,symb);
  huffmanUpdateAndRestructure(NewNode->parent->parent);
  arraySymbol[symb] = NewNode->parent->rightChild;

  return NewNode;
}
void test_streamWriteBits_given_value_8_bitSize_4_should_write_8_to_byteToWrite_return_bitIndex_4()
{
    OutStream *out = initOutStream();
    
    streamWriteBits(out,8,4);

    TEST_ASSERT_EQUAL(8,out->byteToWrite);
    TEST_ASSERT_EQUAL(4,out->bitIndex);
    
    freeOutStream(out);
}
void test_streamWriteBits_given_A_bitSize_8_should_flush_A_during_closeOutStream()
{
    OutStream *out = initOutStream();
    
    out = openOutStream("test/support/test_streamWriteBits.txt", "wb" , out);
    
    streamWriteBits(out,'A',8);
    
    TEST_ASSERT_EQUAL('A',out->byteToWrite);
    TEST_ASSERT_EQUAL(8,out->bitIndex);
    
    closeOutStream(out);
   
    TEST_ASSERT_EQUAL(0,out->byteToWrite);
    TEST_ASSERT_EQUAL(0,out->bitIndex);
    
    freeOutStream(out);
}
void test_streamWriteBits_given_ABCDEFGH_bitSize_64_should_flush_ABCDEFGH()
{
    char *string = "ABCDEFGH" ;
    
    int i ;
    OutStream *out = initOutStream();
    
    out = openOutStream("test/support/test_streamWriteBits_1.txt", "wb" , out);

    for( i = 0 ; i < strlen(string) ; i ++ )
        streamWriteBits(out,string[i],8);

    closeOutStream(out);
    
    TEST_ASSERT_EQUAL(0,out->byteToWrite);
    TEST_ASSERT_EQUAL(0,out->bitIndex);
    
    freeOutStream(out);
}