Bug 545892 - Always pass WM_NCPAINT events to the default event procedure. r=bent...
[mozilla-central.git] / xpcom / tests / TestPipe.cpp
blob9605ddd747dcd9640aacd37c9e91016a94e972c8
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Jeff Walden <jwalden+code@mit.edu>.
19 * Portions created by the Initial Developer are Copyright (C) 2007
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "TestHarness.h"
40 #include "nsIPipe.h"
41 #include "nsIMemory.h"
43 /** NS_NewPipe2 reimplemented, because it's not exported by XPCOM */
44 nsresult TP_NewPipe2(nsIAsyncInputStream** input,
45 nsIAsyncOutputStream** output,
46 PRBool nonBlockingInput,
47 PRBool nonBlockingOutput,
48 PRUint32 segmentSize,
49 PRUint32 segmentCount,
50 nsIMemory* segmentAlloc)
52 nsCOMPtr<nsIPipe> pipe = do_CreateInstance("@mozilla.org/pipe;1");
53 if (!pipe)
54 return NS_ERROR_OUT_OF_MEMORY;
56 nsresult rv = pipe->Init(nonBlockingInput,
57 nonBlockingOutput,
58 segmentSize,
59 segmentCount,
60 segmentAlloc);
62 if (NS_FAILED(rv))
63 return rv;
65 pipe->GetInputStream(input);
66 pipe->GetOutputStream(output);
67 return NS_OK;
70 /**
71 * Allocator can allocate exactly count * size bytes, stored at mMemory;
72 * immediately after the end of this is a byte-map of 0/1 values indicating
73 * which <size>-byte locations in mMemory are empty and which are filled.
74 * Pretty stupid, but enough to test bug 394692.
76 class BackwardsAllocator : public nsIMemory
78 public:
79 BackwardsAllocator()
80 : mMemory(0),
81 mIndex(0xFFFFFFFF),
82 mCount(0xFFFFFFFF),
83 mSize(0)
84 { }
85 ~BackwardsAllocator()
87 delete [] mMemory;
90 nsresult Init(PRUint32 count, size_t size);
92 NS_DECL_ISUPPORTS
93 NS_DECL_NSIMEMORY
95 private:
96 PRUint32 previous(PRUint32 i)
98 if (i == 0)
99 return mCount - 1;
100 return i - 1;
103 private:
104 PRUint8* mMemory;
105 PRUint32 mIndex;
106 PRUint32 mCount;
107 size_t mSize;
110 NS_IMPL_ISUPPORTS1(BackwardsAllocator, nsIMemory)
112 nsresult BackwardsAllocator::Init(PRUint32 count, size_t size)
114 if (mMemory)
116 fail("allocator already initialized!");
117 return NS_ERROR_ALREADY_INITIALIZED;
120 mMemory = new PRUint8[count * size + count];
121 if (!mMemory)
123 fail("failed to allocate mMemory!");
124 return NS_ERROR_OUT_OF_MEMORY;
126 memset(mMemory, 0, count * size + count);
128 mIndex = 0;
129 mCount = count;
130 mSize = size;
132 return NS_OK;
135 NS_IMETHODIMP_(void*) BackwardsAllocator::Alloc(size_t size)
137 if (size != mSize)
139 NS_ERROR("umm, why would this be reached for this test?");
140 return NULL;
143 PRUint32 index = mIndex;
145 while ((index = previous(index)) != mIndex)
147 if (mMemory[mSize * mCount + index] == 1)
148 continue;
149 mMemory[mSize * mCount + index] = 1;
150 mIndex = index;
151 return &mMemory[mSize * index];
154 NS_ERROR("shouldn't reach here in this test");
155 return NULL;
158 NS_IMETHODIMP_(void*) BackwardsAllocator::Realloc(void* ptr, size_t newSize)
160 NS_ERROR("shouldn't reach here in this test");
161 return NULL;
164 NS_IMETHODIMP_(void) BackwardsAllocator::Free(void* ptr)
166 PRUint8* p = static_cast<PRUint8*>(ptr);
167 if (p)
168 mMemory[mCount * mSize + (p - mMemory) / mSize] = 0;
171 NS_IMETHODIMP BackwardsAllocator::HeapMinimize(PRBool immediate)
173 return NS_OK;
176 NS_IMETHODIMP BackwardsAllocator::IsLowMemory(PRBool* retval)
178 *retval = PR_FALSE;
179 return NS_OK;
183 nsresult TestBackwardsAllocator()
185 const PRUint32 SEGMENT_COUNT = 10;
186 const PRUint32 SEGMENT_SIZE = 10;
188 nsRefPtr<BackwardsAllocator> allocator = new BackwardsAllocator();
189 if (!allocator)
191 fail("Allocation of BackwardsAllocator failed!");
192 return NS_ERROR_OUT_OF_MEMORY;
194 nsresult rv = allocator->Init(SEGMENT_COUNT, SEGMENT_SIZE);
195 if (NS_FAILED(rv))
196 return rv;
198 nsCOMPtr<nsIAsyncInputStream> input;
199 nsCOMPtr<nsIAsyncOutputStream> output;
200 rv = TP_NewPipe2(getter_AddRefs(input),
201 getter_AddRefs(output),
202 PR_FALSE,
203 PR_FALSE,
204 SEGMENT_SIZE, SEGMENT_COUNT, allocator);
205 if (NS_FAILED(rv))
207 fail("TP_NewPipe2 failed: %x", rv);
208 return rv;
211 const PRUint32 BUFFER_LENGTH = 100;
212 const char written[] =
213 "0123456789"
214 "1123456789"
215 "2123456789"
216 "3123456789"
217 "4123456789"
218 "5123456789"
219 "6123456789"
220 "7123456789"
221 "8123456789"
222 "9123456789"; // not just a memset, to ensure the allocator works correctly
223 if (sizeof(written) < BUFFER_LENGTH)
225 fail("test error with string size");
226 return NS_ERROR_FAILURE;
229 PRUint32 writeCount;
230 rv = output->Write(written, BUFFER_LENGTH, &writeCount);
231 if (NS_FAILED(rv) || writeCount != BUFFER_LENGTH)
233 fail("writing %d bytes (wrote %d bytes) to output failed: %x",
234 BUFFER_LENGTH, writeCount, rv);
235 return rv;
238 char read[BUFFER_LENGTH];
239 PRUint32 readCount;
240 rv = input->Read(read, BUFFER_LENGTH, &readCount);
241 if (NS_FAILED(rv) || readCount != BUFFER_LENGTH)
243 fail("reading %d bytes (got %d bytes) from input failed: %x",
244 BUFFER_LENGTH, readCount, rv);
245 return rv;
248 if (0 != memcmp(written, read, BUFFER_LENGTH))
250 fail("didn't read the written data correctly!");
251 return NS_ERROR_FAILURE;
254 passed("TestBackwardsAllocator");
255 return NS_OK;
258 int main(int argc, char** argv)
260 ScopedXPCOM xpcom("nsPipe");
261 if (xpcom.failed())
262 return 1;
264 int rv = 0;
266 if (NS_FAILED(TestBackwardsAllocator()))
267 rv = 1;
269 return rv;