Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to write a tester for a program that writes one bit at a time?
    primarykey
    data
    text
    <p>I am trying to write a tester for the following program to see if it functions correctly, however, I'm not sure if I implemented flush() correctly and for some reason I don't get any output. Can someone suggest code that will test this class to see if I implemented flush and writeBit correctly? </p> <pre><code>#ifndef BITOUTPUTSTREAM_HPP #define BITOUTPUTSTREAM_HPP #include &lt;iostream&gt; class BitOutputStream { private: char buf; // one byte buffer of bits int nbits; // how many bits have been written to buf std::ostream&amp; out; // reference to the output stream to use public: /* Initialize a BitOutputStream that will * use the given ostream for output. * */ BitOutputStream(std::ostream&amp; os) : out(os) { buf = nbits = 0; // clear buffer and bit counter } /* Send the buffer to the output, and clear it */ void flush() { out.put(buf); // EDIT: removed flush(); to stop the infinite recursion buf = nbits = 0; } /* Write the least sig bit of arg into buffer */ int writeBit(int i) { // If bit buffer is full, flush it. if (nbits == 8) flush(); // Write the least significant bit of i into // the buffer at the current index. // buf = buf &lt;&lt; 1; this is another option I was considering // buf |= 1 &amp; i; but decided to go with the one below int lb = i &amp; 1; // extract the lowest bit buf |= lb &lt;&lt; nbits; // shift it nbits and put in in buf // increment index nbits++; return nbits; } }; #endif // BITOUTPUTSTREAM_HPP </code></pre> <p>What I wrote as a tester is:</p> <pre><code>#include "BitOutputStream.hpp" #include &lt;iostream&gt; int main(int argc, char* argv[]) { BitOutputStream bos(std::cout); // channel output to stdout bos.writeBit(1); // Edit: added lines below bos.writeBit(0); bos.writeBit(0); bos.writeBit(0); bos.writeBit(0); bos.writeBit(0); bos.writeBit(0); bos.writeBit(1); // now prints an 'A' ;) return 0; } </code></pre> <p>I know this is wrong since I get no output and have no way to see if the implementation is correct. I appreciate any input you can provide.</p> <p>I compiled the code with: g++ -std=c++11 main.cpp BioOutputStream.hpp BitInputStream.cpp and ran it with: ./a.out</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload