annotate tests/mini/vararg6.d @ 341:1bb99290e03a trunk

[svn r362] Started merging the old 'test' dir as well as the newer 'tangotests' dir into 'tests/mini' and 'tests/minicomplex'.
author lindquist
date Sun, 13 Jul 2008 02:51:19 +0200
parents
children cc40db549aea
rev   line source
lindquist@341 1 // tries to implement a fairly complete variadic print function
lindquist@341 2 module tangotests.vararg3;
lindquist@341 3
lindquist@341 4 extern(C) int printf(char*, ...);
lindquist@341 5
lindquist@341 6 struct User
lindquist@341 7 {
lindquist@341 8 char[] name;
lindquist@341 9 char[] nick;
lindquist@341 10 uint age;
lindquist@341 11
lindquist@341 12 char[] toString()
lindquist@341 13 {
lindquist@341 14 return nick ~ "(" ~ name ~ ")";
lindquist@341 15 }
lindquist@341 16 }
lindquist@341 17
lindquist@341 18 struct Infidel
lindquist@341 19 {
lindquist@341 20 char[] whocares;
lindquist@341 21 }
lindquist@341 22
lindquist@341 23 class Obj
lindquist@341 24 {
lindquist@341 25 private char[] ty;
lindquist@341 26
lindquist@341 27 this(char[] t)
lindquist@341 28 {
lindquist@341 29 ty = t;
lindquist@341 30 }
lindquist@341 31
lindquist@341 32 char[] toString()
lindquist@341 33 {
lindquist@341 34 return "Obj(" ~ ty ~ ")";
lindquist@341 35 }
lindquist@341 36 }
lindquist@341 37
lindquist@341 38 struct TLA
lindquist@341 39 {
lindquist@341 40 char[3] acronym;
lindquist@341 41
lindquist@341 42 char[] toString()
lindquist@341 43 {
lindquist@341 44 return acronym;
lindquist@341 45 }
lindquist@341 46 }
lindquist@341 47
lindquist@341 48 void main()
lindquist@341 49 {
lindquist@341 50 User user = User("Bob Doe", "bd", 47);
lindquist@341 51 char[] str = user.toString();
lindquist@341 52 printf("Direct call:\n%.*s\nBy typeinfo:\n", str.length, str.ptr);
lindquist@341 53 print(user, '\n');
lindquist@341 54
lindquist@341 55 print("Without toString:\n");
lindquist@341 56 Infidel inf = Infidel("not me");
lindquist@341 57 print(inf, '\n');
lindquist@341 58
lindquist@341 59 print("Character arrays:\n");
lindquist@341 60 print("hello world\n");
lindquist@341 61
lindquist@341 62 print("Signed integers:\n");
lindquist@341 63 print(cast(byte)byte.max,' ',cast(short)short.max,' ',cast(int)int.max,' ',cast(long)long.max,'\n');
lindquist@341 64
lindquist@341 65 print("Unsigned integers:\n");
lindquist@341 66 print(cast(ubyte)ubyte.max,' ',cast(ushort)ushort.max,' ',cast(uint)uint.max,' ',cast(ulong)ulong.max,'\n');
lindquist@341 67
lindquist@341 68 print("Floating point:\n");
lindquist@341 69 print(cast(float)1.273f, ' ', cast(double)3.412367, ' ', cast(real)142.96731112, '\n');
lindquist@341 70
lindquist@341 71 print("Arrays:\n");
lindquist@341 72 int[] ia1 = [1,2,3,4,5,6,7,8,9];
lindquist@341 73 print(ia1, '\n');
lindquist@341 74 float[] fa1 = [0.1f, 0.15f, 0.2f, 0.25f, 0.3f];
lindquist@341 75 print(fa1, '\n');
lindquist@341 76
lindquist@341 77 print("Pointers:\n");
lindquist@341 78 print(&user,'\n');
lindquist@341 79 print(&inf,'\n');
lindquist@341 80 print(&ia1,'\n');
lindquist@341 81 print(&fa1,'\n');
lindquist@341 82
lindquist@341 83 print("Static arrays:\n");
lindquist@341 84 int[5] isa1 = [1,2,4,8,16];
lindquist@341 85 print(isa1,'\n');
lindquist@341 86
lindquist@341 87 print("Classes:\n");
lindquist@341 88 Obj o = new Obj("foo");
lindquist@341 89 print(o, '\n');
lindquist@341 90
lindquist@341 91 print("Mixed:\n");
lindquist@341 92 print(123, ' ', 42.536f, " foobar ", ia1, ' ', user, '\n');
lindquist@341 93 print(42, ' ', cast(byte)12, ' ', user, ' ', cast(short)1445, " foo\n");
lindquist@341 94
lindquist@341 95 print("International:\n");
lindquist@341 96 print('æ','ø','å','\n');
lindquist@341 97 print('Æ','Ø','Å','\n');
lindquist@341 98 print("rød grød med fløde\n");
lindquist@341 99 print("Heiße\n");
lindquist@341 100
lindquist@341 101 print("TLAs:\n");
lindquist@341 102 TLA tla1 = TLA("FBI");
lindquist@341 103 TLA tla2 = TLA("CIA");
lindquist@341 104 TLA tla3 = TLA("TLA");
lindquist@341 105 print(tla1);
lindquist@341 106 print(tla2);
lindquist@341 107 print(tla3, '\n');
lindquist@341 108 print(tla1, tla2, tla3, '\n');
lindquist@341 109 print(TLA("FBI"), TLA("CIA"), TLA("TLA"), '\n');
lindquist@341 110
lindquist@341 111 print("Done!\n");
lindquist@341 112 }
lindquist@341 113
lindquist@341 114 private void* get_va_arg(TypeInfo ti, ref void* vp)
lindquist@341 115 {
lindquist@341 116 void* arg = vp;
lindquist@341 117 vp = vp + ( ( ti.tsize + size_t.sizeof - 1 ) & ~( size_t.sizeof - 1 ) );
lindquist@341 118 return arg;
lindquist@341 119 }
lindquist@341 120
lindquist@341 121 void print(TypeInfo ti, void* arg)
lindquist@341 122 {
lindquist@341 123 if (ti == typeid(byte))
lindquist@341 124 printf("%d", *cast(byte*)arg);
lindquist@341 125 else if (ti == typeid(short))
lindquist@341 126 printf("%d", *cast(short*)arg);
lindquist@341 127 else if (ti == typeid(int))
lindquist@341 128 printf("%d", *cast(int*)arg);
lindquist@341 129 else if (ti == typeid(long))
lindquist@341 130 printf("%lld", *cast(long*)arg);
lindquist@341 131
lindquist@341 132 else if (ti == typeid(ubyte))
lindquist@341 133 printf("%u", *cast(ubyte*)arg);
lindquist@341 134 else if (ti == typeid(ushort))
lindquist@341 135 printf("%u", *cast(ushort*)arg);
lindquist@341 136 else if (ti == typeid(uint))
lindquist@341 137 printf("%u", *cast(uint*)arg);
lindquist@341 138 else if (ti == typeid(ulong))
lindquist@341 139 printf("%llu", *cast(ulong*)arg);
lindquist@341 140
lindquist@341 141 else if (ti == typeid(float))
lindquist@341 142 printf("%f", *cast(float*)arg);
lindquist@341 143 else if (ti == typeid(double))
lindquist@341 144 printf("%f", *cast(double*)arg);
lindquist@341 145 else if (ti == typeid(real)) // FIXME: 80bit?
lindquist@341 146 {
lindquist@341 147 version(LLVM_X86_FP80)
lindquist@341 148 printf("%llf", *cast(real*)arg);
lindquist@341 149 else
lindquist@341 150 printf("%f", *cast(real*)arg);
lindquist@341 151 }
lindquist@341 152
lindquist@341 153 else if (ti == typeid(char))
lindquist@341 154 printf("%.*s", 1, arg);
lindquist@341 155 else if (ti == typeid(wchar))
lindquist@341 156 printf("%.*s", 2, arg);
lindquist@341 157 else if (ti == typeid(dchar))
lindquist@341 158 printf("%.*s", 4, arg);
lindquist@341 159
lindquist@341 160 else if (ti == typeid(char[]))
lindquist@341 161 {
lindquist@341 162 char[] str = *cast(char[]*)arg;
lindquist@341 163 printf("%.*s", str.length, str.ptr);
lindquist@341 164 }
lindquist@341 165 else if (ti == typeid(wchar[]))
lindquist@341 166 {
lindquist@341 167 wchar[] str = *cast(wchar[]*)arg;
lindquist@341 168 printf("%.*s", str.length*2, str.ptr);
lindquist@341 169 }
lindquist@341 170 else if (ti == typeid(dchar[]))
lindquist@341 171 {
lindquist@341 172 dchar[] str = *cast(dchar[]*)arg;
lindquist@341 173 printf("%.*s", str.length*4, str.ptr);
lindquist@341 174 }
lindquist@341 175
lindquist@341 176 else if (auto pti = cast(TypeInfo_Pointer)ti)
lindquist@341 177 {
lindquist@341 178 printf("%p", *cast(void**)arg);
lindquist@341 179 }
lindquist@341 180
lindquist@341 181 else if (auto sti = cast(TypeInfo_Struct)ti)
lindquist@341 182 {
lindquist@341 183 if (sti.xtoString !is null)
lindquist@341 184 {
lindquist@341 185 char[] str = sti.xtoString(arg);
lindquist@341 186 printf("%.*s", str.length, str.ptr);
lindquist@341 187 }
lindquist@341 188 else
lindquist@341 189 {
lindquist@341 190 char[] str = sti.toString();
lindquist@341 191 printf("%.*s", str.length, str.ptr);
lindquist@341 192 }
lindquist@341 193 }
lindquist@341 194
lindquist@341 195 else if (auto ati = cast(TypeInfo_Array)ti)
lindquist@341 196 {
lindquist@341 197 auto tnext = ati.next;
lindquist@341 198 size_t len = *cast(size_t*)arg;
lindquist@341 199 void* ptr = *(cast(void**)arg + 1);
lindquist@341 200 printf("[");
lindquist@341 201 for(auto i=0; i<len; ++i)
lindquist@341 202 {
lindquist@341 203 print(tnext, get_va_arg(tnext, ptr));
lindquist@341 204 if (i < len-1)
lindquist@341 205 printf(",");
lindquist@341 206 }
lindquist@341 207 printf("]");
lindquist@341 208 }
lindquist@341 209
lindquist@341 210 else if (auto cti = cast(TypeInfo_Class)ti)
lindquist@341 211 {
lindquist@341 212 auto o = *cast(Object*)arg;
lindquist@341 213 char[] str = o.toString();
lindquist@341 214 printf("%.*s", str.length, str.ptr);
lindquist@341 215 }
lindquist@341 216
lindquist@341 217 // static arrays are converted to dynamic arrays when passed to variadic functions
lindquist@341 218 else if (auto sati = cast(TypeInfo_StaticArray)ti)
lindquist@341 219 {
lindquist@341 220 assert(0, "static arrays not supported");
lindquist@341 221 }
lindquist@341 222
lindquist@341 223 else if (auto aati = cast(TypeInfo_AssociativeArray)ti)
lindquist@341 224 {
lindquist@341 225 assert(0, "associative array not supported");
lindquist@341 226 }
lindquist@341 227
lindquist@341 228 else
lindquist@341 229 {
lindquist@341 230 char[] str = ti.toString();
lindquist@341 231 printf("typeinfo: %.*s\n", str.length, str.ptr);
lindquist@341 232 str = ti.classinfo.name;
lindquist@341 233 printf("typeinfo.classinfo: %.*s\n", str.length, str.ptr);
lindquist@341 234 assert(0, "unsupported type ^");
lindquist@341 235 }
lindquist@341 236 }
lindquist@341 237
lindquist@341 238 void print(...)
lindquist@341 239 {
lindquist@341 240 void* argptr = _argptr;
lindquist@341 241 assert(argptr);
lindquist@341 242
lindquist@341 243 foreach(i,ti; _arguments)
lindquist@341 244 {
lindquist@341 245 void* arg = get_va_arg(ti, argptr);
lindquist@341 246 assert(arg);
lindquist@341 247 print(ti, arg);
lindquist@341 248 }
lindquist@341 249 }