xpcom/tests/TestStreamUtils.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/tests/TestStreamUtils.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,74 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include <stdlib.h>
    1.11 +#include <stdio.h>
    1.12 +#include "nsIPipe.h"
    1.13 +#include "nsStreamUtils.h"
    1.14 +#include "nsString.h"
    1.15 +#include "nsCOMPtr.h"
    1.16 +
    1.17 +//----
    1.18 +
    1.19 +static bool test_consume_stream() {
    1.20 +  const char kData[] =
    1.21 +      "Get your facts first, and then you can distort them as much as you "
    1.22 +      "please.";
    1.23 +
    1.24 +  nsCOMPtr<nsIInputStream> input;
    1.25 +  nsCOMPtr<nsIOutputStream> output;
    1.26 +  NS_NewPipe(getter_AddRefs(input),
    1.27 +             getter_AddRefs(output),
    1.28 +             10, UINT32_MAX);
    1.29 +  if (!input || !output)
    1.30 +    return false;
    1.31 +
    1.32 +  uint32_t n = 0;
    1.33 +  output->Write(kData, sizeof(kData) - 1, &n);
    1.34 +  if (n != (sizeof(kData) - 1))
    1.35 +    return false;
    1.36 +  output = nullptr;  // close output
    1.37 +
    1.38 +  nsCString buf;
    1.39 +  if (NS_FAILED(NS_ConsumeStream(input, UINT32_MAX, buf)))
    1.40 +    return false;
    1.41 +
    1.42 +  if (!buf.Equals(kData))
    1.43 +    return false;
    1.44 +
    1.45 +  return true; 
    1.46 +}
    1.47 +
    1.48 +//----
    1.49 +
    1.50 +typedef bool (*TestFunc)();
    1.51 +#define DECL_TEST(name) { #name, name }
    1.52 +
    1.53 +static const struct Test {
    1.54 +  const char* name;
    1.55 +  TestFunc    func;
    1.56 +} tests[] = {
    1.57 +  DECL_TEST(test_consume_stream),
    1.58 +  { nullptr, nullptr }
    1.59 +};
    1.60 +
    1.61 +int main(int argc, char **argv) {
    1.62 +  int count = 1;
    1.63 +  if (argc > 1)
    1.64 +    count = atoi(argv[1]);
    1.65 +
    1.66 +  if (NS_FAILED(NS_InitXPCOM2(nullptr, nullptr, nullptr)))
    1.67 +    return -1;
    1.68 +
    1.69 +  while (count--) {
    1.70 +    for (const Test* t = tests; t->name != nullptr; ++t) {
    1.71 +      printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE");
    1.72 +    }
    1.73 +  }
    1.74 +  
    1.75 +  NS_ShutdownXPCOM(nullptr);
    1.76 +  return 0;
    1.77 +}

mercurial