00001 /* -*- mode: C++; c-file-style: "stroustrup"; c-basic-offset: 4; indent-tabs-mode: nil; -*- */ 00002 /********************************************************************* 00003 * 00004 * Filename : utils.h (debug) 00005 * C header. 00006 * 00007 * Utility functions for debugging. 00008 * 00009 * This file is a part of the UPPAAL toolkit. 00010 * Copyright (c) 1995 - 2003, Uppsala University and Aalborg University. 00011 * All right reserved. 00012 * 00013 * v 1.3 reviewed. 00014 * $Id: utils.h,v 1.17 2005/05/11 19:08:14 adavid Exp $ 00015 * 00016 **********************************************************************/ 00017 00018 #ifndef INCLUDE_DEBUG_UTILS_H 00019 #define INCLUDE_DEBUG_UTILS_H 00020 00021 #include <stdio.h> 00022 #include "base/inttypes.h" 00023 00024 #ifdef __cplusplus 00025 #include <iostream> 00026 00027 00028 /** Tabulation: print n spaces. 00029 * @param n: size of the tabulation. 00030 * @param out: output stream where to print (cerr, cout) 00031 */ 00032 static inline 00033 std::ostream& debug_cpptab(std::ostream& os, size_t n) 00034 { 00035 while(n) { os << ' '; --n; } 00036 return os; 00037 } 00038 00039 00040 /** Print bitstring, lower bits first. 00041 * @param s: start of the string. 00042 * @param n: size in int of the string to 00043 * @param out: output stream where to print (cerr,cout) 00044 * print. int[n] will be printed as bits. 00045 */ 00046 std::ostream& debug_cppPrintBitstring(std::ostream& out, const uint32_t *s, size_t n); 00047 00048 00049 /** Print a matrix of bit. 00050 * @param out: output stream. 00051 * @param s,dim: bit matrix dimxdim. 00052 * @pre s is a uint32_t[bits2intsize(dim*dim)] 00053 */ 00054 std::ostream& debug_cppPrintBitMatrix(std::ostream& out, const uint32_t *s, cindex_t dim); 00055 00056 00057 /** Print diff between bitstrings, lower bits first. 00058 * @param s1,s2: start of the strings. 00059 * @param n: size in int of the string to 00060 * @param out: output stream where to print (cerr,cout) 00061 * print. int[n] will be printed as bits. 00062 */ 00063 std::ostream& debug_cppPrintDiffBitstrings(std::ostream& out, 00064 const uint32_t *s1, const uint32_t *s2, size_t n); 00065 00066 00067 /** Print bits, lower bits first. 00068 * @param i: the int to print. 00069 * @param out: output stream where to print (cerr,cout) 00070 */ 00071 std::ostream& debug_cppPrintBits(std::ostream& out, uint32_t i); 00072 00073 00074 /** Print diff bits of i with j, lower bits first. 00075 * @param i,j: the ints to print. 00076 * @param out: output stream where to print (cerr,cout) 00077 */ 00078 std::ostream& debug_cppPrintDiffBits(std::ostream& out, uint32_t i, uint32_t j); 00079 00080 00081 /** Print a vector of ints. 00082 * @param data: the vector of ints. 00083 * @param size: size of the vector. 00084 * @param out: where to print. 00085 * @pre data is a int32_t[size] 00086 */ 00087 std::ostream& debug_cppPrintVector(std::ostream& out, const int32_t *data, size_t size); 00088 00089 00090 /** Print a vector of ints. 00091 * @param data: the vector of ints. 00092 * @param size: size of the vector. 00093 * @param out: where to print. 00094 * @pre data is a int32_t[size] 00095 */ 00096 std::ostream& debug_cppPrintRealVector(std::ostream& out, const double *data, size_t size); 00097 00098 00099 /** Print 2 vectors and highlight 00100 * the difference between them. 00101 * @param vec1,vec2: the vectors to print. 00102 * @param size: size of the vectors. 00103 * @param out: where t print. 00104 * @pre vec1 and vec2 are int32_t[size] 00105 */ 00106 std::ostream& debug_cppPrintDiffVectors(std::ostream& out, 00107 const int32_t *vec1, 00108 const int32_t *vec2, size_t size); 00109 00110 00111 /** Print memory quantity in human 00112 * readable format with B,MB,GB units. 00113 * @param mem: memory to print 00114 * @param out: where to print. 00115 */ 00116 std::ostream& debug_cppPrintMemory(std::ostream& out, size_t mem); 00117 00118 00119 /** Print the set of active "things" according 00120 * to a bit vector that marks which ones are active. 00121 * @param out: where to print. 00122 * @param bits: bit array. 00123 * @param intSize: size in ints of the array. 00124 */ 00125 std::ostream& debug_cppPrintActiveSet(std::ostream& out, const uint32_t *bits, size_t intSize); 00126 00127 extern "C" { 00128 00129 #endif /* __cplusplus */ 00130 00131 00132 /** Randomize memory. 00133 * Write random numbers (rand()) in memory. 00134 * NOTE: one should call seed(something) to initialize 00135 * the random generator beforehand. 00136 * @param data: where to write. 00137 * @param intSize: size to write in int. Will 00138 * write int[intSize]. 00139 * @post ((int*)data)[intSize] is randomized. 00140 */ 00141 void debug_randomize(void *data, size_t intSize); 00142 00143 00144 /** Tabulation: print n spaces. 00145 * @param n: size of the tabulation. 00146 * @param out: output stream where to print (stderr,stdout) 00147 */ 00148 static inline 00149 void debug_tab(FILE* out, size_t n) 00150 { 00151 while(n) { fputc(' ', out); --n; } 00152 } 00153 00154 00155 /** Print bitstring, lower bits first. 00156 * @param s: start of the string. 00157 * @param n: size in int of the string to 00158 * @param out: output stream where to print (stderr,stdout) 00159 * print. int[n] will be printed as bits. 00160 */ 00161 void debug_printBitstring(FILE* out, const uint32_t *s, size_t n); 00162 00163 00164 /** Print a bit matrix. 00165 * @param s,dim: bit matrix dimxdim. 00166 * @param out: output stream. 00167 * @pre s is a uint32_t[bits2intsize(dim*dim)] 00168 */ 00169 void debug_printBitMatrix(FILE* out, const uint32_t *s, cindex_t dim); 00170 00171 00172 /** Print diff between bitstrings, lower bits first. 00173 * @param s1,s2: start of the strings. 00174 * @param n: size in int of the string to 00175 * @param out: output stream where to print (stderr,stdout) 00176 * print. int[n] will be printed as bits. 00177 */ 00178 void debug_printDiffBitstrings(FILE* out, 00179 const uint32_t *s1, const uint32_t *s2, size_t n); 00180 00181 00182 /** Print bits, lower bits first. 00183 * @param i: the int to print. 00184 * @param out: output stream where to print (stderr,stdout) 00185 */ 00186 void debug_printBits(FILE *out, uint32_t i); 00187 00188 00189 /** Print diff bits of i with j, lower bits first. 00190 * @param i,j: the ints to print. 00191 * @param out: output stream where to print (stderr,stdout) 00192 */ 00193 void debug_printDiffBits(FILE *out, uint32_t i, uint32_t j); 00194 00195 00196 /** Print a vector of ints. 00197 * @param data: the vector of ints. 00198 * @param size: size of the vector. 00199 * @param out: where to print. 00200 * @pre data is a int32_t[size] 00201 */ 00202 void debug_printVector(FILE *out, const int32_t *data, size_t size); 00203 00204 00205 /** Print a vector of ints. 00206 * @param data: the vector of ints. 00207 * @param size: size of the vector. 00208 * @param out: where to print. 00209 * @pre data is a int32_t[size] 00210 */ 00211 void debug_printRealVector(FILE *out, const double *data, size_t size); 00212 00213 00214 /** Print 2 vectors and highlight 00215 * the difference between them. 00216 * @param vec1,vec2: the vectors to print. 00217 * @param size: size of the vectors. 00218 * @param out: where t print. 00219 * @pre vec1 and vec2 are int32_t[size] 00220 */ 00221 void debug_printDiffVectors(FILE *out, 00222 const int32_t *vec1, 00223 const int32_t *vec2, size_t size); 00224 00225 /** Print the set of active "things" according 00226 * to a bit vector that marks which ones are active. 00227 * @param out: where to print. 00228 * @param bits: bit array. 00229 * @param intSize: size in ints of the array. 00230 */ 00231 void debug_printActiveSet(FILE *out, const uint32_t *bits, size_t intSize); 00232 00233 /** Return sub-string of filename. 00234 * No allocation, it is just a pointer offset. 00235 * @param filename: filename to truncate. 00236 * @param test: test directory name (typically "tests/") 00237 * NOTE: the test is done as a strncmp(xx, test, strlen(test)) 00238 * so if '/' is ommitted, then we test for the beginning of 00239 * the name only. 00240 * @pre filename != NULL && test != NULL 00241 * @return pointer in filename to truncate from 00242 * absolute path to module path. 00243 * e.g.: 00244 * "somewhere/hop/foo.c" -> "hop/foo.c" 00245 * "somewhere/tests/foo.c" -> "somewhere/tests/foo.c" 00246 * "long/path/here/foo.cpp" -> "here/foo.cpp" 00247 */ 00248 const char* debug_shortName(const char *filename, 00249 const char *test); 00250 00251 static inline 00252 const char* debug_shortSource(const char *filename) 00253 { 00254 return debug_shortName(filename, "tests/"); 00255 } 00256 00257 00258 /** Generate random bits. To the diffence of 00259 * randomize, this function allows specifying 00260 * the number of bits we want to be set. 00261 * @param bits: bit string to write. 00262 * @param bitSize: size in int of the bit string. 00263 * @param nbits: nb of bits to set. 00264 * @param bit1: to force the 1st bit to be set, 00265 * if == 0, does not force anything, if == 1 00266 * force 1st bit to 1. 00267 * @pre: 00268 * - nbits <= 32*bitSize otherwise it is not 00269 * possible to meet the specification 00270 * - bits is a uint32_t[bitSize] 00271 */ 00272 void debug_generateBits(uint32_t *bits, size_t bitSize, 00273 size_t nbits, BOOL bit1); 00274 00275 00276 00277 /** Fix generated bits (typically by debug_generateBits) 00278 * so that the index of the highest bit < bitStringSize 00279 * @param bits,bitSize: bitstring of size bitSize (ints) 00280 * @param bitStringSize: size in bits of the bitstring 00281 * @pre bitStringSize <= number of bits to keep == 1, otherwise 00282 * bits will be lost. 00283 */ 00284 void debug_fixGeneratedBits(uint32_t *bits, size_t bitSize, size_t bitStringSize); 00285 00286 00287 /** Test if a the unpacking of a bit table 00288 * gives the given index table as a result. 00289 * @param table: index redirection table 00290 * @param nbSet: number of used indices 00291 * (check that = number of bits in 'bits') 00292 * @param bits: bit array 00293 * @param n: size in int of the bit array 00294 * @return true if table matches bits. 00295 * @pre 00296 * - table is large enough: at least 00297 * uint32_t[number of bits read == n*32] 00298 * - bits is at least a uint32_t[n] 00299 */ 00300 BOOL debug_bits2indexTableOK(const uint32_t *bits, size_t n, 00301 const uint32_t *table, size_t nbSet); 00302 00303 00304 /** Print memory quantity in human 00305 * readable format with B,MB,GB units. 00306 * @param mem: memory to print 00307 * @param out: where to print. 00308 */ 00309 void debug_printMemory(FILE *out, size_t mem); 00310 00311 00312 /** Print a spinning bar on a given output. 00313 * Successive calls will make the bar spin. 00314 * @param out: stream for output. 00315 */ 00316 void debug_spin(FILE *out); 00317 00318 00319 #ifdef __cplusplus 00320 } 00321 #endif 00322 00323 #endif /* INCLUDE_DEBUG_UTILS_H */