Line data Source code
1 : // Copyright (c) 2018-2024 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/snapdev
4 : // contact@m2osw.com
5 : //
6 : // This program is free software: you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation, either version 3 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License
17 : // along with this program. If not, see <https://www.gnu.org/licenses/>.
18 :
19 : /** \file
20 : * \brief Verify that the memsearch() function works.
21 : *
22 : * This file implements tests for the memsearch() function.
23 : */
24 :
25 : // self
26 : //
27 : #include <snapdev/memsearch.h>
28 :
29 : #include "catch_main.h"
30 :
31 :
32 : // last include
33 : //
34 : #include <snapdev/poison.h>
35 :
36 :
37 :
38 :
39 1 : CATCH_TEST_CASE("memsearch", "[memory]")
40 : {
41 1 : CATCH_START_SECTION("memsearch: verify memory search")
42 : {
43 1 : char const haystack[] = {
44 : 0x00, 0x30, 0x18, 0x29, static_cast<char>(0xFF), 0x45, 0x71, static_cast<char>(0xAC),
45 : 0x10, 0x41, 0x58, 0x33, static_cast<char>(0xEF), static_cast<char>(0xD5), 0x6F, 0x39,
46 : };
47 :
48 1 : char const needle1[] = { 0x18, 0x29, static_cast<char>(0xFF) };
49 1 : int pos(snapdev::memsearch(haystack, sizeof(haystack), needle1, sizeof(needle1)));
50 1 : CATCH_REQUIRE(pos == 2);
51 1 : pos = snapdev::memsearch(needle1, sizeof(needle1), haystack, sizeof(haystack));
52 1 : CATCH_REQUIRE(pos == -1);
53 :
54 1 : char const needle2[] = { 0x11, 0x29, static_cast<char>(0xFF) };
55 1 : pos = snapdev::memsearch(haystack, sizeof(haystack), needle2, sizeof(needle2));
56 1 : CATCH_REQUIRE(pos == -1);
57 1 : pos = snapdev::memsearch(needle2, sizeof(needle2), haystack, sizeof(haystack));
58 1 : CATCH_REQUIRE(pos == -1);
59 :
60 1 : char const needle3[] = { static_cast<char>(0xD5), 0x6F, 0x39 };
61 1 : pos = snapdev::memsearch(haystack, sizeof(haystack), needle3, sizeof(needle3));
62 1 : CATCH_REQUIRE(pos == 13);
63 1 : pos = snapdev::memsearch(needle3, sizeof(needle3), haystack, sizeof(haystack));
64 1 : CATCH_REQUIRE(pos == -1);
65 :
66 1 : pos = snapdev::memsearch(haystack, sizeof(haystack), haystack, sizeof(haystack));
67 1 : CATCH_REQUIRE(pos == 0);
68 : }
69 1 : CATCH_END_SECTION()
70 1 : }
71 :
72 :
73 :
74 : // vim: ts=4 sw=4 et
|