LCOV - code coverage report
Current view: top level - lib/util/tests - dlinklist.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 74 74 100.0 %
Date: 2024-04-21 15:09:00 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    local testing of DLIST_*() macros
       5             : 
       6             :    Copyright (C) Andrew Tridgell 2010
       7             : 
       8             :    This program is free software; you can redistribute it and/or modify
       9             :    it under the terms of the GNU General Public License as published by
      10             :    the Free Software Foundation; either version 3 of the License, or
      11             :    (at your option) any later version.
      12             : 
      13             :    This program is distributed in the hope that it will be useful,
      14             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :    GNU General Public License for more details.
      17             : 
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      20             : */
      21             : 
      22             : #include "includes.h"
      23             : #include "torture/torture.h"
      24             : #include "torture/local/proto.h"
      25             : #include "lib/util/dlinklist.h"
      26             : 
      27             : struct listel {
      28             :         struct listel *next, *prev;
      29             : };
      30             : 
      31           1 : static bool torture_local_dlinklist_simple(struct torture_context *tctx)
      32             : {
      33           1 :         TALLOC_CTX *mem_ctx = talloc_new(tctx);
      34           1 :         struct listel *l1 = NULL, *l2 = NULL, *el, *el2;
      35           1 :         int i;
      36             : 
      37           1 :         torture_comment(tctx, "add 5 elements at front\n");
      38           7 :         for (i=0; i<5; i++) {
      39           5 :                 el = talloc(mem_ctx, struct listel);
      40           5 :                 DLIST_ADD(l1, el);
      41             :         }
      42             : 
      43           1 :         torture_comment(tctx, "add 5 elements at end\n");
      44           7 :         for (i=0; i<5; i++) {
      45           5 :                 el = talloc(mem_ctx, struct listel);
      46           5 :                 DLIST_ADD_END(l1, el);
      47             :         }
      48             : 
      49           1 :         torture_comment(tctx, "delete 3 from front\n");
      50           5 :         for (i=0; i < 3; i++) {
      51           3 :                 el = l1;
      52           3 :                 DLIST_REMOVE(l1, l1);
      53           3 :                 DLIST_ADD(l2, el);
      54             :         }
      55             : 
      56           1 :         torture_comment(tctx, "delete 3 from back\n");
      57           5 :         for (i=0; i < 3; i++) {
      58           3 :                 el = DLIST_TAIL(l1);
      59           3 :                 DLIST_REMOVE(l1, el);
      60           3 :                 DLIST_ADD_END(l2, el);
      61             :         }
      62             : 
      63           1 :         torture_comment(tctx, "count forward\n");
      64           6 :         for (i=0,el=l1; el; el=el->next) i++;
      65           1 :         torture_assert_int_equal(tctx, i, 4, "should have 4 elements");
      66             : 
      67           1 :         torture_comment(tctx, "count backwards\n");
      68           6 :         for (i=0,el=DLIST_TAIL(l1); el; el=DLIST_PREV(el)) i++;
      69           1 :         torture_assert_int_equal(tctx, i, 4, "should have 4 elements");
      70             : 
      71           1 :         torture_comment(tctx, "check DLIST_HEAD\n");
      72           1 :         el = DLIST_TAIL(l1);
      73           4 :         DLIST_HEAD(el, el2);
      74           1 :         torture_assert(tctx, el2 == l1, "should find head");
      75             : 
      76           1 :         torture_comment(tctx, "check DLIST_ADD_AFTER\n");
      77           1 :         el  = talloc(mem_ctx, struct listel);
      78           1 :         el2 = talloc(mem_ctx, struct listel);
      79           1 :         DLIST_ADD_AFTER(l1, el, l1);
      80           1 :         DLIST_ADD_AFTER(l1, el2, el);
      81           1 :         torture_assert(tctx, l1->next == el, "2nd in list");
      82           1 :         torture_assert(tctx, el->next == el2, "3rd in list");
      83             : 
      84           1 :         torture_comment(tctx, "check DLIST_PROMOTE\n");
      85           1 :         DLIST_PROMOTE(l1, el2);
      86           1 :         torture_assert(tctx, el2==l1, "1st in list");
      87           1 :         torture_assert(tctx, el2->next->next == el, "3rd in list");
      88             : 
      89           1 :         torture_comment(tctx, "check DLIST_DEMOTE\n");
      90           1 :         DLIST_DEMOTE(l1, el);
      91           1 :         torture_assert(tctx, el->next == NULL, "last in list");
      92           1 :         torture_assert(tctx, el2->prev == el, "backlink from head");
      93             : 
      94           1 :         torture_comment(tctx, "count forward\n");
      95           8 :         for (i=0,el=l1; el; el=el->next) i++;
      96           1 :         torture_assert_int_equal(tctx, i, 6, "should have 6 elements");
      97             : 
      98           1 :         torture_comment(tctx, "count backwards\n");
      99           8 :         for (i=0,el=DLIST_TAIL(l1); el; el=DLIST_PREV(el)) i++;
     100           1 :         torture_assert_int_equal(tctx, i, 6, "should have 6 elements");
     101             : 
     102           1 :         torture_comment(tctx, "check DLIST_CONCATENATE\n");
     103           1 :         DLIST_CONCATENATE(l1, l2);
     104           1 :         torture_comment(tctx, "count forward\n");
     105          14 :         for (i=0,el=l1; el; el=el->next) i++;
     106           1 :         torture_assert_int_equal(tctx, i, 12, "should have 12 elements");
     107             : 
     108           1 :         torture_comment(tctx, "count backwards\n");
     109          14 :         for (i=0,el=DLIST_TAIL(l1); el; el=DLIST_PREV(el)) i++;
     110           1 :         torture_assert_int_equal(tctx, i, 12, "should have 12 elements");
     111             : 
     112           1 :         torture_comment(tctx, "free forwards\n");
     113          14 :         for (el=l1; el; el=el2) {
     114          12 :                 el2 = el->next;
     115          12 :                 DLIST_REMOVE(l1, el);
     116          12 :                 talloc_free(el);
     117             :         }
     118             : 
     119           1 :         torture_assert(tctx, l1 == NULL, "list empty");
     120           1 :         torture_assert_int_equal(tctx, talloc_total_blocks(mem_ctx), 1, "1 block");
     121             : 
     122           1 :         talloc_free(mem_ctx);
     123           1 :         return true;
     124             : }
     125             : 
     126        2354 : struct torture_suite *torture_local_dlinklist(TALLOC_CTX *mem_ctx)
     127             : {
     128        2354 :         struct torture_suite *suite = torture_suite_create(mem_ctx, "dlinklist");
     129        2354 :         torture_suite_add_simple_test(suite, "dlinklist", torture_local_dlinklist_simple);
     130        2354 :         return suite;
     131             : }

Generated by: LCOV version 1.14