srctree

Robin Linden parent 5cefccc1 86d8b669
img: Allow creating pngs from rvalues

inlinesplit
img/png.h added: 6, removed: 8, total 0
@@ -14,6 +14,7 @@ namespace img {
 
class Png {
public:
static std::optional<Png> from(std::istream &&is) { return from(is); }
static std::optional<Png> from(std::istream &is);
 
std::uint32_t width{};
 
img/png_test.cpp added: 6, removed: 8, total 0
@@ -33,22 +33,19 @@ int main() {
return expected;
}();
 
auto ss = std::stringstream(png_bytes);
auto png = img::Png::from(ss).value();
auto png = img::Png::from(std::stringstream(png_bytes)).value();
expect_eq(png, img::Png{.width = 256, .height = 256, .bytes = std::move(expected_pixels)});
});
 
etest::test("invalid signatures are rejected", [] {
auto invalid_signature_bytes = png_bytes;
invalid_signature_bytes[7] = 'b';
auto ss = std::stringstream(std::move(invalid_signature_bytes));
expect_eq(img::Png::from(ss), std::nullopt);
expect_eq(img::Png::from(std::stringstream(std::move(invalid_signature_bytes))), std::nullopt);
});
 
etest::test("it handles truncated files", [] {
auto truncated_bytes = png_bytes.substr(0, 30);
auto ss = std::stringstream(std::move(truncated_bytes));
expect_eq(img::Png::from(ss), std::nullopt);
expect_eq(img::Png::from(std::stringstream(std::move(truncated_bytes))), std::nullopt);
});
 
return etest::run_all_tests();