TestGMPUtils.cpp (2444B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include <string> 8 9 #include "GMPUtils.h" 10 #include "gtest/gtest.h" 11 #include "nsString.h" 12 13 using namespace mozilla; 14 15 void TestSplitAt(const char* aInput, const char* aDelims, 16 size_t aNumExpectedTokens, const char* aExpectedTokens[]) { 17 nsCString input(aInput); 18 nsTArray<nsCString> tokens; 19 SplitAt(aDelims, input, tokens); 20 EXPECT_EQ(tokens.Length(), aNumExpectedTokens) 21 << "Should get expected number of tokens"; 22 for (size_t i = 0; i < tokens.Length(); i++) { 23 EXPECT_TRUE(tokens[i].EqualsASCII(aExpectedTokens[i])) 24 << "Tokenize fail; expected=" << aExpectedTokens[i] 25 << " got=" << tokens[i].BeginReading(); 26 } 27 } 28 29 TEST(GeckoMediaPlugins, TestSplitAt) 30 { 31 { 32 const char* input = "1,2,3,4"; 33 const char* delims = ","; 34 const char* tokens[] = {"1", "2", "3", "4"}; 35 TestSplitAt(input, delims, std::size(tokens), tokens); 36 } 37 38 { 39 const char* input = "a simple, comma, seperated, list"; 40 const char* delims = ","; 41 const char* tokens[] = {"a simple", " comma", " seperated", " list"}; 42 TestSplitAt(input, delims, std::size(tokens), tokens); 43 } 44 45 { 46 const char* input = // Various platform line endings... 47 "line1\r\n" // Windows 48 "line2\r" // Old MacOSX 49 "line3\n" // Unix 50 "line4"; 51 const char* delims = "\r\n"; 52 const char* tokens[] = {"line1", "line2", "line3", "line4"}; 53 TestSplitAt(input, delims, std::size(tokens), tokens); 54 } 55 } 56 57 TEST(GeckoMediaPlugins, ToHexString) 58 { 59 struct Test { 60 nsTArray<uint8_t> bytes; 61 std::string hex; 62 }; 63 64 static const Test tests[] = { 65 {{0x00, 0x00}, "0000"}, 66 {{0xff, 0xff}, "ffff"}, 67 {{0xff, 0x00}, "ff00"}, 68 {{0x00, 0xff}, "00ff"}, 69 {{0xf0, 0x10}, "f010"}, 70 {{0x05, 0x50}, "0550"}, 71 {{0xf}, "0f"}, 72 {{0x10}, "10"}, 73 {{}, ""}, 74 {{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 75 0xcc, 0xdd, 0xee, 0xff}, 76 "00112233445566778899aabbccddeeff"}, 77 }; 78 79 for (const Test& test : tests) { 80 EXPECT_STREQ(test.hex.c_str(), ToHexString(test.bytes).get()); 81 } 82 }