early-access version 2862
This commit is contained in:
1
externals/cpp-jwt/include/jwt/test/compile.txt
vendored
Executable file
1
externals/cpp-jwt/include/jwt/test/compile.txt
vendored
Executable file
@@ -0,0 +1 @@
|
||||
g++ -std=c++14 -I /usr/local/Cellar/openssl/1.0.2j/include/ -I /Users/amuralid/dev_test/cpp-jwt/include/ -o test_rsa test_rsa.cc -L /usr/local/Cellar//openssl/1.0.2j/lib/ -lssl -lcrypto
|
BIN
externals/cpp-jwt/include/jwt/test/test_base64
vendored
Executable file
BIN
externals/cpp-jwt/include/jwt/test/test_base64
vendored
Executable file
Binary file not shown.
48
externals/cpp-jwt/include/jwt/test/test_base64.cc
vendored
Executable file
48
externals/cpp-jwt/include/jwt/test/test_base64.cc
vendored
Executable file
@@ -0,0 +1,48 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cassert>
|
||||
#include "jwt/base64.hpp"
|
||||
|
||||
void base64_test_encode()
|
||||
{
|
||||
std::string input = "ArunMu";
|
||||
std::string output = jwt::base64_encode(input.c_str(), input.length());
|
||||
assert (output == "QXJ1bk11");
|
||||
|
||||
input = "Something really strange!!";
|
||||
output = jwt::base64_encode(input.c_str(), input.length());
|
||||
assert (output == "U29tZXRoaW5nIHJlYWxseSBzdHJhbmdlISE=");
|
||||
|
||||
input = "Do you want to know something more stranger ????";
|
||||
output = jwt::base64_encode(input.c_str(), input.length());
|
||||
assert (output == "RG8geW91IHdhbnQgdG8ga25vdyBzb21ldGhpbmcgbW9yZSBzdHJhbmdlciA/Pz8/");
|
||||
|
||||
input = R"({"a" : "b", "c" : [1,2,3,4,5]})";
|
||||
output = jwt::base64_encode(input.c_str(), input.length());
|
||||
assert (output == "eyJhIiA6ICJiIiwgImMiIDogWzEsMiwzLDQsNV19");
|
||||
}
|
||||
|
||||
void base64_test_decode()
|
||||
{
|
||||
std::string input = "QXJ1bk11";
|
||||
std::string output = jwt::base64_decode(input.c_str(), input.length());
|
||||
assert (output == "ArunMu");
|
||||
|
||||
input = "U29tZXRoaW5nIHJlYWxseSBzdHJhbmdlISE=";
|
||||
output = jwt::base64_decode(input.c_str(), input.length());
|
||||
assert (output == "Something really strange!!");
|
||||
|
||||
input = "RG8geW91IHdhbnQgdG8ga25vdyBzb21ldGhpbmcgbW9yZSBzdHJhbmdlciA/Pz8/";
|
||||
output = jwt::base64_decode(input.c_str(), input.length());
|
||||
assert (output == "Do you want to know something more stranger ????");
|
||||
|
||||
input = "eyJhIiA6ICJiIiwgImMiIDogWzEsMiwzLDQsNV19";
|
||||
output = jwt::base64_decode(input.c_str(), input.length());
|
||||
assert (output == R"({"a" : "b", "c" : [1,2,3,4,5]})");
|
||||
}
|
||||
|
||||
int main() {
|
||||
base64_test_encode();
|
||||
base64_test_decode();
|
||||
return 0;
|
||||
}
|
45
externals/cpp-jwt/include/jwt/test/test_evp.c
vendored
Executable file
45
externals/cpp-jwt/include/jwt/test/test_evp.c
vendored
Executable file
@@ -0,0 +1,45 @@
|
||||
#include <stdio.h>
|
||||
#include <openssl/evp.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
EVP_MD_CTX *mdctx;
|
||||
const EVP_MD *md;
|
||||
char mess1[] = "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaXNzIjoiYXJ1bi5jb20iLCJ0aW1lX3N0ciI6Ijg6MThwbSAyNCBOb3YgMjAxNyIsIndoZXJlIjoiYWlycG9ydCJ9";
|
||||
unsigned char md_value[EVP_MAX_MD_SIZE];
|
||||
int md_len, i;
|
||||
|
||||
//OpenSSL_add_all_digests();
|
||||
|
||||
if(!argv[1]) {
|
||||
printf("Usage: mdtest digestname\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
md = EVP_sha256();
|
||||
|
||||
if(!md) {
|
||||
printf("Unknown message digest %s\n", argv[1]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
mdctx = EVP_MD_CTX_create();
|
||||
EVP_DigestInit_ex(mdctx, md, NULL);
|
||||
EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
|
||||
EVP_DigestFinal_ex(mdctx, md_value, &md_len);
|
||||
EVP_MD_CTX_destroy(mdctx);
|
||||
|
||||
printf("Dig: %s\n", md_value);
|
||||
printf("Dig: %d\n", md_len);
|
||||
|
||||
printf("Digest is: ");
|
||||
for(i = 0; i < md_len; i++)
|
||||
printf("%02x", md_value[i]);
|
||||
printf("\n");
|
||||
|
||||
d2i_ECDSA_SIG(NULL, (const unsigned char **)&md_value[0], md_len);
|
||||
|
||||
/* Call this once before exit. */
|
||||
EVP_cleanup();
|
||||
exit(0);
|
||||
}
|
BIN
externals/cpp-jwt/include/jwt/test/test_hmac
vendored
Executable file
BIN
externals/cpp-jwt/include/jwt/test/test_hmac
vendored
Executable file
Binary file not shown.
16
externals/cpp-jwt/include/jwt/test/test_hmac.cc
vendored
Executable file
16
externals/cpp-jwt/include/jwt/test/test_hmac.cc
vendored
Executable file
@@ -0,0 +1,16 @@
|
||||
#include <iostream>
|
||||
#include "jwt/algorithm.hpp"
|
||||
|
||||
void basic_hmac_test()
|
||||
{
|
||||
jwt::string_view sv = "secret" ;
|
||||
jwt::string_view d = "Some random data string";
|
||||
auto res = jwt::HMACSign<jwt::algo::HS256>::sign(sv, d);
|
||||
|
||||
std::cout << res.first << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
basic_hmac_test();
|
||||
return 0;
|
||||
}
|
BIN
externals/cpp-jwt/include/jwt/test/test_jwt_decode
vendored
Executable file
BIN
externals/cpp-jwt/include/jwt/test/test_jwt_decode
vendored
Executable file
Binary file not shown.
30
externals/cpp-jwt/include/jwt/test/test_jwt_decode.cc
vendored
Executable file
30
externals/cpp-jwt/include/jwt/test/test_jwt_decode.cc
vendored
Executable file
@@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
#include "jwt/jwt.hpp"
|
||||
|
||||
void basic_decode_test()
|
||||
{
|
||||
// Create header
|
||||
jwt::jwt_header hdr;
|
||||
hdr = jwt::jwt_header{jwt::algorithm::HS256};
|
||||
|
||||
// Create payload
|
||||
jwt::jwt_payload jp;
|
||||
jp.add_claim("sub", "1234567890");
|
||||
jp.add_claim("name", "John Doe");
|
||||
jp.add_claim("admin", true);
|
||||
|
||||
jwt::jwt_signature sgn{"secret"};
|
||||
std::error_code ec{};
|
||||
auto res = sgn.encode(hdr, jp, ec);
|
||||
std::cout << res << std::endl;
|
||||
|
||||
using namespace jwt::params;
|
||||
|
||||
std::cout << "DECODE: \n";
|
||||
jwt::decode(res, algorithms({"none", "HS256"}), ec, verify(false), secret("secret"));
|
||||
}
|
||||
|
||||
int main() {
|
||||
basic_decode_test();
|
||||
return 0;
|
||||
}
|
BIN
externals/cpp-jwt/include/jwt/test/test_jwt_header
vendored
Executable file
BIN
externals/cpp-jwt/include/jwt/test/test_jwt_header
vendored
Executable file
Binary file not shown.
19
externals/cpp-jwt/include/jwt/test/test_jwt_header.cc
vendored
Executable file
19
externals/cpp-jwt/include/jwt/test/test_jwt_header.cc
vendored
Executable file
@@ -0,0 +1,19 @@
|
||||
#include <iostream>
|
||||
#include "jwt/jwt.hpp"
|
||||
|
||||
void test_basic_header()
|
||||
{
|
||||
jwt::jwt_header hdr;
|
||||
hdr = jwt::jwt_header{jwt::algorithm::HS256};
|
||||
std::string jstr = to_json_str(hdr);
|
||||
std::cout << jstr << std::endl;
|
||||
|
||||
std::string enc_str = hdr.base64_encode();
|
||||
std::cout << "Base64: " << enc_str << std::endl;
|
||||
std::cout << "Decoded: " << hdr.base64_decode(enc_str) << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
test_basic_header();
|
||||
return 0;
|
||||
}
|
BIN
externals/cpp-jwt/include/jwt/test/test_jwt_object
vendored
Executable file
BIN
externals/cpp-jwt/include/jwt/test/test_jwt_object
vendored
Executable file
Binary file not shown.
93
externals/cpp-jwt/include/jwt/test/test_jwt_object.cc
vendored
Executable file
93
externals/cpp-jwt/include/jwt/test/test_jwt_object.cc
vendored
Executable file
@@ -0,0 +1,93 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include <ctime>
|
||||
#include <unordered_map>
|
||||
#include "jwt/jwt.hpp"
|
||||
|
||||
void basic_jwt_object_test()
|
||||
{
|
||||
using namespace jwt::params;
|
||||
jwt::jwt_object obj(payload({
|
||||
{"a", "b"},
|
||||
{"c", "d"}
|
||||
}));
|
||||
|
||||
//check with std::map
|
||||
std::map<std::string, std::string> m;
|
||||
m["a"] = "b";
|
||||
m["c"] = "d";
|
||||
|
||||
jwt::jwt_object obj1{payload(m)};
|
||||
|
||||
auto obj2 = std::move(obj1);
|
||||
|
||||
std::cout << obj2.payload() << std::endl;
|
||||
|
||||
//check with unordered map of string_view
|
||||
std::unordered_map<jwt::string_view, std::string> um = {
|
||||
{"a", "b"},
|
||||
{"c", "d"}
|
||||
};
|
||||
jwt::jwt_object obj3{payload(um)};
|
||||
|
||||
obj3.add_claim("f", true)
|
||||
.add_claim("time", 176353563)
|
||||
.add_claim("exp", std::chrono::system_clock::now())
|
||||
;
|
||||
|
||||
std::cout << jwt::to_json_str(obj3.payload(), true) << std::endl;
|
||||
|
||||
obj3.remove_claim(std::string{"a"});
|
||||
std::cout << obj3.payload() << std::endl;
|
||||
|
||||
obj3.secret("secret");
|
||||
obj3.header().algo("HS256");
|
||||
|
||||
auto dec_obj = jwt::decode(obj3.signature(), algorithms({"HS256"}), secret("secret"));
|
||||
}
|
||||
|
||||
void jwt_object_pem_test()
|
||||
{
|
||||
using namespace jwt::params;
|
||||
|
||||
std::string pub_key =
|
||||
R"(-----BEGIN PUBLIC KEY-----
|
||||
MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEomxC9ycc8AkXSwWQpu1kN5Fmgy/sD/KJ
|
||||
qN3tlSZmUEZ3w3c6KYJfK97PMOSZQaUdeydBoq/IOglQQOj8zLqubq5IpaaUiDQ5
|
||||
0eJg79PvXuLiVUH98cBL/o8sDVB/sGzz
|
||||
-----END PUBLIC KEY-----)";
|
||||
|
||||
std::string priv_key =
|
||||
R"(-----BEGIN EC PRIVATE KEY-----
|
||||
MIGkAgEBBDBeLCgapjZmvTatMHaYX3A02+0Ys3Tr8kda+E9DFnmCSiCOEig519fT
|
||||
13edeU8YdDugBwYFK4EEACKhZANiAASibEL3JxzwCRdLBZCm7WQ3kWaDL+wP8omo
|
||||
3e2VJmZQRnfDdzopgl8r3s8w5JlBpR17J0Gir8g6CVBA6PzMuq5urkilppSINDnR
|
||||
4mDv0+9e4uJVQf3xwEv+jywNUH+wbPM=
|
||||
-----END EC PRIVATE KEY-----)";
|
||||
|
||||
jwt::jwt_object obj;
|
||||
obj.secret(priv_key);
|
||||
obj.header().algo(jwt::algorithm::ES256);
|
||||
|
||||
obj.add_claim("iss", "arun.com")
|
||||
.add_claim("where", "airport")
|
||||
.add_claim("time_str", "8:18pm 24 Nov 2017")
|
||||
.add_claim("id", 1)
|
||||
.add_claim("exp", std::chrono::system_clock::now())
|
||||
;
|
||||
|
||||
std::cout << "pem sign " << obj.signature() << std::endl;
|
||||
std::cout << "Get claim value for exp: " <<
|
||||
obj.payload().get_claim_value<uint64_t>("exp") << std::endl;
|
||||
|
||||
auto dec_obj = jwt::decode(obj.signature(), algorithms({"ES256"}), secret(pub_key));
|
||||
std::cout << dec_obj.payload() << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
basic_jwt_object_test();
|
||||
//jwt_object_pem_test();
|
||||
return 0;
|
||||
}
|
BIN
externals/cpp-jwt/include/jwt/test/test_jwt_payload
vendored
Executable file
BIN
externals/cpp-jwt/include/jwt/test/test_jwt_payload
vendored
Executable file
Binary file not shown.
34
externals/cpp-jwt/include/jwt/test/test_jwt_payload.cc
vendored
Executable file
34
externals/cpp-jwt/include/jwt/test/test_jwt_payload.cc
vendored
Executable file
@@ -0,0 +1,34 @@
|
||||
#include <iostream>
|
||||
#include "jwt/jwt.hpp"
|
||||
|
||||
void basic_payload_test()
|
||||
{
|
||||
jwt::jwt_payload jp;
|
||||
jp.add_claim("iss", "myself");
|
||||
jp.add_claim("exp", 1234567);
|
||||
jp.add_claim("Exp", 1234567, true);
|
||||
|
||||
auto jstr = jwt::to_json_str(jp);
|
||||
std::cout << jstr << std::endl;
|
||||
|
||||
auto enc = jp.base64_encode();
|
||||
std::cout << "Base64 enc: " << enc << std::endl;
|
||||
|
||||
auto dec = jp.base64_decode(enc);
|
||||
std::cout << "Base64 dec: " << dec << std::endl;
|
||||
std::cout << "Base64 dec: " << jstr << std::endl;
|
||||
|
||||
assert (jstr == dec && "Encoded and decoded messages do not match");
|
||||
assert (jp.has_claim("exp") && "Claim exp must exist");
|
||||
assert (jp.has_claim("Exp") && "Claim Exp must exist");
|
||||
|
||||
assert (!jp.has_claim("aud") && "Claim aud does not exist");
|
||||
assert (jp.has_claim_with_value("exp", 1234567) && "Claim exp with value 1234567 does not exist");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int main() {
|
||||
basic_payload_test();
|
||||
return 0;
|
||||
}
|
BIN
externals/cpp-jwt/include/jwt/test/test_jwt_signature
vendored
Executable file
BIN
externals/cpp-jwt/include/jwt/test/test_jwt_signature
vendored
Executable file
Binary file not shown.
25
externals/cpp-jwt/include/jwt/test/test_jwt_signature.cc
vendored
Executable file
25
externals/cpp-jwt/include/jwt/test/test_jwt_signature.cc
vendored
Executable file
@@ -0,0 +1,25 @@
|
||||
#include <iostream>
|
||||
#include "jwt/jwt.hpp"
|
||||
|
||||
void basic_sign_test()
|
||||
{
|
||||
// Create header
|
||||
jwt::jwt_header hdr;
|
||||
hdr = jwt::jwt_header{jwt::algorithm::HS256};
|
||||
|
||||
// Create payload
|
||||
jwt::jwt_payload jp;
|
||||
jp.add_claim("sub", "1234567890");
|
||||
jp.add_claim("name", "John Doe");
|
||||
jp.add_claim("admin", true);
|
||||
|
||||
jwt::jwt_signature sgn{"secret"};
|
||||
std::error_code ec{};
|
||||
auto res = sgn.encode(hdr, jp, ec);
|
||||
std::cout << res << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
basic_sign_test();
|
||||
return 0;
|
||||
}
|
BIN
externals/cpp-jwt/include/jwt/test/test_rsa
vendored
Executable file
BIN
externals/cpp-jwt/include/jwt/test/test_rsa
vendored
Executable file
Binary file not shown.
47
externals/cpp-jwt/include/jwt/test/test_rsa.cc
vendored
Executable file
47
externals/cpp-jwt/include/jwt/test/test_rsa.cc
vendored
Executable file
@@ -0,0 +1,47 @@
|
||||
#include <iostream>
|
||||
#include "jwt/algorithm.hpp"
|
||||
|
||||
static const char* rsa_2048_pem =
|
||||
R"(-----BEGIN PRIVATE KEY-----
|
||||
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDC2kwAziXUf33m
|
||||
iqWp0yG6o259+nj7hpQLC4UT0Hmz0wmvreDJ/yNbSgOvsxvVdvzL2IaRZ+Gi5mo0
|
||||
lswWvL6IGz7PZO0kXTq9sdBnNqMOx27HddV9e/2/p0MgibJTbgywY2Sk23QYhJpq
|
||||
Kq/nU0xlBfSaI5ddZ2RC9ZNkVeGawUKYksTruhAVJqviHN8BoK6VowP5vcxyyOWH
|
||||
TK9KruDqzCIhqwRTeo0spokBkTN/LCuhVivcHAzUiJVtB4qAiTI9L/zkzhjpKz9P
|
||||
45aLU54rj011gG8U/6E1USh5nMnPkr+d3oLfkhfS3Zs3kJVdyFQWZpQxiTaI92Fd
|
||||
2wLvbS0HAgMBAAECggEAD8dTnkETSSjlzhRuI9loAtAXM3Zj86JLPLW7GgaoxEoT
|
||||
n7lJ2bGicFMHB2ROnbOb9vnas82gtOtJsGaBslmoaCckp/C5T1eJWTEb+i+vdpPp
|
||||
wZcmKZovyyRFSE4+NYlU17fEv6DRvuaGBpDcW7QgHJIl45F8QWEM+msee2KE+V4G
|
||||
z/9vAQ+sOlvsb4mJP1tJIBx9Lb5loVREwCRy2Ha9tnWdDNar8EYkOn8si4snPT+E
|
||||
3ZCy8mlcZyUkZeiS/HdtydxZfoiwrSRYamd1diQpPhWCeRteQ802a7ds0Y2YzgfF
|
||||
UaYjNuRQm7zA//hwbXS7ELPyNMU15N00bajlG0tUOQKBgQDnLy01l20OneW6A2cI
|
||||
DIDyYhy5O7uulsaEtJReUlcjEDMkin8b767q2VZHb//3ZH+ipnRYByUUyYUhdOs2
|
||||
DYRGGeAebnH8wpTT4FCYxUsIUpDfB7RwfdBONgaKewTJz/FPswy1Ye0b5H2c6vVi
|
||||
m2FZ33HQcoZ3wvFFqyGVnMzpOwKBgQDXxL95yoxUGKa8vMzcE3Cn01szh0dFq0sq
|
||||
cFpM+HWLVr84CItuG9H6L0KaStEEIOiJsxOVpcXfFFhsJvOGhMA4DQTwH4WuXmXp
|
||||
1PoVMDlV65PYqvhzwL4+QhvZO2bsrEunITXOmU7CI6kilnAN3LuP4HbqZgoX9lqP
|
||||
I31VYzLupQKBgGEYck9w0s/xxxtR9ILv5XRnepLdoJzaHHR991aKFKjYU/KD7JDK
|
||||
INfoAhGs23+HCQhCCtkx3wQVA0Ii/erM0II0ueluD5fODX3TV2ZibnoHW2sgrEsW
|
||||
vFcs36BnvIIaQMptc+f2QgSV+Z/fGsKYadG6Q+39O7au/HB7SHayzWkjAoGBAMgt
|
||||
Fzslp9TpXd9iBWjzfCOnGUiP65Z+GWkQ/SXFqD+SRir0+m43zzGdoNvGJ23+Hd6K
|
||||
TdQbDJ0uoe4MoQeepzoZEgi4JeykVUZ/uVfo+nh06yArVf8FxTm7WVzLGGzgV/uA
|
||||
+wtl/cRtEyAsk1649yW/KHPEIP8kJdYAJeoO8xSlAoGAERMrkFR7KGYZG1eFNRdV
|
||||
mJMq+Ibxyw8ks/CbiI+n3yUyk1U8962ol2Q0T4qjBmb26L5rrhNQhneM4e8mo9FX
|
||||
LlQapYkPvkdrqW0Bp72A/UNAvcGTmN7z5OCJGMUutx2hmEAlrYmpLKS8pM/p9zpK
|
||||
tEOtzsP5GMDYVlEp1jYSjzQ=
|
||||
-----END PRIVATE KEY-----)";
|
||||
|
||||
void basic_rsa_test()
|
||||
{
|
||||
jwt::string_view sv = rsa_2048_pem;
|
||||
jwt::string_view d = "Some random data string";
|
||||
|
||||
auto res = jwt::PEMSign<jwt::algo::RS256>::sign(sv, d);
|
||||
|
||||
std::cout << res.first << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
basic_rsa_test();
|
||||
return 0;
|
||||
}
|
BIN
externals/cpp-jwt/include/jwt/test/test_stack_alloc
vendored
Executable file
BIN
externals/cpp-jwt/include/jwt/test/test_stack_alloc
vendored
Executable file
Binary file not shown.
22
externals/cpp-jwt/include/jwt/test/test_stack_alloc.cc
vendored
Executable file
22
externals/cpp-jwt/include/jwt/test/test_stack_alloc.cc
vendored
Executable file
@@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "jwt/stack_alloc.hpp"
|
||||
|
||||
template <typename T, size_t SZ = 2>
|
||||
using SmallVector = std::vector<T, jwt::stack_alloc<T, SZ, alignof(T)>>;
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
SmallVector<int>::allocator_type::arena_type a;
|
||||
SmallVector<int> v{a};
|
||||
|
||||
v.push_back(1);
|
||||
v.push_back(1);
|
||||
v.push_back(1);
|
||||
v.push_back(1);
|
||||
v.push_back(1);
|
||||
v.push_back(1);
|
||||
|
||||
return 0;
|
||||
}
|
BIN
externals/cpp-jwt/include/jwt/test/test_sv
vendored
Executable file
BIN
externals/cpp-jwt/include/jwt/test/test_sv
vendored
Executable file
Binary file not shown.
169
externals/cpp-jwt/include/jwt/test/test_sv.cc
vendored
Executable file
169
externals/cpp-jwt/include/jwt/test/test_sv.cc
vendored
Executable file
@@ -0,0 +1,169 @@
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include "jwt/string_view.hpp"
|
||||
|
||||
using string_view = jwt::basic_string_view<char>;
|
||||
|
||||
void basic_cons()
|
||||
{
|
||||
// Default construction
|
||||
string_view sv{};
|
||||
assert (sv.length() == 0 && "Size must be zero for default constructor");
|
||||
|
||||
// Construction from string literal
|
||||
string_view sv2{"Arun Muralidharan"};
|
||||
assert (sv2.length() == strlen("Arun Muralidharan") && "Lengths must match");
|
||||
|
||||
const char* haystack = "some really big data with infinite objects....";
|
||||
|
||||
// Construct using part of data
|
||||
string_view sv3{haystack, 4};
|
||||
assert (sv3.length() == 4 && "Partial construction is not ok");
|
||||
assert (sv3.to_string() == "some" && "Partial strings are not equal");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void iterator_test()
|
||||
{
|
||||
string_view sv{"Arun Muralidharan"};
|
||||
for (auto c : sv) std::cout << c;
|
||||
std::cout << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
void str_operations()
|
||||
{
|
||||
string_view sv{"Arun Muralidharan"};
|
||||
string_view tmp = sv;
|
||||
sv.remove_prefix(5);
|
||||
assert (sv.to_string() == "Muralidharan" && "Remove prefix failed");
|
||||
|
||||
sv = tmp;
|
||||
sv.remove_suffix(strlen("Muralidharan"));
|
||||
assert (sv.to_string() == "Arun " && "Remove suffix failed");
|
||||
|
||||
sv=tmp;
|
||||
{
|
||||
std::unique_ptr<char[]> dst{new char[32]};
|
||||
sv.copy(dst.get(), 6, 0);
|
||||
dst[6] = '\0';
|
||||
assert (strlen(dst.get()) == 6 && "Copy Failed-1");
|
||||
assert (std::string{dst.get()} == "Arun M" && "Copy Failed-2");
|
||||
|
||||
sv.copy(dst.get(), 8, 4);
|
||||
dst[8] = '\0';
|
||||
assert (strlen(dst.get()) == 8 && "Middle copy failed-1");
|
||||
assert (std::string{dst.get()} == " Muralid" && "Middle copy failed-2");
|
||||
}
|
||||
|
||||
{
|
||||
auto ss1 = sv.substr(0, 4);
|
||||
assert (ss1.to_string() == "Arun" && "Substr failed - 1");
|
||||
|
||||
auto ss2 = sv.substr(1, 3);
|
||||
assert (ss2.to_string() == "run" && "Substr failed - 2");
|
||||
|
||||
auto ss3 = sv.substr(0);
|
||||
assert (ss3.length() == sv.length() && "Substr failed - 3");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void find_oper()
|
||||
{
|
||||
string_view sv{"Arun Muralidharan"};
|
||||
auto pos = sv.find("Arun", 0, 4);
|
||||
assert (pos == 0 && "Arun not found in sv");
|
||||
|
||||
pos = sv.find("arun", 0, 4);
|
||||
assert (pos == string_view::npos && "arun is not there in sv");
|
||||
|
||||
sv = "This has a, in it.";
|
||||
pos = sv.find_first_of(",", 0, 1);
|
||||
assert (pos != string_view::npos);
|
||||
assert (pos == 10 && "Comma not found at correct place");
|
||||
|
||||
pos = sv.find_first_of(",", 10, 1);
|
||||
assert (pos != string_view::npos);
|
||||
assert (pos == 10 && "Comma not found at correct place");
|
||||
|
||||
pos = sv.find_first_of(":", 10, 1);
|
||||
assert (pos == string_view::npos);
|
||||
|
||||
pos = sv.find_last_of(",", 5, 1);
|
||||
assert (pos == string_view::npos);
|
||||
|
||||
pos = sv.find_last_of(",", sv.length() - 1, 1);
|
||||
assert (pos != string_view::npos);
|
||||
assert (pos == 10 && "Comma not found at correct place");
|
||||
|
||||
pos = sv.find_first_of(".", 0, 1);
|
||||
assert (pos == sv.length() - 1 && "Dot not found at correct place");
|
||||
|
||||
pos = sv.find_last_of(".", sv.length() - 2, 1);
|
||||
assert (pos == string_view::npos);
|
||||
|
||||
pos = sv.find_last_of(".", sv.length() - 1, 1);
|
||||
assert (pos == sv.length() - 1);
|
||||
|
||||
sv = "Some string :<> with some ??? pattern --**";
|
||||
|
||||
pos = sv.rfind("???", sv.length() - 1, 3);
|
||||
assert (pos != string_view::npos && "??? not found");
|
||||
assert (pos == 26 && "??? not found at the correct place");
|
||||
|
||||
sv = "ATCGTTCACGRRRTCGGGGACGTC";
|
||||
|
||||
pos = sv.find_first_not_of("ATCG");
|
||||
assert (pos != string_view::npos);
|
||||
assert (pos == 10);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void conversions()
|
||||
{
|
||||
auto c2sv = [](int num) -> string_view {
|
||||
switch (num) {
|
||||
case 1: return "one";
|
||||
case 2: return "two";
|
||||
case 3: return "three";
|
||||
default: return "many";
|
||||
};
|
||||
};
|
||||
|
||||
auto res = c2sv(2);
|
||||
assert (res.to_string() == "two");
|
||||
|
||||
auto s2sv = [](std::string s) {
|
||||
return s;
|
||||
};
|
||||
|
||||
s2sv(static_cast<std::string>(res));
|
||||
}
|
||||
|
||||
void comparisons()
|
||||
{
|
||||
string_view s1{"Apple"};
|
||||
string_view s2{"Orange"};
|
||||
|
||||
assert (s1 != s2 && "Two string views are not equal");
|
||||
assert (s2 > s1 && "Orange is lexicographically bigger than Apple");
|
||||
|
||||
s2 = "Apples";
|
||||
assert (s2 > s1 && "Because Apples is plural");
|
||||
}
|
||||
|
||||
int main() {
|
||||
basic_cons();
|
||||
iterator_test();
|
||||
str_operations();
|
||||
find_oper();
|
||||
conversions();
|
||||
comparisons();
|
||||
return 0;
|
||||
};
|
Reference in New Issue
Block a user