#include #include #include static BOOL TestStream_Verify(wStream* s, int mincap, int len, int pos) { if (Stream_Buffer(s) == NULL) { printf("stream buffer is null\n"); return FALSE; } if (Stream_Pointer(s) == NULL) { printf("stream pointer is null\n"); return FALSE; } if (Stream_Pointer(s) < Stream_Buffer(s)) { printf("stream pointer (%p) or buffer (%p) is invalid\n", Stream_Pointer(s), Stream_Buffer(s)); return FALSE; } if (Stream_Capacity(s) < mincap) { printf("stream capacity is %ld but minimum expected value is %d\n", Stream_Capacity(s), mincap); return FALSE; } if (Stream_Length(s) != len) { printf("stream has unexpected length (%ld instead of %d)\n", Stream_Length(s), len); return FALSE; } if (Stream_GetPosition(s) != pos) { printf("stream has unexpected position (%ld instead of %d)\n", Stream_GetPosition(s), pos); return FALSE; } if (Stream_GetPosition(s) > Stream_Length(s)) { printf("stream position (%ld) exceeds length (%ld)\n", Stream_GetPosition(s), Stream_Length(s)); return FALSE; } if (Stream_GetPosition(s) > Stream_Capacity(s)) { printf("stream position (%ld) exceeds capacity (%ld)\n", Stream_GetPosition(s), Stream_Capacity(s)); return FALSE; } if (Stream_Length(s) > Stream_Capacity(s)) { printf("stream length (%ld) exceeds capacity (%ld)\n", Stream_Length(s), Stream_Capacity(s)); return FALSE; } if (Stream_GetRemainingLength(s) != len - pos) { printf("stream remaining length (%ld instead of %d)\n", Stream_GetRemainingLength(s), len - pos); return FALSE; } return TRUE; } static BOOL TestStream_Create(int count, BOOL selfAlloc) { int i, len, cap, pos; wStream *s = NULL; void* buffer = NULL; for (i = 0; i < count; i++) { len = cap = i+1; pos = 0; if (selfAlloc) { if (!(buffer = malloc(cap))) { printf("%s: failed to allocate buffer of size %d\n", __FUNCTION__, cap); goto fail; } } if (!(s = Stream_New(selfAlloc ? buffer : NULL, len))) { printf("%s: Stream_New failed for stream #%d\n", __FUNCTION__, i); goto fail; } if (!TestStream_Verify(s, cap, len, pos)) { goto fail; } for (pos = 0; pos < len; pos++) { Stream_SetPosition(s, pos); Stream_SealLength(s); if (!TestStream_Verify(s, cap, pos, pos)) { goto fail; } } if (selfAlloc) { memset(buffer, i%256, cap); if (memcmp(buffer, Stream_Buffer(s), cap)) { printf("%s: buffer memory corruption\n", __FUNCTION__); goto fail; } } Stream_Free(s, buffer ? FALSE : TRUE); free(buffer); } return TRUE; fail: free(buffer); if (s) { Stream_Free(s, buffer ? FALSE : TRUE); } return FALSE; } static BOOL TestStream_Extent(UINT32 maxSize) { int i; wStream *s = NULL; BOOL result = FALSE; if (!(s = Stream_New(NULL, 1))) { printf("%s: Stream_New failed\n", __FUNCTION__); return FALSE; } for (i = 1; i < maxSize; i++) { if (i % 2) { if (!Stream_EnsureRemainingCapacity(s, i)) goto fail; } else { if (!Stream_EnsureCapacity(s, i)) goto fail; } Stream_SetPosition(s, i); Stream_SealLength(s); if (!TestStream_Verify(s, i, i, i)) { printf("%s: failed to verify stream in iteration %d\n", __FUNCTION__, i); goto fail; } } result = TRUE; fail: if (s) { Stream_Free(s, TRUE); } return result; } #define Stream_Peek_UINT8_BE Stream_Peek_UINT8 #define Stream_Read_UINT8_BE Stream_Read_UINT8 #define Stream_Peek_INT8_BE Stream_Peek_UINT8 #define Stream_Read_INT8_BE Stream_Read_UINT8 #define TestStream_PeekAndRead(_s, _r, _t) do \ { \ _t _a, _b; \ int _i; \ BYTE* _p = Stream_Buffer(_s); \ Stream_SetPosition(_s, 0); \ Stream_Peek_ ## _t(_s, _a); \ Stream_Read_ ## _t(_s, _b); \ if (_a != _b) \ { \ printf("%s: test1 " #_t "_LE failed\n", __FUNCTION__); \ _r = FALSE; \ } \ for (_i=0; _i