/system/mp/mptag_test.cpp

http://github.com/hpcc-systems/HPCC-Platform · C++ · 139 lines · 101 code · 14 blank · 24 comment · 11 complexity · 69f913a57ce7ed9f94032a1b18943130 MD5 · raw file

  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "mptag.hpp"
  14. #include "mptag.ipp"
  15. //#include "mptag.cpp"
  16. #include <stdio.h>
  17. #include <jexcept.hpp>
  18. /******************************
  19. ** TEST CASES
  20. ******************************/
  21. // 1. populate to and beyond maximum limit, free everything,
  22. void test1()
  23. {
  24. int i; const size = 1000; //TAG_DYNAMIC_MAX - TAG_DYNAMIC
  25. mptag_t a[size];
  26. for( i=0;i<(size);i++) {
  27. a[i]=allocMPtag("hello",123);
  28. printf("alloc: %d \n" ,a[i]);
  29. }
  30. printf("alloc: %d \n" , allocMPtag("hello",123));
  31. for( i=0;i<size;i++) { //free marked 10
  32. printf("free: %d \n",a[i]);
  33. freeMPtag(a[i]);
  34. }
  35. }
  36. // 2. populate table, remove a few randomly, populate some more
  37. void test2()
  38. {
  39. mptag_t a[10]; int i; //IMessageTraceFormatter f;
  40. printf("**********************ENTER 100 \n");
  41. for( i=1;i<101;i++) { //enter 100
  42. printf("alloc: %d \n" ,allocMPtag("hello",123));
  43. }
  44. printf("**********************BELOW MARKED FOR REMOVAL \n");
  45. for( i=0;i<10;i++) { //enter another 10 (marked for removal)
  46. printf("alloc: %d \n" ,a[i] = allocMPtag("hello",123));
  47. }
  48. printf("**********************ENTER ANOTHER 100 \n");
  49. for( i=1;i<101;i++) { //enter 100
  50. printf("alloc: %d \n" ,allocMPtag("hello",123));
  51. }
  52. printf("**********************RETRIVE FORMATTER, CALL FORMAT ON MARKED 10\n");
  53. MemoryBuffer* m = new MemoryBuffer(); m->append("sdasda"); StringBuffer* s = new StringBuffer("random string");
  54. for( i=0;i<10;i++) { //free marked 10
  55. CMessageTraceFormatterDefault* f = (CMessageTraceFormatterDefault*) queryFormatter(a[i]);
  56. printf("format: %s \n",f->format((mptag_t)999, *m, *s));
  57. }
  58. printf("**********************FREEING MARKED FOR REMOVAL \n");
  59. for( i=0;i<10;i++) { //free marked 10
  60. printf("free: %d \n",a[i]);
  61. freeMPtag(a[i]);
  62. }
  63. printf("**********************ENTER 100 \n");
  64. for( i=1;i<101;i++) { //enter 100
  65. printf("alloc: %d \n" ,allocMPtag("hello",123));
  66. }
  67. printf("**********************ENDGAME \n");
  68. a[0] = allocMPtag("hello",123); //alloc,free,alloc
  69. printf("alloc: %d \n" , a[0]);
  70. freeMPtag(a[0]);
  71. printf("free: %d \n" , a[0]);
  72. a[0] = allocMPtag("hello",123);
  73. printf("alloc: %d \n" , a[0]);
  74. delete m; delete s;
  75. }
  76. // 3. create formatter and pass in; create new formatter and associate
  77. void test3(){
  78. IMessageTraceFormatter* f = new CMessageTraceFormatterDefault("tracename ",101);
  79. int i; const size = 10; //size must be even
  80. mptag_t a[size];
  81. for( i=0;i<(size);i++) {
  82. a[i]=allocMPtag(*f);
  83. printf("allocF: %d \n" ,a[i]);
  84. }
  85. f->Release();
  86. IMessageTraceFormatter* f2 = new CMessageTraceFormatterDefault("tracename ",101);
  87. for( i=0;i<((size/2)-1);i++) {
  88. associateMPtag(a[i],*f2);
  89. printf("a$$ocF: %d \n" ,a[i]);
  90. }
  91. f2->Release();
  92. for( i=((size/2));i<size;i++) { //free marked 10
  93. printf("freeF: %d \n",a[i]);
  94. freeMPtag(a[i]);
  95. }
  96. }
  97. // 4. free non-existant tag, get fromatter table for non-existant tag
  98. void test4()
  99. { StringBuffer msg("ERROR: "); StringBuffer msg2("ERROR: ");
  100. printf("***Freeing Tag that does not exist****\n");
  101. try{
  102. freeMPtag((mptag_t)999);
  103. }catch(IException *e){printf("%s\n", e->errorMessage(msg).str());e->Release();}
  104. try{
  105. printf("***Getting Formater for Tag that does not exist***\n");
  106. queryFormatter((mptag_t)999);
  107. }catch(IException *e){printf("%s\n", e->errorMessage(msg2).str());e->Release();}
  108. }
  109. int main(int argc, char* argv[])
  110. {
  111. printf("\n\n");
  112. test1();
  113. printf("\n\n");
  114. test2();
  115. printf("\n\n");
  116. test3();
  117. printf("\n\n");
  118. test4();
  119. releaseAtoms();
  120. return 1;
  121. }