xpcom/tests/TestStreamUtils.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include <stdlib.h>
michael@0 8 #include <stdio.h>
michael@0 9 #include "nsIPipe.h"
michael@0 10 #include "nsStreamUtils.h"
michael@0 11 #include "nsString.h"
michael@0 12 #include "nsCOMPtr.h"
michael@0 13
michael@0 14 //----
michael@0 15
michael@0 16 static bool test_consume_stream() {
michael@0 17 const char kData[] =
michael@0 18 "Get your facts first, and then you can distort them as much as you "
michael@0 19 "please.";
michael@0 20
michael@0 21 nsCOMPtr<nsIInputStream> input;
michael@0 22 nsCOMPtr<nsIOutputStream> output;
michael@0 23 NS_NewPipe(getter_AddRefs(input),
michael@0 24 getter_AddRefs(output),
michael@0 25 10, UINT32_MAX);
michael@0 26 if (!input || !output)
michael@0 27 return false;
michael@0 28
michael@0 29 uint32_t n = 0;
michael@0 30 output->Write(kData, sizeof(kData) - 1, &n);
michael@0 31 if (n != (sizeof(kData) - 1))
michael@0 32 return false;
michael@0 33 output = nullptr; // close output
michael@0 34
michael@0 35 nsCString buf;
michael@0 36 if (NS_FAILED(NS_ConsumeStream(input, UINT32_MAX, buf)))
michael@0 37 return false;
michael@0 38
michael@0 39 if (!buf.Equals(kData))
michael@0 40 return false;
michael@0 41
michael@0 42 return true;
michael@0 43 }
michael@0 44
michael@0 45 //----
michael@0 46
michael@0 47 typedef bool (*TestFunc)();
michael@0 48 #define DECL_TEST(name) { #name, name }
michael@0 49
michael@0 50 static const struct Test {
michael@0 51 const char* name;
michael@0 52 TestFunc func;
michael@0 53 } tests[] = {
michael@0 54 DECL_TEST(test_consume_stream),
michael@0 55 { nullptr, nullptr }
michael@0 56 };
michael@0 57
michael@0 58 int main(int argc, char **argv) {
michael@0 59 int count = 1;
michael@0 60 if (argc > 1)
michael@0 61 count = atoi(argv[1]);
michael@0 62
michael@0 63 if (NS_FAILED(NS_InitXPCOM2(nullptr, nullptr, nullptr)))
michael@0 64 return -1;
michael@0 65
michael@0 66 while (count--) {
michael@0 67 for (const Test* t = tests; t->name != nullptr; ++t) {
michael@0 68 printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE");
michael@0 69 }
michael@0 70 }
michael@0 71
michael@0 72 NS_ShutdownXPCOM(nullptr);
michael@0 73 return 0;
michael@0 74 }

mercurial