Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /* test/basictests.c
  2.  *
  3.  * Basic tests for the test suite for libwapcaplet
  4.  *
  5.  * Copyright 2009 The NetSurf Browser Project
  6.  *                Daniel Silverstone <dsilvers@netsurf-browser.org>
  7.  */
  8.  
  9. #include <check.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. #include "tests.h"
  14.  
  15. #ifndef UNUSED
  16. #define UNUSED(x) (void)(x)
  17. #endif
  18.  
  19. #ifndef NDEBUG
  20. /* All the basic assert() tests */
  21. START_TEST (test_lwc_intern_string_aborts1)
  22. {
  23.         lwc_intern_string(NULL, 0, NULL);
  24. }
  25. END_TEST
  26.  
  27. START_TEST (test_lwc_intern_string_aborts2)
  28. {
  29.         lwc_intern_string("A", 1, NULL);
  30. }
  31. END_TEST
  32.  
  33. START_TEST (test_lwc_intern_substring_aborts1)
  34. {
  35.         lwc_intern_substring(NULL, 0, 0, NULL);
  36. }
  37. END_TEST
  38.  
  39. START_TEST (test_lwc_intern_substring_aborts2)
  40. {
  41.         lwc_string *str;
  42.         fail_unless(lwc_intern_string("Jam", 3, &str) == lwc_error_ok,
  43.                     "unable to intern 'Jam'");
  44.        
  45.         lwc_intern_substring(str, 88, 77, NULL);
  46. }
  47. END_TEST
  48.  
  49. START_TEST (test_lwc_string_ref_aborts)
  50. {
  51.         lwc_string_ref(NULL);
  52. }
  53. END_TEST
  54.  
  55. START_TEST (test_lwc_string_unref_aborts)
  56. {
  57.         lwc_string_unref(NULL);
  58. }
  59. END_TEST
  60.  
  61. START_TEST (test_lwc_string_data_aborts)
  62. {
  63.         lwc_string_data(NULL);
  64. }
  65. END_TEST
  66.  
  67. START_TEST (test_lwc_string_length_aborts)
  68. {
  69.         lwc_string_length(NULL);
  70. }
  71. END_TEST
  72.  
  73. START_TEST (test_lwc_string_hash_value_aborts)
  74. {
  75.         lwc_string_hash_value(NULL);
  76. }
  77. END_TEST
  78.  
  79. #endif
  80.  
  81. /**** The next set of tests need a fixture set ****/
  82.  
  83. static void
  84. with_simple_context_setup(void)
  85. {
  86.         /* Nothing to set up */
  87. }
  88.  
  89. static void
  90. with_simple_context_teardown(void)
  91. {
  92.         /* Nothing to do to tear down */
  93. }
  94.  
  95. START_TEST (test_lwc_intern_string_ok)
  96. {
  97.         lwc_string *str = NULL;
  98.         fail_unless(lwc_intern_string("A", 1, &str) == lwc_error_ok,
  99.                     "Unable to intern a simple string");
  100.         fail_unless(str != NULL,
  101.                     "Returned OK but str was still NULL");
  102. }
  103. END_TEST
  104.  
  105. START_TEST (test_lwc_intern_string_twice_ok)
  106. {
  107.         lwc_string *str1 = NULL, *str2 = NULL;
  108.         fail_unless(lwc_intern_string("A", 1, &str1) == lwc_error_ok,
  109.                     "Unable to intern a simple string");
  110.         fail_unless(str1 != NULL,
  111.                     "Returned OK but str was still NULL");
  112.         fail_unless(lwc_intern_string("B", 1, &str2) == lwc_error_ok,
  113.                     "Unable to intern a simple string");
  114.         fail_unless(str2 != NULL,
  115.                     "Returned OK but str was still NULL");
  116. }
  117. END_TEST
  118.  
  119. START_TEST (test_lwc_intern_string_twice_same_ok)
  120. {
  121.         lwc_string *str1 = NULL, *str2 = NULL;
  122.         fail_unless(lwc_intern_string("A", 1, &str1) == lwc_error_ok,
  123.                     "Unable to intern a simple string");
  124.         fail_unless(str1 != NULL,
  125.                     "Returned OK but str was still NULL");
  126.         fail_unless(lwc_intern_string("A", 1, &str2) == lwc_error_ok,
  127.                     "Unable to intern a simple string");
  128.         fail_unless(str2 != NULL,
  129.                     "Returned OK but str was still NULL");
  130. }
  131. END_TEST
  132.  
  133. /**** The next set of tests need a fixture set with some strings ****/
  134.  
  135. static lwc_string *intern_one = NULL, *intern_two = NULL, *intern_three = NULL, *intern_YAY = NULL;
  136.  
  137. static void
  138. with_filled_context_setup(void)
  139. {
  140.         fail_unless(lwc_intern_string("one", 3, &intern_one) == lwc_error_ok,
  141.                     "Unable to intern 'one'");
  142.         fail_unless(lwc_intern_string("two", 3, &intern_two) == lwc_error_ok,
  143.                     "Unable to intern 'two'");
  144.         fail_unless(lwc_intern_string("three", 5, &intern_three) == lwc_error_ok,
  145.                     "Unable to intern 'three'");
  146.         fail_unless(lwc_intern_string("YAY", 3, &intern_YAY) == lwc_error_ok,
  147.                     "Unable to intern 'YAY'");
  148.        
  149.         fail_unless(intern_one != intern_two, "'one' == 'two'");
  150.         fail_unless(intern_one != intern_three, "'one' == 'three'");
  151.         fail_unless(intern_two != intern_three, "'two' == 'three'");
  152. }
  153.  
  154. static void
  155. with_filled_context_teardown(void)
  156. {
  157.         lwc_string_unref(intern_one);
  158.         lwc_string_unref(intern_two);
  159.         lwc_string_unref(intern_three);
  160.         lwc_string_unref(intern_YAY);
  161. }
  162.  
  163. START_TEST (test_lwc_interning_works)
  164. {
  165.         lwc_string *new_one = NULL;
  166.        
  167.         fail_unless(lwc_intern_string("one", 3, &new_one) == lwc_error_ok,
  168.                     "Unable to re-intern 'one'");
  169.        
  170.         fail_unless(new_one == intern_one,
  171.                     "Internalising of the string failed");
  172. }
  173. END_TEST
  174.  
  175. START_TEST (test_lwc_intern_substring)
  176. {
  177.         lwc_string *new_hre = NULL, *sub_hre = NULL;
  178.        
  179.         fail_unless(lwc_intern_string("hre", 3, &new_hre) == lwc_error_ok,
  180.                     "Unable to intern 'hre'");
  181.         fail_unless(lwc_intern_substring(intern_three,
  182.                                          1, 3, &sub_hre) == lwc_error_ok,
  183.                     "Unable to re-intern 'hre' by substring");
  184.         fail_unless(new_hre == sub_hre,
  185.                     "'hre' != 'hre' -- wow!");
  186. }
  187. END_TEST
  188.  
  189. START_TEST (test_lwc_intern_substring_bad_offset)
  190. {
  191.         lwc_string *str;
  192.        
  193.         fail_unless(lwc_intern_substring(intern_three, 100, 1, &str) == lwc_error_range,
  194.                     "Able to intern substring starting out of range");
  195. }
  196. END_TEST
  197.  
  198. START_TEST (test_lwc_intern_substring_bad_size)
  199. {
  200.         lwc_string *str;
  201.        
  202.         fail_unless(lwc_intern_substring(intern_three, 1, 100, &str) == lwc_error_range,
  203.                     "Able to intern substring ending out of range");
  204. }
  205. END_TEST
  206.  
  207. START_TEST (test_lwc_string_ref_ok)
  208. {
  209.         fail_unless(lwc_string_ref(intern_one) == intern_one,
  210.                     "Oddly, reffing a string didn't return it");
  211. }
  212. END_TEST
  213.  
  214. START_TEST (test_lwc_string_unref_ok)
  215. {
  216.         lwc_string_unref(intern_one);
  217. }
  218. END_TEST
  219.  
  220. START_TEST (test_lwc_string_ref_unref_ok)
  221. {
  222.         lwc_string_ref(intern_one);
  223.         lwc_string_unref(intern_one);
  224. }
  225. END_TEST
  226.  
  227. START_TEST (test_lwc_string_isequal_ok)
  228. {
  229.         bool result = true;
  230.         fail_unless((lwc_string_isequal(intern_one, intern_two, &result)) == lwc_error_ok,
  231.                     "Failure comparing 'one' and 'two'");
  232.         fail_unless(result == false,
  233.                     "'one' == 'two' ?!");
  234. }
  235. END_TEST
  236.  
  237. START_TEST (test_lwc_string_caseless_isequal_ok1)
  238. {
  239.         bool result = true;
  240.         lwc_string *new_ONE;
  241.        
  242.         fail_unless(lwc_intern_string("ONE", 3, &new_ONE) == lwc_error_ok,
  243.                     "Failure interning 'ONE'");
  244.        
  245.         fail_unless((lwc_string_isequal(intern_one, new_ONE, &result)) == lwc_error_ok);
  246.         fail_unless(result == false,
  247.                     "'one' == 'ONE' ?!");
  248.        
  249.         fail_unless((lwc_string_caseless_isequal(intern_one, new_ONE, &result)) == lwc_error_ok,
  250.                     "Failure comparing 'one' and 'ONE' caselessly");
  251.         fail_unless(result == true,
  252.                     "'one' !~= 'ONE' ?!");
  253. }
  254. END_TEST
  255.  
  256. START_TEST (test_lwc_string_caseless_isequal_ok2)
  257. {
  258.         bool result = true;
  259.         lwc_string *new_yay;
  260.        
  261.         fail_unless(lwc_intern_string("yay", 3, &new_yay) == lwc_error_ok,
  262.                     "Failure interning 'yay'");
  263.        
  264.         fail_unless((lwc_string_isequal(intern_YAY, new_yay, &result)) == lwc_error_ok);
  265.         fail_unless(result == false,
  266.                     "'yay' == 'YAY' ?!");
  267.        
  268.         fail_unless((lwc_string_caseless_isequal(intern_YAY, new_yay, &result)) == lwc_error_ok,
  269.                     "Failure comparing 'yay' and 'YAY' caselessly");
  270.         fail_unless(result == true,
  271.                     "'yay' !~= 'YAY' ?!");
  272. }
  273. END_TEST
  274.  
  275. START_TEST (test_lwc_string_caseless_isequal_bad)
  276. {
  277.         bool result = true;
  278.        
  279.         fail_unless(lwc_string_caseless_isequal(intern_YAY, intern_one, &result) == lwc_error_ok,
  280.                     "Failure comparing 'YAY' and 'one' caselessly");
  281.         fail_unless(result == false,
  282.                     "'YAY' ~= 'one' ?!");
  283. }
  284. END_TEST
  285.  
  286. START_TEST (test_lwc_extract_data_ok)
  287. {
  288.         fail_unless(memcmp("one",
  289.                            lwc_string_data(intern_one),
  290.                            lwc_string_length(intern_one)) == 0,
  291.                     "Extracting data ptr etc failed");
  292. }
  293. END_TEST
  294.  
  295. START_TEST (test_lwc_string_hash_value_ok)
  296. {
  297.         (void)lwc_string_hash_value(intern_one);
  298. }
  299. END_TEST
  300.  
  301. START_TEST (test_lwc_string_is_nul_terminated)
  302. {
  303.         lwc_string *new_ONE;
  304.  
  305.         fail_unless(lwc_intern_string("ONE", 3, &new_ONE) == lwc_error_ok,
  306.                     "Failure interning 'ONE'");
  307.  
  308.         fail_unless(lwc_string_data(new_ONE)[lwc_string_length(new_ONE)] == '\0',
  309.                     "Interned string isn't NUL terminated");
  310. }
  311. END_TEST
  312.  
  313. START_TEST (test_lwc_substring_is_nul_terminated)
  314. {
  315.         lwc_string *new_ONE;
  316.         lwc_string *new_O;
  317.  
  318.         fail_unless(lwc_intern_string("ONE", 3, &new_ONE) == lwc_error_ok,
  319.                     "Failure interning 'ONE'");
  320.  
  321.         fail_unless(lwc_intern_substring(new_ONE, 0, 1, &new_O) == lwc_error_ok,
  322.                     "Failure interning substring 'O'");
  323.  
  324.         fail_unless(lwc_string_data(new_O)[lwc_string_length(new_O)] == '\0',
  325.                     "Interned substring isn't NUL terminated");
  326. }
  327. END_TEST
  328.  
  329. static void
  330. counting_cb(lwc_string *str, void *pw)
  331. {
  332.         UNUSED(str);
  333.        
  334.         *((int *)pw) += 1;
  335. }
  336.  
  337. START_TEST (test_lwc_string_iteration)
  338. {
  339.         int counter = 0;
  340.         lwc_iterate_strings(counting_cb, (void*)&counter);
  341.         fail_unless(counter == 4, "Incorrect string count");
  342. }
  343. END_TEST
  344.  
  345. /**** And the suites are set up here ****/
  346.  
  347. void
  348. lwc_basic_suite(SRunner *sr)
  349. {
  350.         Suite *s = suite_create("libwapcaplet: Basic tests");
  351.         TCase *tc_basic = tcase_create("Creation/Destruction");
  352.        
  353. #ifndef NDEBUG
  354.         tcase_add_test_raise_signal(tc_basic,
  355.                                     test_lwc_intern_string_aborts1,
  356.                                     SIGABRT);
  357.         tcase_add_test_raise_signal(tc_basic,
  358.                                     test_lwc_intern_string_aborts2,
  359.                                     SIGABRT);
  360.         tcase_add_test_raise_signal(tc_basic,
  361.                                     test_lwc_intern_substring_aborts1,
  362.                                     SIGABRT);
  363.         tcase_add_test_raise_signal(tc_basic,
  364.                                     test_lwc_intern_substring_aborts2,
  365.                                     SIGABRT);
  366.         tcase_add_test_raise_signal(tc_basic,
  367.                                     test_lwc_string_ref_aborts,
  368.                                     SIGABRT);
  369.         tcase_add_test_raise_signal(tc_basic,
  370.                                     test_lwc_string_unref_aborts,
  371.                                     SIGABRT);
  372.         tcase_add_test_raise_signal(tc_basic,
  373.                                     test_lwc_string_data_aborts,
  374.                                     SIGABRT);
  375.         tcase_add_test_raise_signal(tc_basic,
  376.                                     test_lwc_string_length_aborts,
  377.                                     SIGABRT);
  378.         tcase_add_test_raise_signal(tc_basic,
  379.                                     test_lwc_string_hash_value_aborts,
  380.                                     SIGABRT);
  381. #endif
  382.        
  383.         suite_add_tcase(s, tc_basic);
  384.        
  385.         tc_basic = tcase_create("Ops with a context");
  386.        
  387.         tcase_add_checked_fixture(tc_basic, with_simple_context_setup,
  388.                                   with_simple_context_teardown);
  389.         tcase_add_test(tc_basic, test_lwc_intern_string_ok);
  390.         tcase_add_test(tc_basic, test_lwc_intern_string_twice_ok);
  391.         tcase_add_test(tc_basic, test_lwc_intern_string_twice_same_ok);
  392.         suite_add_tcase(s, tc_basic);
  393.        
  394.         tc_basic = tcase_create("Ops with a filled context");
  395.        
  396.         tcase_add_checked_fixture(tc_basic, with_filled_context_setup,
  397.                                   with_filled_context_teardown);
  398.         tcase_add_test(tc_basic, test_lwc_interning_works);
  399.         tcase_add_test(tc_basic, test_lwc_intern_substring);
  400.         tcase_add_test(tc_basic, test_lwc_string_ref_ok);
  401.         tcase_add_test(tc_basic, test_lwc_string_ref_unref_ok);
  402.         tcase_add_test(tc_basic, test_lwc_string_unref_ok);
  403.         tcase_add_test(tc_basic, test_lwc_string_isequal_ok);
  404.         tcase_add_test(tc_basic, test_lwc_string_caseless_isequal_ok1);
  405.         tcase_add_test(tc_basic, test_lwc_string_caseless_isequal_ok2);
  406.         tcase_add_test(tc_basic, test_lwc_string_caseless_isequal_bad);
  407.         tcase_add_test(tc_basic, test_lwc_extract_data_ok);
  408.         tcase_add_test(tc_basic, test_lwc_string_hash_value_ok);
  409.         tcase_add_test(tc_basic, test_lwc_string_is_nul_terminated);
  410.         tcase_add_test(tc_basic, test_lwc_substring_is_nul_terminated);
  411.         tcase_add_test(tc_basic, test_lwc_intern_substring_bad_size);
  412.         tcase_add_test(tc_basic, test_lwc_intern_substring_bad_offset);
  413.         tcase_add_test(tc_basic, test_lwc_string_iteration);
  414.         suite_add_tcase(s, tc_basic);
  415.        
  416.         srunner_add_suite(sr, s);
  417. }
  418.