|
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/. */ |
|
6 |
|
7 #include <stdlib.h> |
|
8 #include <stdio.h> |
|
9 #include "nsIPipe.h" |
|
10 #include "nsStreamUtils.h" |
|
11 #include "nsString.h" |
|
12 #include "nsCOMPtr.h" |
|
13 |
|
14 //---- |
|
15 |
|
16 static bool test_consume_stream() { |
|
17 const char kData[] = |
|
18 "Get your facts first, and then you can distort them as much as you " |
|
19 "please."; |
|
20 |
|
21 nsCOMPtr<nsIInputStream> input; |
|
22 nsCOMPtr<nsIOutputStream> output; |
|
23 NS_NewPipe(getter_AddRefs(input), |
|
24 getter_AddRefs(output), |
|
25 10, UINT32_MAX); |
|
26 if (!input || !output) |
|
27 return false; |
|
28 |
|
29 uint32_t n = 0; |
|
30 output->Write(kData, sizeof(kData) - 1, &n); |
|
31 if (n != (sizeof(kData) - 1)) |
|
32 return false; |
|
33 output = nullptr; // close output |
|
34 |
|
35 nsCString buf; |
|
36 if (NS_FAILED(NS_ConsumeStream(input, UINT32_MAX, buf))) |
|
37 return false; |
|
38 |
|
39 if (!buf.Equals(kData)) |
|
40 return false; |
|
41 |
|
42 return true; |
|
43 } |
|
44 |
|
45 //---- |
|
46 |
|
47 typedef bool (*TestFunc)(); |
|
48 #define DECL_TEST(name) { #name, name } |
|
49 |
|
50 static const struct Test { |
|
51 const char* name; |
|
52 TestFunc func; |
|
53 } tests[] = { |
|
54 DECL_TEST(test_consume_stream), |
|
55 { nullptr, nullptr } |
|
56 }; |
|
57 |
|
58 int main(int argc, char **argv) { |
|
59 int count = 1; |
|
60 if (argc > 1) |
|
61 count = atoi(argv[1]); |
|
62 |
|
63 if (NS_FAILED(NS_InitXPCOM2(nullptr, nullptr, nullptr))) |
|
64 return -1; |
|
65 |
|
66 while (count--) { |
|
67 for (const Test* t = tests; t->name != nullptr; ++t) { |
|
68 printf("%25s : %s\n", t->name, t->func() ? "SUCCESS" : "FAILURE"); |
|
69 } |
|
70 } |
|
71 |
|
72 NS_ShutdownXPCOM(nullptr); |
|
73 return 0; |
|
74 } |