From a3a6cb4e36fac50fd21f831092a4b93ade24f713 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Mangano-Tarumi?= Date: Mon, 5 Nov 2018 18:41:14 -0500 Subject: [PATCH] t: check parse_tags on a simple sample --- src/opus.cc | 2 +- src/opustags.h | 2 +- t/unit.cc | 51 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/opus.cc b/src/opus.cc index a584a11..971ab61 100644 --- a/src/opus.cc +++ b/src/opus.cc @@ -34,7 +34,7 @@ #define le32toh(x) OSSwapLittleToHostInt32(x) #endif -int ot::parse_tags(char *data, long len, opus_tags *tags) +int ot::parse_tags(const char *data, long len, opus_tags *tags) { long pos; if (len < 8+4+4) diff --git a/src/opustags.h b/src/opustags.h index a8549c6..0b3a752 100644 --- a/src/opustags.h +++ b/src/opustags.h @@ -40,7 +40,7 @@ struct opus_tags { const char **comment; }; -int parse_tags(char *data, long len, opus_tags *tags); +int parse_tags(const char *data, long len, opus_tags *tags); int render_tags(opus_tags *tags, ogg_packet *op); void delete_tags(opus_tags *tags, const char *field); int add_tags(opus_tags *tags, const char **tags_to_add, uint32_t count); diff --git a/t/unit.cc b/t/unit.cc index 216b360..f887efb 100644 --- a/t/unit.cc +++ b/t/unit.cc @@ -8,10 +8,59 @@ #include +#include +#include #include +class failure : public std::runtime_error { +public: + failure(const char *message) : std::runtime_error(message) {} +}; + +template +static void run(F test, const char *name) +{ + bool result = false; + try { + result = test(); + } catch (failure& e) { + std::cout << "# " << e.what() << "\n"; + } + std::cout << (result ? "ok" : "not ok") << " - " << name << "\n"; +} + +static const char standard_OpusTags[] = + "OpusTags" + "\x14\x00\x00\x00" "opustags test packet" + "\x02\x00\x00\x00" + "\x09\x00\x00\x00" "TITLE=Foo" + "\x0a\x00\x00\x00" "ARTIST=Bar"; + +static bool parse_standard() +{ + ot::opus_tags tags; + int rc = ot::parse_tags(standard_OpusTags, sizeof(standard_OpusTags) - 1, &tags); + if (rc != 0) + throw failure("ot::parse_tags did not return 0"); + if (tags.vendor_length != 20) + throw failure("the vendor string length is invalid"); + if (memcmp(tags.vendor_string, "opustags test packet", 20) != 0) + throw failure("the vendor string is invalid"); + if (tags.count != 2) + throw failure("bad number of comments"); + if (tags.lengths[0] != 9 || tags.lengths[1] != 10) + throw failure("bad comment lengths"); + if (memcmp(tags.comment[0], "TITLE=Foo", tags.lengths[0]) != 0) + throw failure("bad title comment"); + if (memcmp(tags.comment[1], "ARTIST=Bar", tags.lengths[1]) != 0) + throw failure("bad artist comment"); + ot::free_tags(&tags); + return true; +} + int main() { - std::cout << "1..1\nok - trivial\n"; + std::cout << "1..1\n"; + run(parse_standard, "parse a standard OpusTags packet"); return 0; }