File:  [Qemu by Fabrice Bellard] / qemu / check-qdict.c
Revision 1.1.1.3 (vendor branch): download - view: text, annotated - select for diffs
Tue Apr 24 18:56:20 2018 UTC (3 years, 1 month ago) by root
Branches: qemu, MAIN
CVS tags: qemu1000, qemu0151, HEAD
qemu 0.15.1

    1: /*
    2:  * QDict unit-tests.
    3:  *
    4:  * Copyright (C) 2009 Red Hat Inc.
    5:  *
    6:  * Authors:
    7:  *  Luiz Capitulino <lcapitulino@redhat.com>
    8:  *
    9:  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
   10:  * See the COPYING.LIB file in the top-level directory.
   11:  */
   12: #include <check.h>
   13: 
   14: #include "qint.h"
   15: #include "qdict.h"
   16: #include "qstring.h"
   17: #include "qemu-common.h"
   18: 
   19: /*
   20:  * Public Interface test-cases
   21:  *
   22:  * (with some violations to access 'private' data)
   23:  */
   24: 
   25: START_TEST(qdict_new_test)
   26: {
   27:     QDict *qdict;
   28: 
   29:     qdict = qdict_new();
   30:     fail_unless(qdict != NULL);
   31:     fail_unless(qdict_size(qdict) == 0);
   32:     fail_unless(qdict->base.refcnt == 1);
   33:     fail_unless(qobject_type(QOBJECT(qdict)) == QTYPE_QDICT);
   34: 
   35:     // destroy doesn't exit yet
   36:     free(qdict);
   37: }
   38: END_TEST
   39: 
   40: START_TEST(qdict_put_obj_test)
   41: {
   42:     QInt *qi;
   43:     QDict *qdict;
   44:     QDictEntry *ent;
   45:     const int num = 42;
   46: 
   47:     qdict = qdict_new();
   48: 
   49:     // key "" will have tdb hash 12345
   50:     qdict_put_obj(qdict, "", QOBJECT(qint_from_int(num)));
   51: 
   52:     fail_unless(qdict_size(qdict) == 1);
   53:     ent = QLIST_FIRST(&qdict->table[12345 % QDICT_BUCKET_MAX]);
   54:     qi = qobject_to_qint(ent->value);
   55:     fail_unless(qint_get_int(qi) == num);
   56: 
   57:     // destroy doesn't exit yet
   58:     QDECREF(qi);
   59:     qemu_free(ent->key);
   60:     qemu_free(ent);
   61:     qemu_free(qdict);
   62: }
   63: END_TEST
   64: 
   65: START_TEST(qdict_destroy_simple_test)
   66: {
   67:     QDict *qdict;
   68: 
   69:     qdict = qdict_new();
   70:     qdict_put_obj(qdict, "num", QOBJECT(qint_from_int(0)));
   71:     qdict_put_obj(qdict, "str", QOBJECT(qstring_from_str("foo")));
   72: 
   73:     QDECREF(qdict);
   74: }
   75: END_TEST
   76: 
   77: static QDict *tests_dict = NULL;
   78: 
   79: static void qdict_setup(void)
   80: {
   81:     tests_dict = qdict_new();
   82:     fail_unless(tests_dict != NULL);
   83: }
   84: 
   85: static void qdict_teardown(void)
   86: {
   87:     QDECREF(tests_dict);
   88:     tests_dict = NULL;
   89: }
   90: 
   91: START_TEST(qdict_get_test)
   92: {
   93:     QInt *qi;
   94:     QObject *obj;
   95:     const int value = -42;
   96:     const char *key = "test";
   97: 
   98:     qdict_put(tests_dict, key, qint_from_int(value));
   99: 
  100:     obj = qdict_get(tests_dict, key);
  101:     fail_unless(obj != NULL);
  102: 
  103:     qi = qobject_to_qint(obj);
  104:     fail_unless(qint_get_int(qi) == value);
  105: }
  106: END_TEST
  107: 
  108: START_TEST(qdict_get_int_test)
  109: {
  110:     int ret;
  111:     const int value = 100;
  112:     const char *key = "int";
  113: 
  114:     qdict_put(tests_dict, key, qint_from_int(value));
  115: 
  116:     ret = qdict_get_int(tests_dict, key);
  117:     fail_unless(ret == value);
  118: }
  119: END_TEST
  120: 
  121: START_TEST(qdict_get_try_int_test)
  122: {
  123:     int ret;
  124:     const int value = 100;
  125:     const char *key = "int";
  126: 
  127:     qdict_put(tests_dict, key, qint_from_int(value));
  128: 
  129:     ret = qdict_get_try_int(tests_dict, key, 0);
  130:     fail_unless(ret == value);
  131: }
  132: END_TEST
  133: 
  134: START_TEST(qdict_get_str_test)
  135: {
  136:     const char *p;
  137:     const char *key = "key";
  138:     const char *str = "string";
  139: 
  140:     qdict_put(tests_dict, key, qstring_from_str(str));
  141: 
  142:     p = qdict_get_str(tests_dict, key);
  143:     fail_unless(p != NULL);
  144:     fail_unless(strcmp(p, str) == 0);
  145: }
  146: END_TEST
  147: 
  148: START_TEST(qdict_get_try_str_test)
  149: {
  150:     const char *p;
  151:     const char *key = "key";
  152:     const char *str = "string";
  153: 
  154:     qdict_put(tests_dict, key, qstring_from_str(str));
  155: 
  156:     p = qdict_get_try_str(tests_dict, key);
  157:     fail_unless(p != NULL);
  158:     fail_unless(strcmp(p, str) == 0);
  159: }
  160: END_TEST
  161: 
  162: START_TEST(qdict_haskey_not_test)
  163: {
  164:     fail_unless(qdict_haskey(tests_dict, "test") == 0);
  165: }
  166: END_TEST
  167: 
  168: START_TEST(qdict_haskey_test)
  169: {
  170:     const char *key = "test";
  171: 
  172:     qdict_put(tests_dict, key, qint_from_int(0));
  173:     fail_unless(qdict_haskey(tests_dict, key) == 1);
  174: }
  175: END_TEST
  176: 
  177: START_TEST(qdict_del_test)
  178: {
  179:     const char *key = "key test";
  180: 
  181:     qdict_put(tests_dict, key, qstring_from_str("foo"));
  182:     fail_unless(qdict_size(tests_dict) == 1);
  183: 
  184:     qdict_del(tests_dict, key);
  185: 
  186:     fail_unless(qdict_size(tests_dict) == 0);
  187:     fail_unless(qdict_haskey(tests_dict, key) == 0);
  188: }
  189: END_TEST
  190: 
  191: START_TEST(qobject_to_qdict_test)
  192: {
  193:     fail_unless(qobject_to_qdict(QOBJECT(tests_dict)) == tests_dict);
  194: }
  195: END_TEST
  196: 
  197: START_TEST(qdict_iterapi_test)
  198: {
  199:     int count;
  200:     const QDictEntry *ent;
  201: 
  202:     fail_unless(qdict_first(tests_dict) == NULL);
  203: 
  204:     qdict_put(tests_dict, "key1", qint_from_int(1));
  205:     qdict_put(tests_dict, "key2", qint_from_int(2));
  206:     qdict_put(tests_dict, "key3", qint_from_int(3));
  207: 
  208:     count = 0;
  209:     for (ent = qdict_first(tests_dict); ent; ent = qdict_next(tests_dict, ent)){
  210:         fail_unless(qdict_haskey(tests_dict, qdict_entry_key(ent)) == 1);
  211:         count++;
  212:     }
  213: 
  214:     fail_unless(count == qdict_size(tests_dict));
  215: 
  216:     /* Do it again to test restarting */
  217:     count = 0;
  218:     for (ent = qdict_first(tests_dict); ent; ent = qdict_next(tests_dict, ent)){
  219:         fail_unless(qdict_haskey(tests_dict, qdict_entry_key(ent)) == 1);
  220:         count++;
  221:     }
  222: 
  223:     fail_unless(count == qdict_size(tests_dict));
  224: }
  225: END_TEST
  226: 
  227: /*
  228:  * Errors test-cases
  229:  */
  230: 
  231: START_TEST(qdict_put_exists_test)
  232: {
  233:     int value;
  234:     const char *key = "exists";
  235: 
  236:     qdict_put(tests_dict, key, qint_from_int(1));
  237:     qdict_put(tests_dict, key, qint_from_int(2));
  238: 
  239:     value = qdict_get_int(tests_dict, key);
  240:     fail_unless(value == 2);
  241: 
  242:     fail_unless(qdict_size(tests_dict) == 1);
  243: }
  244: END_TEST
  245: 
  246: START_TEST(qdict_get_not_exists_test)
  247: {
  248:     fail_unless(qdict_get(tests_dict, "foo") == NULL);
  249: }
  250: END_TEST
  251: 
  252: /*
  253:  * Stress test-case
  254:  *
  255:  * This is a lot big for a unit-test, but there is no other place
  256:  * to have it.
  257:  */
  258: 
  259: static void remove_dots(char *string)
  260: {
  261:     char *p = strchr(string, ':');
  262:     if (p)
  263:         *p = '\0';
  264: }
  265: 
  266: static QString *read_line(FILE *file, char *key)
  267: {
  268:     char value[128];
  269: 
  270:     if (fscanf(file, "%127s%127s", key, value) == EOF) {
  271:         return NULL;
  272:     }
  273:     remove_dots(key);
  274:     return qstring_from_str(value);
  275: }
  276: 
  277: #define reset_file(file)    fseek(file, 0L, SEEK_SET)
  278: 
  279: START_TEST(qdict_stress_test)
  280: {
  281:     size_t lines;
  282:     char key[128];
  283:     FILE *test_file;
  284:     QDict *qdict;
  285:     QString *value;
  286:     const char *test_file_path = "qdict-test-data.txt";
  287: 
  288:     test_file = fopen(test_file_path, "r");
  289:     fail_unless(test_file != NULL);
  290: 
  291:     // Create the dict
  292:     qdict = qdict_new();
  293:     fail_unless(qdict != NULL);
  294: 
  295:     // Add everything from the test file
  296:     for (lines = 0;; lines++) {
  297:         value = read_line(test_file, key);
  298:         if (!value)
  299:             break;
  300: 
  301:         qdict_put(qdict, key, value);
  302:     }
  303:     fail_unless(qdict_size(qdict) == lines);
  304: 
  305:     // Check if everything is really in there
  306:     reset_file(test_file);
  307:     for (;;) {
  308:         const char *str1, *str2;
  309: 
  310:         value = read_line(test_file, key);
  311:         if (!value)
  312:             break;
  313: 
  314:         str1 = qstring_get_str(value);
  315: 
  316:         str2 = qdict_get_str(qdict, key);
  317:         fail_unless(str2 != NULL);
  318: 
  319:         fail_unless(strcmp(str1, str2) == 0);
  320: 
  321:         QDECREF(value);
  322:     }
  323: 
  324:     // Delete everything
  325:     reset_file(test_file);
  326:     for (;;) {
  327:         value = read_line(test_file, key);
  328:         if (!value)
  329:             break;
  330: 
  331:         qdict_del(qdict, key);
  332:         QDECREF(value);
  333: 
  334:         fail_unless(qdict_haskey(qdict, key) == 0);
  335:     }
  336:     fclose(test_file);
  337: 
  338:     fail_unless(qdict_size(qdict) == 0);
  339:     QDECREF(qdict);
  340: }
  341: END_TEST
  342: 
  343: static Suite *qdict_suite(void)
  344: {
  345:     Suite *s;
  346:     TCase *qdict_public_tcase;
  347:     TCase *qdict_public2_tcase;
  348:     TCase *qdict_stress_tcase;
  349:     TCase *qdict_errors_tcase;
  350: 
  351:     s = suite_create("QDict test-suite");
  352: 
  353:     qdict_public_tcase = tcase_create("Public Interface");
  354:     suite_add_tcase(s, qdict_public_tcase);
  355:     tcase_add_test(qdict_public_tcase, qdict_new_test);
  356:     tcase_add_test(qdict_public_tcase, qdict_put_obj_test);
  357:     tcase_add_test(qdict_public_tcase, qdict_destroy_simple_test);
  358: 
  359:     /* Continue, but now with fixtures */
  360:     qdict_public2_tcase = tcase_create("Public Interface (2)");
  361:     suite_add_tcase(s, qdict_public2_tcase);
  362:     tcase_add_checked_fixture(qdict_public2_tcase, qdict_setup, qdict_teardown);
  363:     tcase_add_test(qdict_public2_tcase, qdict_get_test);
  364:     tcase_add_test(qdict_public2_tcase, qdict_get_int_test);
  365:     tcase_add_test(qdict_public2_tcase, qdict_get_try_int_test);
  366:     tcase_add_test(qdict_public2_tcase, qdict_get_str_test);
  367:     tcase_add_test(qdict_public2_tcase, qdict_get_try_str_test);
  368:     tcase_add_test(qdict_public2_tcase, qdict_haskey_not_test);
  369:     tcase_add_test(qdict_public2_tcase, qdict_haskey_test);
  370:     tcase_add_test(qdict_public2_tcase, qdict_del_test);
  371:     tcase_add_test(qdict_public2_tcase, qobject_to_qdict_test);
  372:     tcase_add_test(qdict_public2_tcase, qdict_iterapi_test);
  373: 
  374:     qdict_errors_tcase = tcase_create("Errors");
  375:     suite_add_tcase(s, qdict_errors_tcase);
  376:     tcase_add_checked_fixture(qdict_errors_tcase, qdict_setup, qdict_teardown);
  377:     tcase_add_test(qdict_errors_tcase, qdict_put_exists_test);
  378:     tcase_add_test(qdict_errors_tcase, qdict_get_not_exists_test);
  379: 
  380:     /* The Big one */
  381:     qdict_stress_tcase = tcase_create("Stress Test");
  382:     suite_add_tcase(s, qdict_stress_tcase);
  383:     tcase_add_test(qdict_stress_tcase, qdict_stress_test);
  384: 
  385:     return s;
  386: }
  387: 
  388: int main(void)
  389: {
  390: 	int nf;
  391: 	Suite *s;
  392: 	SRunner *sr;
  393: 
  394: 	s = qdict_suite();
  395: 	sr = srunner_create(s);
  396: 
  397: 	srunner_run_all(sr, CK_NORMAL);
  398: 	nf = srunner_ntests_failed(sr);
  399: 	srunner_free(sr);
  400: 
  401: 	return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  402: }

unix.superglobalmegacorp.com