File: buf_insert.c

package info (click to toggle)
neomutt 20250510%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 23,372 kB
  • sloc: ansic: 171,730; sh: 5,357; perl: 1,121; tcl: 1,065; python: 443; makefile: 48
file content (120 lines) | stat: -rw-r--r-- 3,722 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
 * @file
 * Test code for buf_insert()
 *
 * @authors
 * Copyright (C) 2023 Richard Russon <rich@flatcap.org>
 * Copyright (C) 2023 Pietro Cerutti <gahr@gahr.ch>
 *
 * @copyright
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#define TEST_NO_MAIN
#include "config.h"
#include "acutest.h"
#include <stddef.h>
#include "mutt/lib.h"
#include "test_common.h"

struct InsertTest
{
  const char *orig;
  int position;
  const char *insert;
  const char *result;
};

void test_buf_insert(void)
{
  // Degenerate tests
  {
    TEST_CHECK(buf_insert(NULL, 0, NULL) == -1);
  }

  {
    struct Buffer buf = { 0 };
    TEST_CHECK(buf_insert(&buf, 0, NULL) == -1);
  }

  {
    TEST_CHECK(buf_insert(NULL, 0, "something") == -1);
  }

  static const struct InsertTest tests[] = {
    // clang-format off
    { NULL,          0,  "I",      "I"       },
    { NULL,          0,  "INSERT", "INSERT"  },
    { NULL,          1,  "I",      " I"      },
    { NULL,          1,  "INSERT", " INSERT" },

    { "a",           0,  "I",      "Ia"       },
    { "a",           0,  "INSERT", "INSERTa"  },
    { "a",           1,  "I",      "aI"       },
    { "a",           1,  "INSERT", "aINSERT"  },
    { "a",           2,  "I",      "a I"      },
    { "a",           2,  "INSERT", "a INSERT" },

    { "ab",          0,  "I",      "Iab"       },
    { "ab",          0,  "INSERT", "INSERTab"  },
    { "ab",          1,  "I",      "aIb"       },
    { "ab",          1,  "INSERT", "aINSERTb"  },
    { "ab",          2,  "I",      "abI"       },
    { "ab",          2,  "INSERT", "abINSERT"  },
    { "ab",          3,  "I",      "ab I"      },
    { "ab",          3,  "INSERT", "ab INSERT" },

    { "applebanana", 0,  "I",      "Iapplebanana"       },
    { "applebanana", 0,  "INSERT", "INSERTapplebanana"  },
    { "applebanana", 1,  "I",      "aIpplebanana"       },
    { "applebanana", 1,  "INSERT", "aINSERTpplebanana"  },
    { "applebanana", 5,  "I",      "appleIbanana"       },
    { "applebanana", 5,  "INSERT", "appleINSERTbanana"  },
    { "applebanana", 10, "I",      "applebananIa"       },
    { "applebanana", 10, "INSERT", "applebananINSERTa"  },
    { "applebanana", 11, "I",      "applebananaI"       },
    { "applebanana", 11, "INSERT", "applebananaINSERT"  },
    { "applebanana", 12, "I",      "applebanana I"      },
    { "applebanana", 12, "INSERT", "applebanana INSERT" },

    { NULL, 0, NULL, NULL },
    // clang-format on
  };

  {
    for (size_t i = 0; tests[i].result; i++)
    {
      TEST_CASE_("%d", i);
      struct Buffer *buf = buf_pool_get();
      buf_addstr(buf, tests[i].orig);
      buf_insert(buf, tests[i].position, tests[i].insert);
      TEST_CHECK_STR_EQ(buf_string(buf), tests[i].result);
      buf_pool_release(&buf);
    }
  }

  {
    // Insertion that triggers a realloc
    struct Buffer *buf = buf_pool_get();
    const size_t len = buf->dsize;
    for (size_t i = 0; i < len - 2; ++i)
    {
      buf_addch(buf, 'A');
    }
    TEST_CHECK(buf->dsize == len);
    buf_insert(buf, len / 2, "CDEFG");
    TEST_CHECK(buf->dsize != len);
    buf_pool_release(&buf);
  }
}