EditResStream.c revision 9dedec0c
1/* 2 * Copyright (c) 2022, Oracle and/or its affiliates. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21 * DEALINGS IN THE SOFTWARE. 22 */ 23 24/* Test code for ProtocolStream Get/Put functions in src/EditResCom.c */ 25#include <X11/Xmu/EditresP.h> 26#include <assert.h> 27 28static const char *test_string = "\tIt was a dark and stormy night...\n"; 29 30#define FillBuffer(d, v) memset(d, v, sizeof(d)) 31 32int main(void) 33{ 34 ProtocolStream ps = { 0, 0, NULL, NULL, NULL }; 35 unsigned char c; 36 unsigned short s; 37 unsigned long l; 38 Bool res; 39 char *str; 40 unsigned long ids[] = { 1, 10, 0xbabe, 0xbabeface, 0xffffffff }; 41 WidgetInfo i = { 42 .num_widgets = sizeof(ids) / sizeof(ids[0]), 43 .ids = ids, 44 .real_widget = 0 45 }; 46 WidgetInfo out = { 0, NULL, 0 }; 47 48 _XEditResResetStream(&ps); 49 50 _XEditResPut8(&ps, 8); 51 _XEditResPut16(&ps, 16); 52 _XEditResPut16(&ps, 0xface); 53 _XEditResPut32(&ps, 32); 54 _XEditResPut32(&ps, 0xbabeface); 55 _XEditResPutString8(&ps, test_string); 56 _XEditResPutWidgetInfo(&ps, &i); 57 58 /* current is a pointer to the next byte to read from or write to, 59 so we need to reset it to the beginning to read the data we wrote */ 60 ps.current = ps.top; 61 62 res = _XEditResGet8(&ps, &c); 63 assert(res == True); 64 assert(c == 8); 65 66 res = _XEditResGet16(&ps, &s); 67 assert(res == True); 68 assert(s == 16); 69 70 res = _XEditResGet16(&ps, &s); 71 assert(res == True); 72 assert(s == 0xface); 73 74 /* set the full value so we can make sure that in 64-bit mode we 75 write to the full long value, not just 32-bits of it. */ 76 memset(&l, 0x0f, sizeof(l)); 77 res = _XEditResGet32(&ps, &l); 78 assert(res == True); 79 assert(l == 32); 80 81 memset(&l, 0x0f, sizeof(l)); 82 res = _XEditResGet32(&ps, &l); 83 assert(res == True); 84 assert(l == 0xbabeface); 85 86 res = _XEditResGetString8(&ps, &str); 87 assert(res == True); 88 assert(strcmp(str, test_string) == 0); 89 XtFree(str); 90 str = NULL; 91 92 res = _XEditResGetWidgetInfo(&ps, &out); 93 assert(res == True); 94 assert(memcmp(ids, out.ids, sizeof(ids)) == 0); 95 XtFree((char *) out.ids); 96 out.ids = NULL; 97 98 return 0; 99} 100