erlang各种有用的函数包括一些有用nif封装,还有一些性能测试case。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 line
1.8 KiB

5 年之前
  1. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  2. // use this file except in compliance with the License. You may obtain a copy of
  3. // the License at
  4. //
  5. // http://www.apache.org/licenses/LICENSE-2.0
  6. //
  7. // Unless required by applicable law or agreed to in writing, software
  8. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. // License for the specific language governing permissions and limitations under
  11. // the License.
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <assert.h>
  15. #include "hqueue.h"
  16. // Simple test script to stress the public HQueue API.
  17. // Primary use case is for running this under Valgrind.
  18. int main(void)
  19. {
  20. int str_len = 100;
  21. int iterations = 1000;
  22. uint32_t max_elems = 1024;
  23. uint32_t heap_size = 64;
  24. hqueue_t* hq = hqueue_new(max_elems, heap_size);
  25. double priority;
  26. double priority_res;
  27. char* val;
  28. char* val_res;
  29. int i;
  30. assert(max_elems == hqueue_max_elems(hq));
  31. assert(heap_size == hqueue_heap_size(hq));
  32. for(i = 0; i < iterations; i++) {
  33. priority = 1234.4321 * i;
  34. val = (char*) malloc(str_len + 1);
  35. if(val == NULL) {
  36. return 1;
  37. }
  38. assert(hqueue_size(hq) == i);
  39. if(snprintf(val, str_len + 1, "Fun string #%d\n", i)) {
  40. if(!hqueue_insert(hq, priority, val)) {
  41. return 1;
  42. }
  43. } else {
  44. return 1;
  45. }
  46. }
  47. hqueue_scale_by(hq, 3.7);
  48. // Added 1000 elements, so heap size should have expanded to 1024
  49. assert(max_elems == hqueue_max_elems(hq));
  50. assert(max_elems == hqueue_heap_size(hq));
  51. if(!hqueue_extract_max(hq, &priority_res, (void**) &val_res)) {
  52. return 1;
  53. }
  54. free(val_res);
  55. hqueue_free2(hq, free);
  56. return 0;
  57. }