Skip to content

Commit

Permalink
Add test to validate we can survive giving the fountain sink the wron…
Browse files Browse the repository at this point in the history
…g data

... since we're using that as the detection mechanism for auto-detect.

Also, a simplification to Decoder::do_decode()
  • Loading branch information
sz3 committed Mar 1, 2024
1 parent 7b11358 commit f5607a3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/lib/encoder/Decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ inline unsigned Decoder::do_decode(CimbReader& reader, STREAM& ostream, bool leg
colorPositions.resize(reader.num_reads()); // the number of cells == reader.num_reads(). Can we calculate this from config at compile time? Do we care?

unsigned bitsPerSymbol = cimbar::Config::symbol_bits();
unsigned bytesDecoded = 0;
{
bitbuffer symbolBits(cimbar::Config::capacity(bitsPerSymbol));
// read symbols first
Expand Down Expand Up @@ -116,8 +115,8 @@ inline unsigned Decoder::do_decode(CimbReader& reader, STREAM& ostream, bool leg
}

reed_solomon_stream rss(ostream, _eccBytes, _eccBlockSize);
bytesDecoded += colorBits.flush(rss); // will return the pos after this flush(), which includes the previous flush()...
return bytesDecoded;
// flush() will return the (good) cumulative bytes written to the underlying stream
return colorBits.flush(rss);
}

template <typename STREAM>
Expand Down
36 changes: 36 additions & 0 deletions src/lib/encoder/test/EncoderRoundTripTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,42 @@ TEST_CASE( "EncoderRoundTripTest/testFountain.Pad", "[unit]" )

std::string decodedContents = File(tempdir.path() / "0.626").read_all();
assertEquals( "hello", decodedContents );

assertEquals( 1, fds.num_done() );
}

TEST_CASE( "EncoderRoundTripTest/testFountain.SinkMismatch", "[unit]" )
{
MakeTempDirectory tempdir;

std::string inputFile = tempdir.path() / "hello.txt";
std::string outPrefix = tempdir.path() / "encoder.fountain";

{
std::ofstream f(inputFile);
f << "hello"; // 5 bytes!
}

// will be padded so the fountain encoding is happy. The encoded image looks suspiciously non-random!
Encoder enc(30, 4, 2);
assertEquals( 1, enc.encode_fountain(inputFile, outPrefix) );

uint64_t hash = 0xeecc8800efce8c48;
std::string path = fmt::format("{}_0.png", outPrefix);
cv::Mat encodedImg = cv::imread(path);
cv::cvtColor(encodedImg, encodedImg, cv::COLOR_BGR2RGB);
assertEquals( hash, image_hash::average_hash(encodedImg) );

// decoder
Decoder dec(30);
// sink with a mismatched fountain_chunk_size
// importantly, the sink expects a *larger* chunk than we'll give it...
fountain_decoder_sink<cimbar::zstd_decompressor<std::ofstream>> fds(tempdir.path(), cimbar::Config::fountain_chunk_size(30, 6, true));

unsigned bytesDecoded = dec.decode_fountain(encodedImg, fds, 1);
assertEquals( 7500, bytesDecoded );

assertEquals( 0, fds.num_done() );
}

TEST_CASE( "EncoderRoundTripTest/testStreaming", "[unit]" )
Expand Down

0 comments on commit f5607a3

Please sign in to comment.