C and C++ web framework. http://rapida.vilor.one/docs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

62 lines
1.7 KiB

/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright 2022 Ivan Polyakov */
#include "keyval.h"
#include "tests.hxx"
using namespace rpd;
TEST_CASE("Key-value storage")
{
rpd_keyval keyval;
rpd_keyval_init(&keyval, 10);
SECTION("Initializing")
{
rpd_keyval_init(&keyval, 10);
REQUIRE(keyval.size == 0);
REQUIRE(keyval.capacity == 10);
}
SECTION("Inserting")
{
REQUIRE(rpd_keyval_insert(&keyval, "key", "val") == 0);
REQUIRE(keyval.size == 1);
REQUIRE(std::string(keyval.items[0].key) == "key");
REQUIRE(std::string(keyval.items[0].val) == "val");
}
SECTION("Search")
{
int sz = keyval.size;
REQUIRE(rpd_keyval_insert(&keyval, "key1", "val") == 0);
REQUIRE(keyval.size == sz + 1);
rpd_keyval_item *item = rpd_keyval_find(&keyval, "key1");
REQUIRE(item != NULL);
REQUIRE(std::string(item->val) == "val");
}
SECTION("Passing duplicates to non-unique storage and finding them")
{
int sz = keyval.size;
keyval.unique = 0;
rpd_keyval_insert(&keyval, "Set-Cookie", "param=val");
rpd_keyval_insert(&keyval, "Set-Cookie", "param1=val1");
REQUIRE(keyval.size == sz + 2);
rpd_keyval_item **cookies = rpd_keyval_findall(&keyval, "Set-Cookie");
REQUIRE(cookies != NULL);
REQUIRE(std::string(cookies[0]->key) == "Set-Cookie");
REQUIRE(std::string(cookies[0]->val) == "param=val");
REQUIRE(std::string(cookies[1]->key) == "Set-Cookie");
REQUIRE(std::string(cookies[1]->val) == "param1=val1");
REQUIRE(cookies[2] == NULL);
}
SECTION("Cleanup")
{
rpd_keyval_cleanup(&keyval);
REQUIRE(keyval.size == 0);
}
}