Bug 1753166 [wpt PR 32658] - Return a result from the worker in MediaStreamTrack...
[gecko.git] / xpcom / tests / TestStreamUtils.cpp
blob1a24656617d4d6ef598fa8a415f8ee9be6392a33
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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/. */
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include "nsStreamUtils.h"
10 #include "nsString.h"
11 #include "nsCOMPtr.h"
13 //----
15 static bool test_consume_stream() {
16 const char kData[] =
17 "Get your facts first, and then you can distort them as much as you "
18 "please.";
20 nsCOMPtr<nsIInputStream> input;
21 nsCOMPtr<nsIOutputStream> output;
22 NS_NewPipe(getter_AddRefs(input), getter_AddRefs(output), 10, UINT32_MAX);
23 if (!input || !output) return false;
25 uint32_t n = 0;
26 output->Write(kData, sizeof(kData) - 1, &n);
27 if (n != (sizeof(kData) - 1)) return false;
28 output = nullptr; // close output
30 nsCString buf;
31 if (NS_FAILED(NS_ConsumeStream(input, UINT32_MAX, buf))) return false;
33 if (!buf.Equals(kData)) return false;
35 return true;
38 //----
40 typedef bool (*TestFunc)();
41 #define DECL_TEST(name) \
42 { #name, name }
44 static const struct Test {
45 const char* name;
46 TestFunc func;
47 } tests[] = {DECL_TEST(test_consume_stream), {nullptr, nullptr}};
49 int main(int argc, char** argv) {
50 int count = 1;
51 if (argc > 1) count = atoi(argv[1]);
53 if (NS_FAILED(NS_InitXPCOM(nullptr, nullptr, nullptr))) return -1;
55 while (count--) {
56 for (const Test* t = tests; t->name != nullptr; ++t) {
57 printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE");
61 NS_ShutdownXPCOM(nullptr);
62 return 0;