id
int64 0
877k
| file_name
stringlengths 3
109
| file_path
stringlengths 13
185
| content
stringlengths 31
9.38M
| size
int64 31
9.38M
| language
stringclasses 1
value | extension
stringclasses 11
values | total_lines
int64 1
340k
| avg_line_length
float64 2.18
149k
| max_line_length
int64 7
2.22M
| alphanum_fraction
float64 0
1
| repo_name
stringlengths 6
66
| repo_stars
int64 94
47.3k
| repo_forks
int64 0
12k
| repo_open_issues
int64 0
3.4k
| repo_license
stringclasses 11
values | repo_extraction_date
stringclasses 197
values | exact_duplicates_redpajama
bool 2
classes | near_duplicates_redpajama
bool 2
classes | exact_duplicates_githubcode
bool 2
classes | exact_duplicates_stackv2
bool 1
class | exact_duplicates_stackv1
bool 2
classes | near_duplicates_githubcode
bool 2
classes | near_duplicates_stackv1
bool 2
classes | near_duplicates_stackv2
bool 1
class |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1,535,416
|
basic_io.cc
|
falk-werner_zipsign/lib/openssl++/basic_io.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "openssl++/basic_io.hpp"
#include "openssl++/exception.hpp"
#include <cstdio>
namespace openssl
{
BasicIO BasicIO::openInputFile(std::string const & filename)
{
BIO * file = BIO_new_file(filename.c_str(), "rb");
if (NULL == file)
{
throw FileNotFoundException(filename);
}
return BasicIO(file);
}
BasicIO BasicIO::fromMemory()
{
BIO * bio = BIO_new(BIO_s_mem());
return BasicIO(bio);
}
BasicIO BasicIO::fromMemory(void const * data, size_t size)
{
BIO * bio = BIO_new_mem_buf(data, size);
return BasicIO(bio);
}
BasicIO::BasicIO(BIO * bio_)
: bio(bio_)
{
}
BasicIO::~BasicIO()
{
BIO_free_all(bio);
}
BasicIO & BasicIO::operator=(BasicIO && other)
{
BIO_free_all(this->bio);
this->bio = other.bio;
other.bio = nullptr;
return *this;
}
BasicIO::BasicIO(BasicIO && other)
{
this->bio = other.bio;
other.bio = nullptr;
}
BasicIO::operator BIO*()
{
return bio;
}
}
| 1,167
|
C++
|
.cc
| 52
| 19.634615
| 70
| 0.673339
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| true
| false
|
1,535,417
|
init.cc
|
falk-werner_zipsign/lib/openssl++/init.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "openssl++/init.hpp"
#include "openssl++/exception.hpp"
#include <openssl/conf.h>
#include <openssl/err.h>
#include <openssl/evp.h>
namespace openssl
{
void OpenSSL::init(void)
{
#if (OPENSSL_VERSION_NUMBER < 0x10100000L)
OPENSSL_config(NULL);
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
#else
int const rc = OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
if (1 != rc)
{
throw OpenSSLException("unable to initialize openssl");
}
#endif
}
}
| 744
|
C++
|
.cc
| 25
| 27.28
| 106
| 0.722689
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| true
| false
| true
| false
|
1,535,418
|
certificate_stack.cc
|
falk-werner_zipsign/lib/openssl++/certificate_stack.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <openssl++/certificate_stack.hpp>
#include <openssl++/exception.hpp>
namespace openssl
{
CertificateStack::CertificateStack()
: stack(sk_X509_new_null())
{
if (nullptr == stack)
{
throw OpenSSLException("failed to create CertificateStack");
}
}
CertificateStack::~CertificateStack()
{
sk_X509_free(stack);
}
CertificateStack::operator STACK_OF(X509) *()
{
return stack;
}
void CertificateStack::push(X509 * certificate)
{
int rc = sk_X509_push(stack, certificate);
if (0 == rc)
{
throw OpenSSLException("failed to push certificate to stack");
}
}
}
| 823
|
C++
|
.cc
| 32
| 22.8125
| 70
| 0.707908
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| true
| false
|
1,535,419
|
certificate_store.cc
|
falk-werner_zipsign/lib/openssl++/certificate_store.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "openssl++/certificate_store.hpp"
#include "openssl++/exception.hpp"
namespace openssl
{
CertificateStore::CertificateStore()
: store(X509_STORE_new())
{
if (nullptr == store)
{
throw OpenSSLException("failed to create certifiacte store");
}
}
CertificateStore::~CertificateStore()
{
X509_STORE_free(store);
}
CertificateStore & CertificateStore::operator=(CertificateStore && other)
{
X509_STORE_free(this->store);
this->store = other.store;
other.store = nullptr;
return *this;
}
CertificateStore::CertificateStore(CertificateStore && other)
{
this->store = other.store;
other.store = nullptr;
}
CertificateStore::operator X509_STORE *()
{
return store;
}
void CertificateStore::add(X509 * cert)
{
int rc = X509_STORE_add_cert(store, cert);
if (1 != rc)
{
throw OpenSSLException("failed to add certificate to store");
}
}
void CertificateStore::loadFromFile(std::string const & filename)
{
int rc = X509_STORE_load_locations(store, filename.c_str(), nullptr);
if (1 != rc)
{
throw OpenSSLException("failed to load store from file");
}
}
}
| 1,368
|
C++
|
.cc
| 52
| 23.115385
| 73
| 0.704755
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| true
| false
|
1,535,420
|
cms.cc
|
falk-werner_zipsign/lib/openssl++/cms.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "openssl++/cms.hpp"
#include "openssl++/exception.hpp"
#include "openssl++/basic_io.hpp"
#include "base64/base64.h"
#include <iostream>
#include <vector>
namespace openssl
{
CMS CMS::fromBase64(std::string const & data)
{
size_t decoded_size = base64_decoded_size(data.c_str(), data.size());
std::vector<uint8_t> decoded(decoded_size);
base64_decode(data.c_str(), data.size(), decoded.data(), decoded.size());
auto bio = BasicIO::fromMemory(decoded.data(), decoded.size());
CMS_ContentInfo * cms = d2i_CMS_bio(bio, nullptr);
if (NULL == cms)
{
throw OpenSSLException("failed to read file");
}
return CMS(cms);
}
CMS CMS::sign(X509 * cert, EVP_PKEY * key, STACK_OF(X509) * certs, BIO * data, unsigned int flags)
{
CMS_ContentInfo * cms = CMS_sign(cert, key, certs, data, flags);
if (NULL == cms)
{
throw OpenSSLException("unable to sign file");
}
return CMS(cms);
}
CMS::CMS(CMS_ContentInfo * cms_)
: cms(cms_)
{
}
CMS::CMS(CMS && other)
{
this->cms = other.cms;
other.cms = nullptr;
}
CMS & CMS::operator=(CMS && other)
{
CMS_ContentInfo_free(this->cms);
this->cms = other.cms;
other.cms = nullptr;
return *this;
}
CMS::~CMS()
{
CMS_ContentInfo_free(cms);
}
void CMS::addSigner(X509 * cert, EVP_PKEY * key, EVP_MD const * md, unsigned int flags)
{
CMS_SignerInfo * info = CMS_add1_signer(cms, cert, key, md, flags);
if (nullptr == info)
{
throw OpenSSLException("failed to add signer");
}
/*
int rc = CMS_SignerInfo_sign(info);
if (1 != rc)
{
throw OpenSSLException("failed to sign");
}
*/
}
void CMS::final(BIO * data, BIO * dcont, unsigned int flags)
{
int rc = CMS_final(cms, data, dcont, flags);
if (1 != rc)
{
throw OpenSSLException("finalize CMS failed");
}
}
CMS::operator CMS_ContentInfo*()
{
return cms;
}
void CMS::saveToBIO(BIO * bio) const
{
if (!i2d_CMS_bio(bio, cms))
{
throw OpenSSLException("failed to convert CMS to DER format");
}
}
bool CMS::verify(STACK_OF(X509) * certs, X509_STORE * store, BIO * indata, BIO * outdata, unsigned int flags, bool is_verbose)
{
int rc = CMS_verify(cms, certs, store, indata, outdata, flags);
bool result = (1 == rc);
if ((!result) && (is_verbose))
{
OpenSSLException ex("verification failed");
std::cerr << "error: " << ex.what() << std::endl;
}
return result;
}
STACK_OF(X509) * CMS::getCerts()
{
return CMS_get1_certs(cms);
}
std::string CMS::toBase64() const
{
auto bio = BasicIO::fromMemory();
saveToBIO(bio);
BUF_MEM * buffer;
BIO_get_mem_ptr(bio, &buffer);
size_t encoded_size = base64_encoded_size(buffer->length);
std::vector<char> encoded(encoded_size);
base64_encode(reinterpret_cast<uint8_t *>(buffer->data), buffer->length, encoded.data(), encoded.size());
return std::string(encoded.data(), encoded.size());
}
std::string CMS::toString() const
{
auto bio = BasicIO::fromMemory();
CMS_ContentInfo_print_ctx(bio, cms, 0, NULL);
BUF_MEM * buffer;
BIO_get_mem_ptr(bio, &buffer);
return std::string(buffer->data, buffer->length);
}
}
| 3,434
|
C++
|
.cc
| 122
| 24.467213
| 126
| 0.649283
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,421
|
file.cc
|
falk-werner_zipsign/lib/zipsign/file.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "zipsign/file.hpp"
#include "zipsign/ftruncate.h"
#include "openssl++/exception.hpp"
#include <stdexcept>
#include <sys/stat.h>
#include <sys/types.h>
namespace zipsign
{
void File::remove(std::string const & filename)
{
unlink(filename.c_str());
}
File::File(std::string const & name, std::string const & mode)
{
file = fopen(name.c_str(), mode.c_str());
if (nullptr == file)
{
throw openssl::FileNotFoundException(name);
}
}
File::~File()
{
fclose(file);
}
void File::seek(long offset, int whence)
{
int rc = fseek(file, offset, whence);
if (0 != rc)
{
throw std::runtime_error("fseek failed");
}
}
long File::tell()
{
return ftell(file);
}
void File::write(void const * buffer, size_t count)
{
size_t written = fwrite(buffer, 1, count, file);
if (written != count)
{
throw std::runtime_error("write failed");
}
}
size_t File::read(void * buffer, size_t count, bool check)
{
size_t result = fread(buffer, 1, count, file);
if ((check) && (result != count))
{
throw std::runtime_error("read failed");
}
return result;
}
void File::truncate(long offset)
{
int rc = ftruncate(fileno(file), offset);
if (0 != rc)
{
throw std::runtime_error("truncate failed");
}
}
}
| 1,526
|
C++
|
.cc
| 65
| 20.169231
| 70
| 0.646653
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,422
|
zip.cc
|
falk-werner_zipsign/lib/zipsign/zip.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "zipsign/zip.hpp"
#include "zipsign/file.hpp"
#include <cstdio>
#include <cinttypes>
#include <algorithm>
#include <stdexcept>
#include <vector>
#define MAX_COMMENT_SIZE (64 * 1024)
#define MIN_EOCD_SIZE (22)
#define EOCD_COMMENT_POS (20)
#define MAX_EOCD_SIZE (MIN_EOCD_SIZE + MAX_COMMENT_SIZE)
namespace zipsign
{
Zip::Zip(std::string const & filename_)
: filename(filename_)
{
}
Zip::~Zip()
{
}
void Zip::setComment(std::string const & comment)
{
if (comment.size() > MAX_COMMENT_SIZE)
{
throw std::runtime_error("zip comment too long");
}
size_t commentStart = getCommentStart();
File file(filename, "rb+");
file.seek(commentStart);
uint8_t buffer[2];
buffer[0] = (uint8_t) (comment.size() & 0xff);
buffer[1] = (uint8_t) ((comment.size() >> 8) & 0xff);
file.write(buffer, 2);
file.write(comment.data(), comment.size());
file.truncate(file.tell());
}
std::string Zip::getComment()
{
size_t commentPos = getCommentStart();
File file(filename, "rb");
file.seek(commentPos);
uint8_t buffer[2];
file.read(buffer, 2);
size_t commentLength = (buffer[1] << 8) | buffer[0];
std::string comment;
if (commentLength > 0)
{
std::vector<char> commentBuffer(commentLength);
file.read(commentBuffer.data(), commentBuffer.size());
comment = std::string(commentBuffer.data(), commentBuffer.size());
}
return comment;
}
std::size_t Zip::getCommentStart()
{
File file(filename, "rb");
file.seek(0, SEEK_END);
size_t length = file.tell();
if (length < MIN_EOCD_SIZE)
{
throw std::runtime_error("invalid zip archive (too small)");
}
size_t buffer_size = std::min<size_t>(length, MAX_EOCD_SIZE);
std::vector<uint8_t> buffer(buffer_size);
size_t offset = length - buffer_size;
file.seek(offset);
file.read(buffer.data(),buffer.size());
bool found = false;
size_t pos = buffer_size - MIN_EOCD_SIZE;
while ((!found) && (pos > 0))
{
if ( (buffer[pos + 3] == 0x06)
&& (buffer[pos + 2] == 0x05)
&& (buffer[pos + 1] == 0x4b)
&& (buffer[pos + 0] == 0x50))
{
found = true;
}
else
{
pos--;
}
}
if (!found)
{
throw std::runtime_error("invalid zip archive: EOCD not found");
}
return offset + pos + EOCD_COMMENT_POS;
}
}
| 2,692
|
C++
|
.cc
| 93
| 23.784946
| 82
| 0.611435
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,423
|
informer.cc
|
falk-werner_zipsign/lib/zipsign/informer.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "zipsign/informer.hpp"
#include "zipsign/zip.hpp"
#include "zipsign/partial_input_file.hpp"
#include "zipsign/signature.hpp"
#include <stdexcept>
using openssl::CMS;
namespace zipsign
{
Informer::Informer()
{
}
Informer::~Informer()
{
}
void Informer::print(std::string const & filename, std::ostream & out)
{
Zip zip(filename);
auto commentSize = zip.getCommentStart();
PartialInputFile partialFile;
auto file = partialFile.open(filename, commentSize);
auto comment = zip.getComment();
if (0 != comment.find(ZIPSIGN_SIGNATURE_PREFIX))
{
throw std::runtime_error("missing signature");
}
auto signature = comment.substr(std::string(ZIPSIGN_SIGNATURE_PREFIX).size());
auto cms = CMS::fromBase64(signature);
out << cms.toString() << std::endl;
}
}
| 1,026
|
C++
|
.cc
| 33
| 28.030303
| 82
| 0.719101
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,424
|
partial_input_file.cc
|
falk-werner_zipsign/lib/zipsign/partial_input_file.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "zipsign/partial_input_file.hpp"
#include <stdexcept>
#include <cstdio>
#include <algorithm>
using openssl::BasicIO;
using openssl::OpenSSLException;
extern "C"
{
struct zipsign_PartialInputFile
{
FILE * file;
size_t limit;
size_t curPos;
};
static int zipsign_PartialInputFile_create(BIO * bio)
{
auto * data = new zipsign_PartialInputFile({nullptr, 0, 0});
BIO_set_data(bio, reinterpret_cast<void*>(data));
return 1;
}
static int zipsign_PartialInputFile_destory(BIO * bio)
{
auto * data = reinterpret_cast<zipsign_PartialInputFile*>(BIO_get_data(bio));
fclose(data->file);
delete data;
return 1;
}
static int zipsign_PartialInputFile_read(BIO * bio, char * buffer, int count)
{
auto * data = reinterpret_cast<zipsign_PartialInputFile*>(BIO_get_data(bio));
size_t n = std::min<size_t>(count, data->limit - data->curPos);
size_t bytesRead = fread(buffer, 1, n, data->file);
data->curPos += bytesRead;
return bytesRead;
}
}
namespace zipsign
{
PartialInputFile::PartialInputFile()
{
int type = BIO_get_new_index();
method = BIO_meth_new(type, "ZipSign_PartialInputFile");
if (nullptr == method)
{
throw OpenSSLException("failed to register BIO");
}
BIO_meth_set_create(method, zipsign_PartialInputFile_create);
BIO_meth_set_destroy(method, zipsign_PartialInputFile_destory);
BIO_meth_set_read(method, zipsign_PartialInputFile_read);
}
PartialInputFile::~PartialInputFile()
{
BIO_meth_free(method);
}
BasicIO PartialInputFile::open(std::string const & filename, std::size_t upperLimit)
{
FILE * file = fopen(filename.c_str(), "rb");
if (nullptr == file)
{
throw std::runtime_error("failed to open file");
}
BIO * bio = BIO_new(method);
if (nullptr == bio)
{
throw OpenSSLException("failed to create file;");
}
auto * data = reinterpret_cast<zipsign_PartialInputFile*>(BIO_get_data(bio));
data->file = file;
data->limit = upperLimit;
data->curPos= 0;
BIO_set_init(bio, 1);
return BasicIO(bio);
}
}
| 2,309
|
C++
|
.cc
| 77
| 26.38961
| 84
| 0.696833
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,425
|
signer.cc
|
falk-werner_zipsign/lib/zipsign/signer.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "zipsign/signer.hpp"
#include "zipsign/zip.hpp"
#include "zipsign/partial_input_file.hpp"
#include "zipsign/signature.hpp"
#include <stdexcept>
using openssl::PrivateKey;
using openssl::Certificate;
using openssl::CertificateStack;
using openssl::CMS;
namespace zipsign
{
Signer::Signer(std::string const & key_file, std::string const & cert_file)
: embedCerts(false)
{
addSigner(key_file, cert_file);
}
Signer::~Signer()
{
}
void Signer::addSigner(std::string const key_file, std::string const & cert_file)
{
auto key = PrivateKey::fromPEM(key_file);
auto cert = Certificate::fromPEM(cert_file);
keys.push_back(std::move(key));
certs.push_back(std::move(cert));
}
void Signer::addIntermediate(std::string const & filename)
{
intermediates.push_back(Certificate::fromPEM(filename));
}
void Signer::sign(std::string const & filename)
{
auto signature = createSignature(filename);
auto comment = ZIPSIGN_SIGNATURE_PREFIX + signature;
Zip zip(filename);
zip.setComment(comment);
}
void Signer::setEmbedCerts(bool value)
{
embedCerts = value;
}
std::string Signer::createSignature(std::string const & filename)
{
PartialInputFile partialFile;
Zip zip(filename);
CertificateStack intermetiate_certs;
for(auto & intermediate: intermediates)
{
intermetiate_certs.push(intermediate);
}
unsigned int flags = CMS_DETACHED | CMS_BINARY;
if (!embedCerts)
{
flags |= CMS_NOCERTS;
}
auto commentStart = zip.getCommentStart();
auto file = partialFile.open(filename, commentStart);
auto cms = CMS::sign(nullptr, nullptr, intermetiate_certs, file, flags | CMS_PARTIAL );
for (size_t i = 0; i < certs.size(); ++i)
{
cms.addSigner(certs[i], keys[i], nullptr, flags | CMS_PARTIAL);
}
cms.final(file, nullptr, flags);
auto signature = cms.toBase64();
return signature;
}
}
| 2,137
|
C++
|
.cc
| 70
| 27.071429
| 91
| 0.714635
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,426
|
verifier.cc
|
falk-werner_zipsign/lib/zipsign/verifier.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "zipsign/verifier.hpp"
#include "zipsign/zip.hpp"
#include "zipsign/partial_input_file.hpp"
#include "zipsign/signature.hpp"
#include <iostream>
#include <stdexcept>
using openssl::Certificate;
using openssl::CertificateStore;
using openssl::CertificateStack;
using openssl::CMS;
namespace zipsign
{
Verifier::Verifier(std::string const & cert_file)
{
addCertificate(cert_file);
}
Verifier::~Verifier()
{
}
void Verifier::addCertificate(std::string const & filename)
{
signers.push_back(Certificate::fromPEM(filename));
}
Verifier::Result Verifier::verify(
std::string const & filename,
std::string const & keyring_path,
bool is_verbose,
bool is_self_signed)
{
Result result = Bad;
try
{
Zip zip(filename);
auto commentSize = zip.getCommentStart();
PartialInputFile partialFile;
auto file = partialFile.open(filename, commentSize);
auto comment = zip.getComment();
if (0 != comment.find(ZIPSIGN_SIGNATURE_PREFIX))
{
result = BadMissingSignature;
throw std::runtime_error("missing signature");
}
auto signature = comment.substr(std::string(ZIPSIGN_SIGNATURE_PREFIX).size());
CertificateStore store;
if (!keyring_path.empty())
{
store.loadFromFile(keyring_path);
}
CertificateStack certs;
for(auto & cert: signers)
{
store.add(cert);
certs.push(cert);
}
auto cms = CMS::fromBase64(signature);
for (auto & cert: signers)
{
STACK_OF(X509) * untrusted = cms.getCerts();
if (!is_self_signed && !cert.verify(store, nullptr, untrusted))
{
sk_X509_pop_free(untrusted, X509_free);
result = BadInvalidCertificateChain;
throw std::runtime_error("signers certificate is not valid");
}
sk_X509_pop_free(untrusted, X509_free);
}
auto const chain_valid = cms.verify(certs, store, file, nullptr, CMS_DETACHED | CMS_BINARY | CMS_NO_SIGNER_CERT_VERIFY | CMS_NO_CONTENT_VERIFY, is_verbose);
if (!chain_valid)
{
result = BadInvalidCertificateChain;
throw std::runtime_error("certificate chain is not valid");
}
file = partialFile.open(filename, commentSize);
auto const valid = cms.verify(certs, store, file, nullptr, CMS_DETACHED | CMS_BINARY | CMS_NO_SIGNER_CERT_VERIFY, is_verbose);
result = valid ? Good : BadInvalidSignature;
}
catch(const std::exception& ex)
{
if (is_verbose)
{
std::cerr << "error: " << ex.what() << std::endl;
}
}
return result;
}
}
| 2,985
|
C++
|
.cc
| 89
| 26.550562
| 165
| 0.630865
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,427
|
argument.cc
|
falk-werner_zipsign/lib/cli/argument.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "cli/argument.hpp"
namespace cli
{
Argument::Argument(
char id_,
std::string const & name_,
ArgumentType type_,
std::string const & helpText_,
bool isRequired_,
std::string const & defaultValue_)
: id(id_)
, name(name_)
, type(type_)
, helpText(helpText_)
, isRequired(isRequired_)
, defaultValue(defaultValue_)
{
}
Argument::~Argument()
{
}
char Argument::getId() const
{
return id;
}
std::string const & Argument::getName() const
{
return name;
}
std::string const & Argument::getHelpText() const
{
return helpText;
}
std::string const & Argument::getDefaultValue() const
{
return defaultValue;
}
bool Argument::isOptional() const
{
return !isRequired;
}
bool Argument::isFlag() const
{
return (ArgumentType::FLAG == type);
}
bool Argument::hasDefaultValue() const
{
return !defaultValue.empty();
}
}
| 1,109
|
C++
|
.cc
| 53
| 18.169811
| 70
| 0.697406
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,428
|
default_verb.cc
|
falk-werner_zipsign/lib/cli/default_verb.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "cli/default_verb.hpp"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "cli/options.hpp"
#include "cli/default_arguments.hpp"
namespace cli
{
DefaultVerb::DefaultVerb(AppInfo & appInfo_, std::string const & name_, Command command_)
: appInfo(appInfo_)
, name(name_)
, command(command_)
{
}
DefaultVerb::~DefaultVerb()
{
}
std::string const & DefaultVerb::getName() const
{
return name;
}
std::string const & DefaultVerb::getHelpText() const
{
return helpText;
}
Verb & DefaultVerb::setHelpText(std::string const & helpText_)
{
helpText = helpText_;
return *this;
}
Verb & DefaultVerb::addFlag(char id, std::string const & name, std::string const & description)
{
args.push_back(Argument(id, name, ArgumentType::FLAG, description, false, ""));
return *this;
}
Verb & DefaultVerb::addArg(
char id,
std::string const & name,
std::string const & description,
bool isRequired,
std::string const & defaultValue)
{
(void) defaultValue;
args.push_back(Argument(id, name, ArgumentType::STRING, description, isRequired, ""));
return *this;
}
Verb & DefaultVerb::addList(
char id,
std::string const & name,
std::string const & description,
bool isRequired,
std::string const & defaultValue)
{
(void) defaultValue;
args.push_back(Argument(id, name, ArgumentType::LIST, description, isRequired, ""));
return *this;
}
int DefaultVerb::run(int argc, char* argv[]) const
{
DefaultArguments arguments;
for(auto & arg: args)
{
if (arg.hasDefaultValue())
{
arguments.set(arg.getId(), arg.getDefaultValue());
}
}
Options opts(args);
int result = EXIT_SUCCESS;
bool is_finished = false;
bool print_usage = false;
optind = 0;
while (!is_finished)
{
opterr = 0;
int option_index = 0;
int const c = getopt_long(argc, argv, opts.shortOpts(), opts.longOpts(), &option_index);
switch (c)
{
case -1:
is_finished = true;
break;
case 'h':
is_finished = true;
print_usage = true;
break;
case '?':
std::cout << "error: unrecognized argument" << std::endl;
is_finished = true;
print_usage = true;
result = EXIT_FAILURE;
break;
default:
arguments.set(c, (nullptr != optarg) ? optarg : "");
break;
}
}
if ((EXIT_SUCCESS == result) && (!print_usage))
{
for (auto & arg: args)
{
if ((!arg.isOptional()) && (!arguments.contains(arg.getId())))
{
std::cerr << "error: missing required argument: -" << arg.getId() << std::endl;
print_usage = true;
result = EXIT_FAILURE;
}
}
}
if (!print_usage)
{
result = command(arguments);
}
else
{
printUsage();
}
return result;
}
void DefaultVerb::printUsage() const
{
std::cout
<< appInfo.getName() << ", Copyright (c) " << appInfo.getCopyright() << std::endl
<< appInfo.getDescription() << std::endl
<< std::endl
;
std::cout << name << ": " << helpText << std::endl << std::endl;
std::cout
<< "Usage:" << std::endl
<< '\t' << appInfo.getName() << ' ' << name
;
for (auto const & arg: args)
{
std::cout << ' ';
if (arg.isOptional())
{
std::cout << '[';
}
std::cout << '-' << arg.getId();
if (!arg.isFlag())
{
std::cout << " <value>";
}
if (arg.isOptional())
{
std::cout << ']';
}
}
std::cout
<< " | -h" << std::endl
<< std::endl
<< "Arguments:" << std::endl;
for (auto const & arg: args)
{
std::cout
<< "\t-" << arg.getId()
<< ", --" << std::left << std::setw(20) << arg.getName()
<< "\t";
if (!arg.isOptional())
{
std::cout << "Required. ";
}
std::cout << arg.getHelpText();
if (arg.hasDefaultValue())
{
std::cout << " (default: " << arg.getDefaultValue() << ')';
}
std::cout << std::endl;
}
std::cout
<< "\t-h, --" << std::left << std::setw(20) << "help" << "\tPrint usage." << std::endl
<< std::endl;
}
}
| 4,831
|
C++
|
.cc
| 177
| 20.118644
| 96
| 0.528466
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,429
|
default_arguments.cc
|
falk-werner_zipsign/lib/cli/default_arguments.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "cli/default_arguments.hpp"
#include <stdexcept>
namespace cli
{
DefaultArguments::DefaultArguments()
{
}
DefaultArguments::~DefaultArguments()
{
}
bool DefaultArguments::contains(char id) const
{
return values.end() != values.find(id);
}
std::string const & DefaultArguments::get(char id) const
{
auto const & list = getList(id);
return list[0];
}
std::vector<std::string> const & DefaultArguments::getList(char id) const
{
auto it = values.find(id);
if (values.end() == it)
{
throw std::out_of_range("id not found : " + std::string(1, id));
}
return it->second;
}
void DefaultArguments::set(char id, std::string const & value)
{
auto & list = values[id];
list.push_back(value);
}
}
| 960
|
C++
|
.cc
| 37
| 23.27027
| 73
| 0.694841
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,430
|
app.cc
|
falk-werner_zipsign/lib/cli/app.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "cli/app.hpp"
#include "cli/app_info.hpp"
#include "cli/default_verb.hpp"
#include <iostream>
#include <iomanip>
#include <cstdlib>
namespace cli
{
class App::Private: public AppInfo
{
public:
Private(std::string const & name_);
~Private() override;
std::string const & getName() const override;
std::string const & getDescription() const override;
std::string const & getCopyright() const override;
int run(int argc, char* argv[]) const;
Verb & add(std::string const & name, Command command);
void setCopyright(std::string const & value);
void setDescription(std::string const & value);
void setAdditionalInfo(std::string const & value);
private:
void printUsage() const;
DefaultVerb const * getVerb(std::string const & name) const;
std::string name;
std::string copyright;
std::string description;
std::string additionalInfo;
std::vector<DefaultVerb> verbs;
};
App::App(std::string const & name_)
: d(new App::Private(name_))
{
}
App::~App()
{
delete d;
}
int App::run(int argc, char* argv[]) const
{
return d->run(argc, argv);
}
Verb & App::add(std::string const & name, Command command)
{
return d->add(name, command);
}
App & App::setCopyright(std::string const & value)
{
d->setCopyright(value);
return *this;
}
App & App::setDescription(std::string const & value)
{
d->setDescription(value);
return *this;
}
App & App::setAdditionalInfo(std::string const & value)
{
d->setAdditionalInfo(value);
return *this;
}
App::Private::Private(std::string const & name_)
: name(name_)
{
}
App::Private::~Private()
{
}
std::string const & App::Private::getName() const
{
return name;
}
std::string const & App::Private::getDescription() const
{
return description;
}
std::string const & App::Private::getCopyright() const
{
return copyright;
}
int App::Private::run(int argc, char* argv[]) const
{
int exitCode = EXIT_FAILURE;
if (argc > 1)
{
std::string verbName = argv[1];
DefaultVerb const * verb = getVerb(verbName);
if (nullptr != verb)
{
exitCode = verb->run(argc - 1, &argv[1]);
}
else if ((verbName == "-h") || (verbName == "--help"))
{
printUsage();
exitCode = EXIT_SUCCESS;
}
else
{
std::cerr << "error: unknwon verb: " << verbName << std::endl;
printUsage();
}
}
else
{
std::cerr << "error: missing verb" << std::endl;
printUsage();
}
return exitCode;
}
Verb & App::Private::add(std::string const & verb, Command command)
{
verbs.push_back(DefaultVerb(*this, verb, command));
return verbs[verbs.size() - 1];
}
void App::Private::setCopyright(std::string const & value)
{
copyright = value;
}
void App::Private::setDescription(std::string const & value)
{
description = value;
}
void App::Private::setAdditionalInfo(std::string const & value)
{
additionalInfo = value;
}
DefaultVerb const * App::Private::getVerb(std::string const & name) const
{
for(auto & verb: verbs)
{
if (name == verb.getName())
{
return &verb;
}
}
return nullptr;
}
void App::Private::printUsage(void) const
{
std::cout
<< name << ", Copyright (c) " << copyright << std::endl
<< description << std::endl
<< std::endl
<< "Usage:" << std::endl
<< '\t' << name << ' '
;
for (auto const & verb: verbs)
{
std::cout << verb.getName() << " <args...> | ";
}
std::cout
<< "-h" << std::endl
<< std::endl
<< "Verbs:" << std::endl;
for (auto const & verb: verbs)
{
std::cout
<< "\t" << std::left << std::setw(20) << verb.getName()
<< "\t" << verb.getHelpText()
<< std::endl;
}
std::cout
<< "\t" << std::left << std::setw(20) << "-h, --help" << "\tPrint usage." << std::endl
<< std::endl;
std::cout << additionalInfo;
}
}
| 4,333
|
C++
|
.cc
| 170
| 20.764706
| 94
| 0.600485
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,431
|
options.cc
|
falk-werner_zipsign/lib/cli/options.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "cli/options.hpp"
namespace
{
void setOption(
option & option,
char id,
char const * name,
int hasArg)
{
option.name = name;
option.has_arg = hasArg;
option.flag = nullptr;
option.val = static_cast<int>(id);
}
void setOption(
option & option,
cli::Argument const & arg
)
{
option.name = arg.getName().c_str();
option.has_arg = arg.isFlag() ? no_argument : required_argument;
option.flag = nullptr;
option.val = static_cast<int>(arg.getId());
}
}
namespace cli
{
Options::Options(std::vector<Argument> const & args)
{
size_t const length = args.size();
long_opts = new option[length + 2];
short_opts = "";
for(size_t i = 0; i < length; i++)
{
setOption(long_opts[i], args[i]);
short_opts += args[i].getId();
if (!args[i].isFlag())
{
short_opts += ':';
}
}
short_opts += 'h';
setOption(long_opts[length], 'h', "help", no_argument);
setOption(long_opts[length + 1], '\0', nullptr, 0);
}
Options::~Options()
{
delete[] long_opts;
}
char const * Options::shortOpts() const
{
return short_opts.c_str();
}
option const * Options::longOpts() const
{
return long_opts;
}
}
| 1,464
|
C++
|
.cc
| 61
| 19.934426
| 72
| 0.616547
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| true
| false
|
1,535,432
|
testutils.cc
|
falk-werner_zipsign/test/testutils.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "testutils.hpp"
#include <fstream>
namespace testutils
{
void copy_file(std::string const & from, std::string const & to)
{
std::ifstream source_file(from, std::ios::binary);
std::ofstream target_file(to, std::ios::binary);
target_file << source_file.rdbuf();
}
}
| 496
|
C++
|
.cc
| 14
| 33.071429
| 70
| 0.712788
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,433
|
test_partial_input_file.cc
|
falk-werner_zipsign/test/test_partial_input_file.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <gtest/gtest.h>
#include "zipsign/partial_input_file.hpp"
using zipsign::PartialInputFile;
TEST(zipsign, dummy)
{
PartialInputFile partialFile;
auto file = partialFile.open("message.txt", 2);
BIO * in = file;
char buffer[100];
int i = BIO_read(in, buffer, 100);
ASSERT_EQ(2, i);
ASSERT_EQ('4', buffer[0]);
ASSERT_EQ('2', buffer[1]);
}
| 587
|
C++
|
.cc
| 17
| 31.235294
| 70
| 0.686726
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,434
|
test_file.cc
|
falk-werner_zipsign/test/test_file.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <gtest/gtest.h>
#include "zipsign/file.hpp"
using zipsign::File;
TEST(File, FailedToOpenNonExistingFile)
{
ASSERT_THROW({
File file("_non_existing.file", "rb");
}, std::exception);
}
TEST(File, OpenReadOnly)
{
File file("test.zip", "rb");
}
TEST(File, SeekAndTell)
{
File file("test.zip", "rb");
file.seek(0, SEEK_END);
auto length = file.tell();
ASSERT_LT(0, length);
}
TEST(File, FailedToSeekBeforeBegin)
{
File file("test.zip", "rb");
ASSERT_THROW({
file.seek(-1, SEEK_SET);
}, std::exception);
}
TEST(File, WriteAndRead)
{
{
File file("new.file", "wb");
uint8_t data[] = {1, 2, 3};
file.write(data, 3);
}
{
File file("new.file", "rb");
uint8_t data[3];
auto length = file.read(data, 3);
ASSERT_EQ(3, length);
ASSERT_EQ(1, data[0]);
ASSERT_EQ(2, data[1]);
ASSERT_EQ(3, data[2]);
}
File::remove("new.file");
}
TEST(File, FailedToWriteReadonlyFile)
{
File file("test.zip", "rb");
ASSERT_THROW({
uint8_t data[] = { 0x42 };
file.write(data, 1);
}, std::exception);
}
TEST(File, FailedToReadOutOfBounds)
{
{
File file("new.file", "wb");
uint8_t data[] = {1, 2, 3};
file.write(data, 3);
}
{
File file("new.file", "rb");
ASSERT_THROW({
uint8_t data[3];
file.read(data, 4);
}, std::exception);
}
File::remove("new.file");
}
TEST(File, ReadUnceckedOutOfBound)
{
{
File file("new.file", "wb");
uint8_t data[] = {1, 2, 3};
file.write(data, 3);
}
{
File file("new.file", "rb");
uint8_t data[3];
auto length = file.read(data, 4, false);
ASSERT_EQ(3, length);
ASSERT_EQ(1, data[0]);
ASSERT_EQ(2, data[1]);
ASSERT_EQ(3, data[2]);
}
File::remove("new.file");
}
TEST(File, Truncate)
{
{
File file("new.file", "wb");
uint8_t data[] = {1, 2, 3};
file.write(data, 3);
file.seek(1, SEEK_SET);
file.truncate(1);
}
{
File file("new.file", "rb");
uint8_t data[3];
auto length = file.read(data, 1);
ASSERT_EQ(1, length);
ASSERT_EQ(1, data[0]);
}
File::remove("new.file");
}
TEST(File, FailToTruncateReadonlyFile)
{
File file("test.zip", "rb");
ASSERT_THROW({
file.truncate(1);
}, std::exception);
}
| 2,730
|
C++
|
.cc
| 115
| 18.043478
| 70
| 0.549476
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,435
|
test_zip.cc
|
falk-werner_zipsign/test/test_zip.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <gtest/gtest.h>
#include "zipsign/zip.hpp"
using zipsign::Zip;
TEST(zipsign, getzipcommentpos)
{
Zip zip("test.zip");
ASSERT_EQ(50707, zip.getCommentStart());
}
TEST(zipsign, getzipcomment)
{
Zip zip("test.zip");
ASSERT_STREQ("brummni", zip.getComment().c_str());
}
| 502
|
C++
|
.cc
| 16
| 28.9375
| 70
| 0.711019
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,436
|
test_informer.cc
|
falk-werner_zipsign/test/test_informer.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "testutils.hpp"
#include <zipsign/zipsign.hpp>
#include <zipsign/file.hpp>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
using zipsign::Signer;
using zipsign::Informer;
using zipsign::File;
using testing::HasSubstr;
using testing::Not;
#define TEST_ARCHIVE ("informer.zip")
class InformerTest: public ::testing::Test
{
protected:
void SetUp() override
{
testutils::copy_file("test.zip", TEST_ARCHIVE);
}
void TearDown() override
{
File::remove(TEST_ARCHIVE);
}
};
TEST_F(InformerTest, WithoutEmbeddedSignerCertificate)
{
std::string key_file = "certs/alice.key";
std::string cert_file = "certs/alice.crt";
Signer signer(key_file, cert_file);
signer.sign(TEST_ARCHIVE);
Informer informer;
std::stringstream out;
informer.print(TEST_ARCHIVE, out);
auto info = out.str();
ASSERT_THAT(info.c_str(), HasSubstr("Signing CA"));
ASSERT_THAT(info.c_str(), Not(HasSubstr("Alice")));
}
TEST_F(InformerTest, WithEmbeddedSignerCertificate)
{
std::string key_file = "certs/alice.key";
std::string cert_file = "certs/alice.crt";
Signer signer(key_file, cert_file);
signer.setEmbedCerts(true);
signer.sign(TEST_ARCHIVE);
Informer informer;
std::stringstream out;
informer.print(TEST_ARCHIVE, out);
auto info = out.str();
ASSERT_THAT(info.c_str(), HasSubstr("Signing CA"));
ASSERT_THAT(info.c_str(), HasSubstr("Alice"));
}
TEST_F(InformerTest, Fail_UnsignedArchive)
{
Informer informer;
ASSERT_THROW({
informer.print(TEST_ARCHIVE);
}, std::exception);
}
| 1,823
|
C++
|
.cc
| 60
| 26.566667
| 70
| 0.701149
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,437
|
test_sign_and_verify.cc
|
falk-werner_zipsign/test/test_sign_and_verify.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "testutils.hpp"
#include <zipsign/zipsign.hpp>
#include <zipsign/file.hpp>
#include <gtest/gtest.h>
using zipsign::Signer;
using zipsign::Verifier;
using zipsign::File;
#define TEST_ARCHIVE ("sign_and_verify.zip")
class SignAndVerifyTest: public ::testing::Test
{
protected:
void SetUp() override
{
testutils::copy_file("test.zip", TEST_ARCHIVE);
}
void TearDown() override
{
File::remove(TEST_ARCHIVE);
}
};
TEST_F(SignAndVerifyTest, SelfSigned)
{
std::string key_file = "self-signed/key.pem";
std::string cert_file = "self-signed/cert.pem";
Verifier verifier(cert_file);
ASSERT_EQ(Verifier::BadMissingSignature, verifier.verify(TEST_ARCHIVE, ""));
Signer signer(key_file, cert_file);
signer.sign(TEST_ARCHIVE);
ASSERT_EQ(Verifier::Good, verifier.verify(TEST_ARCHIVE, "", false, true));
}
TEST_F(SignAndVerifyTest, PkiSigned)
{
std::string key_file = "certs/alice.key";
std::string cert_file = "certs/alice.crt";
std::string keyring = "keyring.pem";
Verifier verifier(cert_file);
ASSERT_EQ(Verifier::BadMissingSignature, verifier.verify(TEST_ARCHIVE, ""));
Signer signer(key_file, cert_file);
signer.sign(TEST_ARCHIVE);
ASSERT_EQ(Verifier::Good, verifier.verify(TEST_ARCHIVE, keyring));
}
TEST_F(SignAndVerifyTest, UseIntermediateCert)
{
std::string key_file = "certs/alice.key";
std::string cert_file = "certs/alice.crt";
std::string keyring = "ca/root-ca.crt";
std::string intermediate = "ca/signing-ca.crt";
Signer signer(key_file, cert_file);
signer.addIntermediate(intermediate);
signer.sign(TEST_ARCHIVE);
Verifier verifier(cert_file);
ASSERT_EQ(Verifier::Good, verifier.verify(TEST_ARCHIVE, keyring));
}
TEST_F(SignAndVerifyTest, Fail_ValidateWithoutIntermediateCert)
{
std::string key_file = "certs/alice.key";
std::string cert_file = "certs/alice.crt";
std::string keyring = "ca/root-ca.crt";
Signer signer(key_file, cert_file);
signer.sign(TEST_ARCHIVE);
Verifier verifier(cert_file);
ASSERT_EQ(Verifier::BadInvalidCertificateChain, verifier.verify(TEST_ARCHIVE, keyring));
}
TEST_F(SignAndVerifyTest, Multisign)
{
std::string self_key = "self-signed/key.pem";
std::string self_crt = "self-signed/cert.pem";
std::string alice_key = "certs/alice.key";
std::string alice_crt = "certs/alice.crt";
std::string keyring = "keyring.pem";
Signer signer(self_key, self_crt);
signer.addSigner(alice_key, alice_crt);
signer.setEmbedCerts();
signer.sign(TEST_ARCHIVE);
Verifier self_verifier(self_crt);
ASSERT_EQ(Verifier::Good, self_verifier.verify(TEST_ARCHIVE, ""));
Verifier alice_verifier(alice_crt);
ASSERT_EQ(Verifier::Good, alice_verifier.verify(TEST_ARCHIVE, keyring));
Verifier all_verifier(alice_crt);
all_verifier.addCertificate(self_crt);
ASSERT_EQ(Verifier::Good, all_verifier.verify(TEST_ARCHIVE, keyring));
}
| 3,193
|
C++
|
.cc
| 85
| 33.552941
| 92
| 0.714796
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,438
|
test_base64.cc
|
falk-werner_zipsign/test/base64/test_base64.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <gtest/gtest.h>
#include "base64/base64.h"
TEST(Base64, EncodedSize)
{
ASSERT_EQ(4, base64_encoded_size(1));
ASSERT_EQ(4, base64_encoded_size(2));
ASSERT_EQ(4, base64_encoded_size(3));
ASSERT_EQ(8, base64_encoded_size(4));
ASSERT_EQ(8, base64_encoded_size(5));
ASSERT_EQ(8, base64_encoded_size(6));
ASSERT_EQ(120, base64_encoded_size(90));
}
TEST(Base64, Encode)
{
char buffer[42];
std::string in = "Hello";
size_t length = base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 42);
ASSERT_EQ(8, length);
ASSERT_STREQ("SGVsbG8=", buffer);
in = "Hello\n";
length = base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 42);
ASSERT_EQ(8, length);
ASSERT_STREQ("SGVsbG8K", buffer);
in = "Blue";
length = base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 42);
ASSERT_EQ(8, length);
ASSERT_STREQ("Qmx1ZQ==", buffer);
}
TEST(Base64, FailedToEncodeBufferTooSmall)
{
char buffer[1];
std::string in = "Hello";
size_t length = base64_encode((uint8_t const*) in.c_str(), in.size(), buffer, 1);
ASSERT_EQ(0, length);
}
TEST(Base64, DecodedSize)
{
std::string in = "SGVsbG8="; // Hello
size_t length = base64_decoded_size(in.c_str(), in.size());
ASSERT_EQ(5, length);
in = "SGVsbG8K"; // Hello\n
length = base64_decoded_size(in.c_str(), in.size());
ASSERT_EQ(6, length);
in = "Qmx1ZQ=="; // Blue
length = base64_decoded_size(in.c_str(), in.size());
ASSERT_EQ(4, length);
}
TEST(Base64, IsValid)
{
std::string in = "SGVsbG8="; // Hello
ASSERT_TRUE(base64_isvalid(in.c_str(), in.size()));
in = "SGVsbG8K"; // Hello\n
ASSERT_TRUE(base64_isvalid(in.c_str(), in.size()));
in = "Qmx1ZQ=="; // Blue
ASSERT_TRUE(base64_isvalid(in.c_str(), in.size()));
in = "Qmx1ZQ=a";
ASSERT_FALSE(base64_isvalid(in.c_str(), in.size()));
in = "Qmx1ZQ";
ASSERT_FALSE(base64_isvalid(in.c_str(), in.size()));
in = "Qmx1ZQ=";
ASSERT_FALSE(base64_isvalid(in.c_str(), in.size()));
in = "Qmx1Z===";
ASSERT_FALSE(base64_isvalid(in.c_str(), in.size()));
in = "Qmx1ZQ?=";
ASSERT_FALSE(base64_isvalid(in.c_str(), in.size()));
in = "Qm?1ZQ==";
ASSERT_FALSE(base64_isvalid(in.c_str(), in.size()));
}
TEST(Base64, Decode)
{
char buffer[42];
std::string in = "SGVsbG8="; // Hello
size_t length = base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 42);
ASSERT_EQ(5, length);
buffer[length] = '\0';
ASSERT_STREQ("Hello", buffer);
in = "SGVsbG8K"; // Hello\n
length = base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 42);
ASSERT_EQ(6, length);
buffer[length] = '\0';
ASSERT_STREQ("Hello\n", buffer);
in = "Qmx1ZQ=="; // Blue
length = base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 42);
ASSERT_EQ(4, length);
buffer[length] = '\0';
ASSERT_STREQ("Blue", buffer);
}
TEST(Base64, FailToDecodeBufferTooSmall)
{
char buffer[1];
std::string in = "SGVsbG8="; // Hello
size_t length = base64_decode(in.c_str(), in.size(), (uint8_t*) buffer, 1);
ASSERT_EQ(0, length);
}
| 3,412
|
C++
|
.cc
| 97
| 30.927835
| 86
| 0.614425
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,439
|
test_certificate_stack.cc
|
falk-werner_zipsign/test/openssl/test_certificate_stack.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <gtest/gtest.h>
#include <openssl++/certificate_stack.hpp>
#include <openssl++/certificate.hpp>
#include <openssl++/exception.hpp>
using openssl::CertificateStack;
using openssl::Certificate;
using openssl::OpenSSLException;
TEST(CertificateStack, Constructible)
{
CertificateStack stack;
}
TEST(CertificateStack, AddCertificate)
{
Certificate cert = Certificate::fromPEM("certs/alice.crt");
CertificateStack stack;
stack.push(cert);
}
TEST(CertificateStack, CastToX509Stack)
{
CertificateStack stack;
STACK_OF(X509) * certs = static_cast<STACK_OF(X509) *>(stack);
(void) certs;
}
| 830
|
C++
|
.cc
| 26
| 29.538462
| 70
| 0.758145
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,440
|
test_certificate.cc
|
falk-werner_zipsign/test/openssl/test_certificate.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <gtest/gtest.h>
#include <openssl++/certificate.hpp>
#include <openssl++/exception.hpp>
using openssl::Certificate;
using openssl::OpenSSLException;
TEST(Certificate, FromPEM)
{
Certificate::fromPEM("certs/alice.crt");
}
TEST(Certificate, Fail_FromPEMInvalidFile)
{
ASSERT_THROW({
Certificate::fromPEM("certs/alice.csr");
}, OpenSSLException);
}
TEST(Certificate, MoveConstructible)
{
Certificate cert = Certificate::fromPEM("certs/alice.crt");
Certificate other = std::move(cert);
ASSERT_EQ(nullptr, static_cast<X509*>(cert));
ASSERT_NE(nullptr, static_cast<X509*>(other));
}
TEST(Certificate, MoveAssignable)
{
Certificate cert = Certificate::fromPEM("certs/alice.crt");
Certificate other = Certificate::fromPEM("certs/alice.crt");
other = std::move(cert);
ASSERT_EQ(nullptr, static_cast<X509*>(cert));
ASSERT_NE(nullptr, static_cast<X509*>(other));
}
| 1,134
|
C++
|
.cc
| 33
| 31.333333
| 70
| 0.727106
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,441
|
test_certificate_store.cc
|
falk-werner_zipsign/test/openssl/test_certificate_store.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <gtest/gtest.h>
#include <openssl++/certificate_store.hpp>
#include <openssl++/certificate.hpp>
#include <openssl++/exception.hpp>
using openssl::CertificateStore;
using openssl::Certificate;
using openssl::OpenSSLException;
TEST(CertifiaceStore, Construct)
{
CertificateStore store;
}
TEST(CertificateStore, AddCert)
{
Certificate cert = Certificate::fromPEM("certs/alice.crt");
CertificateStore store;
store.add(cert);
}
TEST(CertificateStore, Fail_AddCertInvalid)
{
CertificateStore store;
ASSERT_THROW({
store.add(nullptr);
}, OpenSSLException);
}
TEST(CertificateStore, LoadFromFile)
{
CertificateStore store;
store.loadFromFile("keyring.pem");
}
TEST(CertificateStore, Fail_LoadFromFile_InvalidFile)
{
CertificateStore store;
ASSERT_THROW({
store.loadFromFile("acerts/alice.csr");
}, OpenSSLException);
}
TEST(CertificateStore, MoveConstructible)
{
CertificateStore store;
CertificateStore other = std::move(store);
ASSERT_EQ(nullptr, static_cast<X509_STORE*>(store));
ASSERT_NE(nullptr, static_cast<X509_STORE*>(other));
}
TEST(CertificateStore, MoveAssignable)
{
CertificateStore store;
CertificateStore other;
other = std::move(store);
ASSERT_EQ(nullptr, static_cast<X509_STORE*>(store));
ASSERT_NE(nullptr, static_cast<X509_STORE*>(other));
}
| 1,582
|
C++
|
.cc
| 54
| 26.148148
| 70
| 0.747688
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,442
|
test_private_key.cc
|
falk-werner_zipsign/test/openssl/test_private_key.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <gtest/gtest.h>
#include <openssl++/private_key.hpp>
#include <openssl++/exception.hpp>
using openssl::PrivateKey;
using openssl::OpenSSLException;
TEST(PrivateKey, FromPEM)
{
PrivateKey::fromPEM("certs/alice.key");
}
TEST(PrivateKey, Fail_FromPEM)
{
ASSERT_THROW({
PrivateKey::fromPEM("certs/alice.csr");
}, OpenSSLException);
}
TEST(PrivateKey, MoveConstructible)
{
PrivateKey key = PrivateKey::fromPEM("certs/alice.key");
PrivateKey other = std::move(key);
ASSERT_EQ(nullptr, static_cast<EVP_PKEY*>(key));
ASSERT_NE(nullptr, static_cast<EVP_PKEY*>(other));
}
TEST(PrivateKey, MoveAssingable)
{
PrivateKey key = PrivateKey::fromPEM("certs/alice.key");
PrivateKey other = PrivateKey::fromPEM("certs/alice.key");
other = std::move(key);
ASSERT_EQ(nullptr, static_cast<EVP_PKEY*>(key));
ASSERT_NE(nullptr, static_cast<EVP_PKEY*>(other));
}
| 1,118
|
C++
|
.cc
| 33
| 30.878788
| 70
| 0.718663
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,443
|
test_basic_io.cc
|
falk-werner_zipsign/test/openssl/test_basic_io.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <gtest/gtest.h>
#include <openssl++/basic_io.hpp>
#include <openssl++/exception.hpp>
using openssl::BasicIO;
TEST(BasicIO, OpenInputFile)
{
BasicIO file = BasicIO::openInputFile("test.zip");
}
TEST(BasicIO, Fail_OpenInputFile_NotExists)
{
ASSERT_THROW({
BasicIO::openInputFile("non-existing-test.zip");
},
openssl::FileNotFoundException);
}
TEST(BasicIO, FromMemory)
{
BasicIO bio = BasicIO::fromMemory();
}
TEST(BasicIO, MoveConstuctible)
{
BasicIO bio = BasicIO::fromMemory();
BasicIO x = std::move(bio);
ASSERT_EQ(nullptr, static_cast<BIO*>(bio));
ASSERT_NE(nullptr, static_cast<BIO*>(x));
}
TEST(BasicIO, MoveAssignable)
{
BasicIO bio = BasicIO::fromMemory();
BasicIO x = BasicIO::fromMemory();
x = std::move(bio);
ASSERT_EQ(nullptr, static_cast<BIO*>(bio));
ASSERT_NE(nullptr, static_cast<BIO*>(x));
}
| 1,094
|
C++
|
.cc
| 37
| 26.567568
| 70
| 0.702574
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,444
|
test_cms.cc
|
falk-werner_zipsign/test/openssl/test_cms.cc
|
#include <gtest/gtest.h>
#include <openssl++/openssl++.hpp>
#include <base64/base64.h>
using openssl::CMS;
using openssl::PrivateKey;
using openssl::Certificate;
using openssl::CertificateStore;
using openssl::CertificateStack;
using openssl::BasicIO;
using openssl::OpenSSLException;
TEST(CMS, SignAndVerify)
{
PrivateKey key = PrivateKey::fromPEM("self-signed/key.pem");
Certificate cert = Certificate::fromPEM("self-signed/cert.pem");
BasicIO file = BasicIO::openInputFile("test.zip");
CMS cms = CMS::sign(cert, key, nullptr, file, CMS_DETACHED | CMS_NOCERTS | CMS_BINARY);
CertificateStore store;
store.add(cert);
CertificateStack certs;
certs.push(cert);
file = BasicIO::openInputFile("test.zip");
bool isValid = cms.verify(certs, store, file, nullptr, CMS_DETACHED | CMS_BINARY, false);
ASSERT_TRUE(isValid);
}
TEST(CMS, FailedToSign)
{
ASSERT_THROW({
CMS::sign(nullptr, nullptr, nullptr, nullptr, CMS_DETACHED | CMS_NOCERTS | CMS_BINARY);
}, OpenSSLException);
}
TEST(CMS, FailedToVerify)
{
PrivateKey key = PrivateKey::fromPEM("certs/alice.key");
Certificate cert = Certificate::fromPEM("certs/alice.crt");
BasicIO file = BasicIO::openInputFile("test.zip");
CMS cms = CMS::sign(cert, key, nullptr, file, CMS_DETACHED | CMS_NOCERTS | CMS_BINARY);
cert = Certificate::fromPEM("self-signed/cert.pem");
CertificateStore store;
store.add(cert);
CertificateStack certs;
certs.push(cert);
file = BasicIO::openInputFile("test.zip");
bool isValid = cms.verify(certs, store, file, nullptr, CMS_DETACHED | CMS_BINARY, true);
ASSERT_FALSE(isValid);
}
TEST(CMS, SaveToBIO)
{
PrivateKey key = PrivateKey::fromPEM("certs/alice.key");
Certificate cert = Certificate::fromPEM("certs/alice.crt");
BasicIO file = BasicIO::openInputFile("test.zip");
CMS cms = CMS::sign(cert, key, nullptr, file, CMS_DETACHED | CMS_NOCERTS | CMS_BINARY);
BasicIO bio = BasicIO::fromMemory();
cms.saveToBIO(bio);
}
TEST(CMS, FailedToSaveToBIO)
{
PrivateKey key = PrivateKey::fromPEM("certs/alice.key");
Certificate cert = Certificate::fromPEM("certs/alice.crt");
BasicIO file = BasicIO::openInputFile("test.zip");
CMS cms = CMS::sign(cert, key, nullptr, file, CMS_DETACHED | CMS_NOCERTS | CMS_BINARY);
ASSERT_THROW({
cms.saveToBIO(nullptr);
}, OpenSSLException);
}
TEST(CMS, FromBase64)
{
PrivateKey key = PrivateKey::fromPEM("certs/alice.key");
Certificate cert = Certificate::fromPEM("certs/alice.crt");
BasicIO file = BasicIO::openInputFile("test.zip");
CMS cms = CMS::sign(cert, key, nullptr, file, CMS_DETACHED | CMS_NOCERTS | CMS_BINARY);
std::string data = cms.toBase64();
cms.fromBase64(data);
}
TEST(CMS, FailFromBase64InvalidContent)
{
char text[] = "Hugo";
size_t text_size = sizeof(text) - 1;
char buffer[80];
size_t length = base64_encode((uint8_t const*) text, text_size, buffer, 80);
std::string data(buffer, length);
ASSERT_THROW({
CMS::fromBase64(data);
}, OpenSSLException);
}
TEST(CMS, CastToContentInfo)
{
PrivateKey key = PrivateKey::fromPEM("certs/alice.key");
Certificate cert = Certificate::fromPEM("certs/alice.crt");
BasicIO file = BasicIO::openInputFile("test.zip");
CMS cms = CMS::sign(cert, key, nullptr, file, CMS_DETACHED | CMS_NOCERTS | CMS_BINARY);
CMS_ContentInfo * contentInfo = static_cast<CMS_ContentInfo*>(cms);
(void) contentInfo;
}
TEST(CMS, MoveConstructible)
{
PrivateKey key = PrivateKey::fromPEM("certs/alice.key");
Certificate cert = Certificate::fromPEM("certs/alice.crt");
BasicIO file = BasicIO::openInputFile("test.zip");
CMS cms = CMS::sign(cert, key, nullptr, file, CMS_DETACHED | CMS_NOCERTS | CMS_BINARY);
CMS other = std::move(cms);
}
TEST(CMS, MoveAssignable)
{
PrivateKey key = PrivateKey::fromPEM("certs/alice.key");
Certificate cert = Certificate::fromPEM("certs/alice.crt");
BasicIO file = BasicIO::openInputFile("test.zip");
CMS cms = CMS::sign(cert, key, nullptr, file, CMS_DETACHED | CMS_NOCERTS | CMS_BINARY);
file = BasicIO::openInputFile("test.zip");
CMS other = CMS::sign(cert, key, nullptr, file, CMS_DETACHED | CMS_NOCERTS | CMS_BINARY);
other = std::move(cms);
}
| 4,347
|
C++
|
.cc
| 111
| 35.072072
| 95
| 0.702793
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,445
|
test_openssl_base_exception.cc
|
falk-werner_zipsign/test/openssl/test_openssl_base_exception.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <gtest/gtest.h>
#include <openssl++/exception.hpp>
using openssl::OpenSSLBaseException;
TEST(OpenSSLBaseException, what)
{
OpenSSLBaseException ex("some error message");
ASSERT_STREQ("some error message", ex.what());
}
| 444
|
C++
|
.cc
| 11
| 38.272727
| 70
| 0.74478
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,446
|
test_file_not_found_exception.cc
|
falk-werner_zipsign/test/openssl/test_file_not_found_exception.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <gtest/gtest.h>
#include <openssl++/exception.hpp>
using openssl::FileNotFoundException;
TEST(FileNotFoundException, path)
{
FileNotFoundException ex("some.file");
ASSERT_STREQ("some.file", ex.path().c_str());
}
| 438
|
C++
|
.cc
| 11
| 37.636364
| 70
| 0.735849
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,447
|
openssl_environment.cc
|
falk-werner_zipsign/test/openssl/openssl_environment.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <gtest/gtest.h>
#include "openssl++/openssl++.hpp"
namespace
{
class OpennSSLEnvironment: ::testing::Environment
{
public:
virtual ~OpennSSLEnvironment() { }
void SetUp() override
{
openssl::OpenSSL::init();
}
};
}
| 459
|
C++
|
.cc
| 17
| 24.176471
| 70
| 0.700229
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,448
|
test_openssl_exception.cc
|
falk-werner_zipsign/test/openssl/test_openssl_exception.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <openssl++/exception.hpp>
#include <openssl/bio.h>
using openssl::OpenSSLException;
using testing::HasSubstr;
TEST(OpenSSLException, ContainsOpenSSLError)
{
BIO * file = BIO_new_file("non-existing.file", "rb");
ASSERT_EQ(nullptr, file);
OpenSSLException ex("open file");
ASSERT_THAT(ex.what(), HasSubstr("open file"));
}
| 606
|
C++
|
.cc
| 16
| 35.5
| 70
| 0.728669
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,449
|
test_app.cc
|
falk-werner_zipsign/test/cli/test_app.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <gtest/gtest.h>
#include <cli/cli.hpp>
#include <cstdlib>
using cli::App;
using cli::Verb;
using cli::Arguments;
class AppTest: public ::testing::Test
{
protected:
void SetUp() override
{
app = new App("SimpleApp");
app->setCopyright("(c) 2019 Falk Werner");
app->setDescription("a simple app");
app->setAdditionalInfo("<FOOTER>");
app->add("run", AppTest::Run);
}
void TearDown() override
{
delete app;
}
static int Run(Arguments const & args)
{
(void) args;
return EXIT_SUCCESS;
}
App * app;
};
TEST_F(AppTest, RunSuccess)
{
char arg0[] = "app";
char arg1[] = "run";
char * argv[] = {arg0, arg1, nullptr};
int exitCode = app->run(2, argv);
ASSERT_EQ(EXIT_SUCCESS, exitCode);
}
TEST_F(AppTest, PrintHelp)
{
char arg0[] = "app";
char arg1[] = "--help";
char * argv[] = {arg0, arg1, nullptr};
int exitCode = app->run(2, argv);
ASSERT_EQ(EXIT_SUCCESS, exitCode);
}
TEST_F(AppTest, PrintHelpForVerb)
{
char arg0[] = "app";
char arg1[] = "run";
char arg2[] = "--help";
char * argv[] = {arg0, arg1, arg2, nullptr};
int exitCode = app->run(3, argv);
ASSERT_EQ(EXIT_SUCCESS, exitCode);
}
TEST_F(AppTest, Fail_MissingVerb)
{
int exitCode = app->run(0, nullptr);
ASSERT_EQ(EXIT_FAILURE, exitCode);
}
TEST_F(AppTest, Fail_InvalidVerb)
{
char arg0[] = "app";
char arg1[] = "invalid";
char * argv[] = { arg0, arg1, nullptr };
int exitCode = app->run(2, argv);
ASSERT_EQ(EXIT_FAILURE, exitCode);
}
| 1,810
|
C++
|
.cc
| 69
| 22.15942
| 70
| 0.620949
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,450
|
test_verb.cc
|
falk-werner_zipsign/test/cli/test_verb.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <gtest/gtest.h>
#include <cstdlib>
#include "cli/cli.hpp"
#include "cli/default_verb.hpp"
#include "cli/mock_app_info.hpp"
using cli::DefaultVerb;
using cli::Arguments;
using testing::ReturnRefOfCopy;
using testing::AtLeast;
int RunSuccess(Arguments const & args)
{
(void) args;
return EXIT_SUCCESS;
}
int RunFailure(Arguments const & args)
{
(void) args;
return EXIT_FAILURE;
}
TEST(DefaultVerb, RunSuccess)
{
AppInfoMock appInfo;
EXPECT_CALL(appInfo, getName()).Times(0);
EXPECT_CALL(appInfo, getCopyright()).Times(0);
EXPECT_CALL(appInfo, getDescription()).Times(0);
DefaultVerb verb(appInfo, "run", RunSuccess);
int exitCode = verb.run(0, nullptr);
ASSERT_EQ(EXIT_SUCCESS, exitCode);
}
TEST(DefaultVerb, RunSuccessWithArgs)
{
AppInfoMock appInfo;
EXPECT_CALL(appInfo, getName()).Times(0);
EXPECT_CALL(appInfo, getCopyright()).Times(0);
EXPECT_CALL(appInfo, getDescription()).Times(0);
DefaultVerb verb(appInfo, "run", RunSuccess);
verb.addArg('f', "file", "a file", true, "");
verb.addFlag('v', "verbose", "enable chatty mode");
verb.addArg('l', "log-level", "set log level", false, "error");
char arg0[] = "run";
char arg1[] = "--file";
char arg2[] = "some.file";
char arg3[] = "--verbose";
char * argv[] = { arg0, arg1, arg2, arg3, nullptr };
int exitCode = verb.run(4, argv);
ASSERT_EQ(EXIT_SUCCESS, exitCode);
}
TEST(DefaultVerb, RunFailure)
{
AppInfoMock appInfo;
EXPECT_CALL(appInfo, getName()).Times(0);
EXPECT_CALL(appInfo, getCopyright()).Times(0);
EXPECT_CALL(appInfo, getDescription()).Times(0);
DefaultVerb verb(appInfo, "run", RunFailure);
int exitCode = verb.run(0, nullptr);
ASSERT_EQ(EXIT_FAILURE, exitCode);
}
TEST(DefaultVerb, Fail_MissingRequiredArg)
{
AppInfoMock appInfo;
EXPECT_CALL(appInfo, getName()).Times(AtLeast(1)).WillRepeatedly(ReturnRefOfCopy(std::string("app")));
EXPECT_CALL(appInfo, getCopyright()).Times(1).WillOnce(ReturnRefOfCopy(std::string("2019 Falk Werner")));
EXPECT_CALL(appInfo, getDescription()).Times(1).WillOnce(ReturnRefOfCopy(std::string("Simple App")));
DefaultVerb verb(appInfo, "run", RunSuccess);
verb.addArg('f', "file", "a file", true, "");
verb.addFlag('v', "verbose", "enable chatty mode");
verb.addArg('l', "log-level", "set log level", true, "error");
char arg0[] = "run";
char arg1[] = "--file";
char arg2[] = "some.file";
char * argv[] = { arg0, arg1, arg2, nullptr };
int exitCode = verb.run(3, argv);
ASSERT_EQ(EXIT_FAILURE, exitCode);
}
TEST(DefaultVerb, Fail_UnrecognizedArg)
{
AppInfoMock appInfo;
EXPECT_CALL(appInfo, getName()).Times(AtLeast(1)).WillRepeatedly(ReturnRefOfCopy(std::string("app")));
EXPECT_CALL(appInfo, getCopyright()).Times(1).WillOnce(ReturnRefOfCopy(std::string("2019 Falk Werner")));
EXPECT_CALL(appInfo, getDescription()).Times(1).WillOnce(ReturnRefOfCopy(std::string("Simple App")));
DefaultVerb verb(appInfo, "run", RunSuccess);
verb.addArg('f', "file", "a file", true, "");
verb.addFlag('v', "verbose", "enable chatty mode");
verb.addArg('l', "log-level", "set log level", false, "error");
char arg0[] = "run";
char arg1[] = "--file";
char arg2[] = "some.file";
char arg3[] = "-v";
char arg4[] = "--unknown";
char * argv[] = { arg0, arg1, arg2, arg3, arg4, nullptr };
int exitCode = verb.run(5, argv);
ASSERT_EQ(EXIT_FAILURE, exitCode);
}
TEST(DefaultVerb, PrintHelp)
{
AppInfoMock appInfo;
EXPECT_CALL(appInfo, getName()).Times(AtLeast(1)).WillRepeatedly(ReturnRefOfCopy(std::string("app")));
EXPECT_CALL(appInfo, getCopyright()).Times(1).WillOnce(ReturnRefOfCopy(std::string("2019 Falk Werner")));
EXPECT_CALL(appInfo, getDescription()).Times(1).WillOnce(ReturnRefOfCopy(std::string("Simple App")));
DefaultVerb verb(appInfo, "run", RunSuccess);
verb.setHelpText("run simple app");
verb.addArg('f', "file", "a file", true, "");
verb.addFlag('v', "verbose", "enable chatty mode");
verb.addArg('l', "log-level", "set log level", false, "error");
char arg0[] = "run";
char arg1[] = "-h";
char * argv[] = { arg0, arg1, nullptr };
int exitCode = verb.run(2, argv);
ASSERT_EQ(EXIT_SUCCESS, exitCode);
}
| 4,563
|
C++
|
.cc
| 113
| 36.247788
| 109
| 0.670809
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,451
|
test_argument.cc
|
falk-werner_zipsign/test/cli/test_argument.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <gtest/gtest.h>
#include <cli/argument.hpp>
using cli::Argument;
using cli::ArgumentType;
TEST(Argument, Id)
{
Argument arg('a', "argument", ArgumentType::STRING, "", true, "");
ASSERT_EQ('a', arg.getId());
}
TEST(Argument, Name)
{
Argument arg('a', "argument", ArgumentType::STRING, "", true, "");
ASSERT_STREQ("argument", arg.getName().c_str());
}
TEST(Argument, HelpText)
{
Argument arg('a', "argument", ArgumentType::STRING, "Description", true, "");
ASSERT_STREQ("Description", arg.getHelpText().c_str());
}
TEST(Argument, DefaultValue)
{
Argument arg('a', "argument", ArgumentType::STRING, "", true, "Value");
ASSERT_STREQ("Value", arg.getDefaultValue().c_str());
ASSERT_TRUE(arg.hasDefaultValue());
}
TEST(DefaultArgument, IsOptionalTrue)
{
Argument arg('a', "argument", ArgumentType::STRING, "", false, "");
ASSERT_TRUE(arg.isOptional());
}
TEST(Argument, IsOptionalFalse)
{
Argument arg('a', "argument", ArgumentType::STRING, "", true, "");
ASSERT_FALSE(arg.isOptional());
}
TEST(Argument, ArgumentTypeFlag)
{
Argument arg('a', "argument", ArgumentType::FLAG, "", false, "");
ASSERT_TRUE(arg.isFlag());
}
TEST(Argument, IsFlagFalse)
{
Argument arg('a', "argument", ArgumentType::STRING, "", true, "");
ASSERT_FALSE(arg.isFlag());
}
| 1,542
|
C++
|
.cc
| 48
| 29.291667
| 81
| 0.677507
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,452
|
test_options.cc
|
falk-werner_zipsign/test/cli/test_options.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <gtest/gtest.h>
#include <cli/options.hpp>
using cli::Options;
using cli::Argument;
using cli::ArgumentType;
TEST(Options, ShortOpts)
{
std::vector<Argument> args;
args.push_back(Argument('f', "file", ArgumentType::STRING, "", true, ""));
args.push_back(Argument('v', "verbose", ArgumentType::FLAG, "", false, ""));
Options opts(args);
ASSERT_STREQ("f:vh", opts.shortOpts());
}
TEST(Options, LongOpts)
{
std::vector<Argument> args;
args.push_back(Argument('f', "file", ArgumentType::STRING, "", true, ""));
args.push_back(Argument('v', "verbose", ArgumentType::FLAG, "", false, ""));
Options opts(args);
option const * long_opts = opts.longOpts();
// file
ASSERT_STREQ("file", long_opts[0].name);
ASSERT_EQ(nullptr, long_opts[0].flag);
ASSERT_EQ('f', long_opts[0].val);
ASSERT_EQ(required_argument, long_opts[0].has_arg);
// verbose
ASSERT_STREQ("verbose", long_opts[1].name);
ASSERT_EQ(nullptr, long_opts[1].flag);
ASSERT_EQ('v', long_opts[1].val);
ASSERT_EQ(no_argument, long_opts[1].has_arg);
// help
ASSERT_STREQ("help", long_opts[2].name);
ASSERT_EQ(nullptr, long_opts[2].flag);
ASSERT_EQ('h', long_opts[2].val);
ASSERT_EQ(no_argument, long_opts[2].has_arg);
// end of options
ASSERT_EQ(nullptr, long_opts[3].name);
ASSERT_EQ(nullptr, long_opts[3].flag);
ASSERT_EQ(0, long_opts[3].val);
ASSERT_EQ(0, long_opts[3].has_arg);
}
| 1,674
|
C++
|
.cc
| 44
| 34.068182
| 80
| 0.655768
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,453
|
test_default_arguments.cc
|
falk-werner_zipsign/test/cli/test_default_arguments.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <gtest/gtest.h>
#include <stdexcept>
#include <cli/default_arguments.hpp>
using cli::DefaultArguments;
TEST(DefaultArguments, Contains)
{
DefaultArguments args;
args.set('c', "");
ASSERT_TRUE(args.contains('c'));
ASSERT_FALSE(args.contains('n'));
}
TEST(DefaultArguments, GetAndSet)
{
DefaultArguments args;
ASSERT_THROW({
args.get('c');
}, std::exception);
args.set('c', "contained");
ASSERT_STREQ("contained", args.get('c').c_str());
}
| 707
|
C++
|
.cc
| 23
| 27.304348
| 70
| 0.689911
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,454
|
main.cc
|
falk-werner_zipsign/src/zipsign/main.cc
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <iostream>
#include <cstdlib>
#include <stdexcept>
#include "zipsign/zipsign.hpp"
#include "openssl++/openssl++.hpp"
#include "cli/cli.hpp"
using openssl::OpenSSL;
using zipsign::Signer;
using zipsign::Verifier;
using zipsign::Informer;
using cli::App;
using cli::Arguments;
using cli::Verb;
namespace
{
int sign(Arguments const & args)
{
auto const & filename = args.get('f');
auto const & key_files = args.getList('p');
auto const & cert_files = args.getList('c');
auto embed_cert = args.contains('e');
int result = EXIT_FAILURE;
try
{
if (key_files.size() != cert_files.size())
{
throw std::runtime_error("count of keys and signer certificated does not match");
}
Signer signer(key_files[0], cert_files[0]);
signer.setEmbedCerts(embed_cert);
if (args.contains('i'))
{
for(auto & intermediate: args.getList('i'))
{
signer.addIntermediate(intermediate);
}
}
for(size_t i = 1; i < key_files.size(); ++i)
{
signer.addSigner(key_files[i], cert_files[i]);
}
signer.sign(filename);
result = EXIT_SUCCESS;
}
catch (std::exception const & ex)
{
std::cerr << "error: " << ex.what() << std::endl;
}
return result;
}
int verify(Arguments const & args)
{
auto const & filename = args.get('f');
auto const & cert_files = args.getList('c');
auto is_verbose = args.contains('v');
auto is_self_signed = args.contains('s');
std::string keyring_path;
if (args.contains('k'))
{
keyring_path = args.get('k');
}
Verifier::Result result = Verifier::Bad;
try
{
Verifier verifier(cert_files[0]);
for(size_t i = 1; i < cert_files.size(); ++i)
{
verifier.addCertificate(cert_files[1]);
}
result = verifier.verify(filename, keyring_path, is_verbose, is_self_signed);
switch (result) {
case Verifier::Good:
std::cout << "OK" << std::endl;
break;
case Verifier::BadMissingSignature:
std::cout << "INVALID_MISSING_SIGNATURE" << std::endl;
break;
case Verifier::BadInvalidCertificateChain:
std::cout << "INVALID_CERTIFICATE_CHAIN" << std::endl;
break;
case Verifier::BadInvalidSignature:
std::cout << "INVALID_SIGNATURE" << std::endl;
break;
case Verifier::Bad:
// fall-through
default:
std::cout << "INVALID" << std::endl;
break;
}
}
catch (std::exception const & ex)
{
std::cerr << "error: " << ex.what() << std::endl;
}
return static_cast<int>(result);
}
int info(Arguments const & args)
{
int result = EXIT_FAILURE;
auto const & filename = args.get('f');
try
{
Informer informer;
informer.print(filename);
result = EXIT_SUCCESS;
}
catch (std::exception const & ex)
{
std::cerr << "error: " << ex.what() << std::endl;
}
return result;
}
}
int main(int argc, char * argv[])
{
OpenSSL::init();
App app("zipsign");
app
.setCopyright("2019 Falk Werner")
.setDescription("Signs and verifies ZIP archives")
.setAdditionalInfo(
"Examples:\n"
"\tSign:\n"
"\t\tzipsign sign -f archive.zip -p key.pem -c cert.pem\n"
"\tVerify:\n"
"\t\tzipsign verify -f archive.zip -c cert.pem\n"
"\t\tzipsign verify -f archive.zip -c cert.pem --self-signed\n"
)
;
app.add("sign", sign)
.setHelpText("Signs a zip archive.")
.addArg('f', "file", "Archive to sign.")
.addList('p', "private-key", "Private key to sign.")
.addList('c', "certificate", "Certificate of signer.")
.addList('i', "intermediate", "Add intermediate certificate", false)
.addFlag('e', "embed-certificate", "Embed signers certificate in signature")
.addFlag('v', "verbose", "Enable additional output")
;
app.add("verify", verify)
.setHelpText("Verifies the signature of a zip archive.")
.addArg('f', "file", "Archive to verify.")
.addArg('c', "certificate", "Certificate of signer.")
.addArg('k', "keyring", "Path of keyring file.", false)
.addFlag('v', "verbose", "Enable additionl output")
.addFlag('s', "self-signed", "Allows self signed certificates, skip cert verify")
;
app.add("info", info)
.setHelpText("Print info about the signature of zip archive.")
.addArg('f', "file", "Archive to verify.")
;
return app.run(argc, argv);
}
| 5,501
|
C++
|
.cc
| 155
| 25.232258
| 97
| 0.530257
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,455
|
ftruncate.h
|
falk-werner_zipsign/lib/zipsign/ftruncate.h
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef ZIPSIGN_FTRUNCATE_H
#define ZIPSIGN_FTRUNCATE_H
#if !defined(_WIN32)
#include <unistd.h>
#else
#include <io.h>
#define ftruncate(fd, length) _chsize(fd, length)
#endif
#endif
| 392
|
C++
|
.h
| 12
| 31.25
| 70
| 0.745358
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,456
|
file.hpp
|
falk-werner_zipsign/lib/zipsign/file.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef ZIPSIGN_FILE_HPP
#define ZIPSIGN_FILE_HPP
#include <string>
#include <cstdio>
namespace zipsign
{
class File
{
File operator=(File const &) = delete;
File(File const &) = delete;
public:
static void remove(std::string const & filename);
File(std::string const & name, std::string const & mode);
~File();
void seek(long offset, int whence = SEEK_SET);
long tell();
void write(void const * buffer, size_t count);
size_t read(void * buffer, size_t count, bool check = true);
void truncate(long offset);
private:
FILE * file;
};
}
#endif
| 797
|
C++
|
.h
| 27
| 26.62963
| 70
| 0.694118
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,457
|
zip.hpp
|
falk-werner_zipsign/lib/zipsign/zip.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef ZIPSIGN_ZIP_HPP
#define ZIPSIGN_ZIP_HPP
#include <string>
#include <cstddef>
namespace zipsign
{
class Zip
{
public:
Zip(std::string const & filename_);
~Zip();
void setComment(std::string const & comment);
std::string getComment();
std::size_t getCommentStart();
private:
std::string filename;
};
}
#endif
| 550
|
C++
|
.h
| 22
| 22.590909
| 70
| 0.717017
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,458
|
partial_input_file.hpp
|
falk-werner_zipsign/lib/zipsign/partial_input_file.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef ZIPSIGN_PARTIAL_INPUT_FILE_HPP
#define ZIPSIGN_PARTIAL_INPUT_FILE_HPP
#include <string>
#include <cstddef>
#include <openssl++/openssl++.hpp>
namespace zipsign
{
class PartialInputFile
{
PartialInputFile operator=(PartialInputFile const &) = delete;
PartialInputFile(PartialInputFile const &other) = delete;
public:
PartialInputFile();
~PartialInputFile();
openssl::BasicIO open(std::string const & filename, std::size_t upperLimit);
private:
BIO_METHOD * method;
};
}
#endif
| 720
|
C++
|
.h
| 23
| 28.913043
| 80
| 0.752533
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,459
|
signature.hpp
|
falk-werner_zipsign/lib/zipsign/signature.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef ZIPSIGN_SIGNATURE_HPP
#define ZIPSIGN_SIGNATURE_HPP
#define ZIPSIGN_SIGNATURE_PREFIX ("ZipSign=data:application/cms;base64,")
#endif
| 350
|
C++
|
.h
| 7
| 48.285714
| 73
| 0.764706
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,460
|
options.hpp
|
falk-werner_zipsign/lib/cli/options.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef CLI_OPTIONS_HPP
#define CLI_OPTIONS_HPP
#include <string>
#include <vector>
#include <memory>
#include "cli/argument.hpp"
extern "C"
{
#include <getopt.h>
}
namespace cli
{
class Options
{
public:
explicit Options(std::vector<Argument> const & args);
~Options();
char const * shortOpts() const;
option const * longOpts() const;
private:
option * long_opts;
std::string short_opts;
};
}
#endif
| 640
|
C++
|
.h
| 28
| 20.607143
| 70
| 0.718543
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| true
| false
|
1,535,461
|
argument.hpp
|
falk-werner_zipsign/lib/cli/argument.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef CLI_DEFAULT_ARGUMENT_HPP
#define CLI_DEFAULT_ARGUMENT_HPP
#include <string>
#include "cli/argument_type.hpp"
namespace cli
{
class Argument
{
public:
Argument(
char id_,
std::string const & name_,
ArgumentType type_,
std::string const & helpText_,
bool isRequired_,
std::string const & defaultValue_);
~Argument();
char getId() const;
std::string const & getName() const;
std::string const & getHelpText() const;
std::string const & getDefaultValue() const;
bool isOptional() const;
bool isFlag() const;
bool hasDefaultValue() const;
private:
char id;
std::string name;
ArgumentType type;
std::string helpText;
bool isRequired;
std::string defaultValue;
};
}
#endif
| 994
|
C++
|
.h
| 37
| 22.72973
| 70
| 0.681388
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,462
|
default_verb.hpp
|
falk-werner_zipsign/lib/cli/default_verb.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef CLI_DEFAULT_VERB_HPP
#define CLI_DEFAULT_VERB_HPP
#include "cli/verb.hpp"
#include "cli/app_info.hpp"
#include "cli/argument.hpp"
#include "cli/command.hpp"
namespace cli
{
class DefaultVerb: public Verb
{
public:
DefaultVerb(AppInfo & appInfo_, std::string const & name, Command command);
~DefaultVerb() override;
Verb & setHelpText(std::string const & helpText_) override;
virtual Verb & addFlag(char id, std::string const & name, std::string const & description) override;
virtual Verb & addArg(
char id,
std::string const & name,
std::string const & description,
bool isRequired,
std::string const & defaultValue) override;
virtual Verb & addList(
char id,
std::string const & name,
std::string const & description,
bool isRequires,
std::string const & defaultValue) override;
std::string const & getName() const;
std::string const & getHelpText() const;
int run(int argc, char * argv[]) const;
private:
void printUsage() const;
AppInfo & appInfo;
std::string name;
Command command;
std::vector<Argument> args;
std::string helpText;
};
}
#endif
| 1,411
|
C++
|
.h
| 43
| 28.325581
| 104
| 0.684559
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,463
|
default_arguments.hpp
|
falk-werner_zipsign/lib/cli/default_arguments.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "cli/arguments.hpp"
#include <unordered_map>
namespace cli
{
class DefaultArguments: public Arguments
{
public:
DefaultArguments();
~DefaultArguments() override;
bool contains(char id) const override;
std::string const & get(char id) const override;
std::vector<std::string> const & getList(char id) const override;
void set(char id, std::string const & value);
private:
std::unordered_map<char, std::vector<std::string>> values;
};
}
| 686
|
C++
|
.h
| 20
| 31.4
| 70
| 0.723565
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,464
|
argument_type.hpp
|
falk-werner_zipsign/lib/cli/argument_type.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef CLI_ARGUMENT_TYPE_HPP
#define CLI_ARGUMENT_TYPE_HPP
namespace cli
{
enum class ArgumentType
{
FLAG,
STRING,
LIST
};
}
#endif
| 378
|
C++
|
.h
| 15
| 21.466667
| 70
| 0.683333
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,465
|
app_info.hpp
|
falk-werner_zipsign/lib/cli/app_info.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef CLI_APP_INFO_HPP
#define CLI_APP_INFO_HPP
#include <string>
namespace cli
{
class AppInfo
{
public:
virtual ~AppInfo() { }
virtual std::string const & getName() const = 0;
virtual std::string const & getDescription() const = 0;
virtual std::string const & getCopyright() const = 0;
};
}
#endif
| 530
|
C++
|
.h
| 18
| 27.111111
| 70
| 0.711462
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,466
|
testutils.hpp
|
falk-werner_zipsign/test/testutils.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef TEST_FILE_UTILS_HPP
#define TEST_FILE_UTILS_HPP
#include <string>
namespace testutils
{
void copy_file(std::string const & from, std::string const & to);
}
#endif
| 383
|
C++
|
.h
| 11
| 33.090909
| 70
| 0.743169
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,467
|
mock_app_info.hpp
|
falk-werner_zipsign/test/cli/mock_app_info.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef CLI_MOCK_APP_INFO_HPP
#define CLI_MOCK_APP_INFO_HPP
#include <gmock/gmock.h>
#include <cli/app_info.hpp>
class AppInfoMock: public cli::AppInfo
{
public:
MOCK_CONST_METHOD0(getName, std::string const & ());
MOCK_CONST_METHOD0(getDescription, std::string const & ());
MOCK_CONST_METHOD0(getCopyright, std::string const & ());
};
#endif
| 565
|
C++
|
.h
| 15
| 35.466667
| 70
| 0.725275
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,468
|
base64.h
|
falk-werner_zipsign/include/base64/base64.h
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef BASE64_H
#define BASE64_H
#ifndef __cplusplus
#include <inttypes.h>
#include <stddef.h>
#include <stdbool.h>
#else
#include <cinttypes>
#include <cstddef>
#endif
#ifdef __cplusplus
extern "C"
{
#endif
extern size_t base64_encoded_size(size_t length);
extern size_t base64_encode(
uint8_t const * data,
size_t length,
char * buffer,
size_t buffer_size);
extern size_t base64_decoded_size(char const * data, size_t length);
extern size_t base64_decode(
char const * data,
size_t length,
uint8_t * buffer,
size_t buffer_size);
extern bool base64_isvalid(char const * data, size_t length);
#ifdef __cplusplus
}
#endif
#endif
| 877
|
C++
|
.h
| 34
| 23.5
| 70
| 0.728691
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,469
|
init.hpp
|
falk-werner_zipsign/include/openssl++/init.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef OPENSSL_INIT_HPP
#define OPENSSL_INIT_HPP
namespace openssl
{
class OpenSSL
{
public:
//-----------------------------------------------------------------------------
/// Initialized OpenSSL library.
///
/// \ņote The application will be terminated, if initialization fails.
//-----------------------------------------------------------------------------
static void init(void);
};
}
#endif
| 634
|
C++
|
.h
| 19
| 30.736842
| 83
| 0.529508
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| true
| false
| true
| false
|
1,535,470
|
basic_io.hpp
|
falk-werner_zipsign/include/openssl++/basic_io.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef OPENSSL_BIO_HPP
#define OPENSSL_BIO_HPP
#include <openssl/bio.h>
#include <string>
namespace openssl
{
class BasicIO
{
BasicIO operator=(BasicIO const &) = delete;
BasicIO(BasicIO const &) = delete;
public:
static BasicIO openInputFile(std::string const & filename);
static BasicIO fromMemory();
static BasicIO fromMemory(void const * data, size_t size);
explicit BasicIO(BIO * bio_);
BasicIO & operator=(BasicIO && other);
BasicIO(BasicIO && other);
~BasicIO();
operator BIO*();
private:
BIO * bio;
};
}
#endif
| 776
|
C++
|
.h
| 27
| 25.814815
| 70
| 0.707941
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| true
| false
|
1,535,471
|
cms.hpp
|
falk-werner_zipsign/include/openssl++/cms.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef OPENSSL_CMS_HPP
#define OPENSSL_CMS_HPP
#include <openssl/cms.h>
#include <string>
namespace openssl
{
class CMS
{
CMS & operator=(CMS const &) = delete;
CMS(CMS const &) = delete;
public:
static CMS fromBase64(std::string const & data);
static CMS sign(X509 * cert, EVP_PKEY * key, STACK_OF(X509) * certs, BIO * data, unsigned int flags);
explicit CMS(CMS_ContentInfo * cms_);
CMS(CMS && other);
CMS & operator=(CMS && other);
~CMS();
operator CMS_ContentInfo*();
void addSigner(X509 * cert, EVP_PKEY * key, EVP_MD const * md, unsigned int flags);
void final(BIO * data, BIO * dcont, unsigned int flags);
void saveToBIO(BIO * bio) const;
bool verify(STACK_OF(X509) * certs, X509_STORE * store, BIO * indata, BIO * outdata, unsigned int flags, bool is_verbose = false);
STACK_OF(X509) * getCerts();
std::string toBase64() const;
std::string toString() const;
private:
CMS_ContentInfo * cms;
};
}
#endif
| 1,191
|
C++
|
.h
| 33
| 32.787879
| 134
| 0.681424
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,472
|
openssl++.hpp
|
falk-werner_zipsign/include/openssl++/openssl++.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef OPENSSL_HPP
#define OPENSSL_HPP
#include <openssl++/exception.hpp>
#include <openssl++/init.hpp>
#include <openssl++/private_key.hpp>
#include <openssl++/basic_io.hpp>
#include <openssl++/certificate.hpp>
#include <openssl++/certificate_store.hpp>
#include <openssl++/certificate_stack.hpp>
#include <openssl++/cms.hpp>
#endif
| 544
|
C++
|
.h
| 14
| 37.5
| 70
| 0.749526
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| true
| false
| true
| false
|
1,535,473
|
exception.hpp
|
falk-werner_zipsign/include/openssl++/exception.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef OPENSSL_EXCEPTION_HPP
#define OPENSSL_EXCEPTION_HPP
#include <exception>
#include <string>
namespace openssl
{
class OpenSSLBaseException: public std::exception
{
public:
OpenSSLBaseException(std::string const & message_);
~OpenSSLBaseException();
char const * what() const noexcept override;
private:
std::string message;
};
class FileNotFoundException: public OpenSSLBaseException
{
public:
FileNotFoundException(std::string const & filename_);
~FileNotFoundException();
std::string const & path() const;
private:
std::string filename;
};
class OpenSSLException: public OpenSSLBaseException
{
public:
OpenSSLException(std::string const & message_);
~OpenSSLException();
};
}
#endif
| 946
|
C++
|
.h
| 35
| 24.6
| 70
| 0.761905
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| true
| false
| true
| false
|
1,535,474
|
certificate_store.hpp
|
falk-werner_zipsign/include/openssl++/certificate_store.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef OPENSSL_CERTIFICATE_STORE_HPP
#define OPENSSL_CERTIFICATE_STORE_HPP
#include <openssl/x509_vfy.h>
#include <string>
namespace openssl
{
class CertificateStore
{
CertificateStore& operator=(CertificateStore const &) = delete;
CertificateStore(CertificateStore const &) = delete;
public:
CertificateStore();
~CertificateStore();
CertificateStore & operator=(CertificateStore && other);
CertificateStore(CertificateStore && other);
operator X509_STORE *();
void add(X509 * cert);
void loadFromFile(std::string const & filename);
private:
X509_STORE * store;
};
}
#endif
| 829
|
C++
|
.h
| 26
| 28.961538
| 70
| 0.742785
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| true
| false
|
1,535,475
|
certificate_stack.hpp
|
falk-werner_zipsign/include/openssl++/certificate_stack.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef OPENSSL_CERTIFICATE_STACK_HPP
#define OPENSSL_CERTIFICATE_STACK_HPP
#include <openssl/safestack.h>
#include <openssl/x509.h>
namespace openssl
{
class CertificateStack
{
CertificateStack operator=(CertificateStack const &) = delete;
CertificateStack(CertificateStack const &) = delete;
public:
CertificateStack();
~CertificateStack();
operator STACK_OF(X509) *();
void push(X509 * certificate);
private:
STACK_OF(X509) * stack;
};
}
#endif
| 689
|
C++
|
.h
| 23
| 27.347826
| 70
| 0.746586
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| true
| false
| true
| false
|
1,535,476
|
certificate.hpp
|
falk-werner_zipsign/include/openssl++/certificate.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef OPENSSL_CERTIFICATE_HPP
#define OPENSSL_CERTIFICATE_HPP
#include <openssl/x509.h>
#include <string>
namespace openssl
{
class Certificate
{
Certificate operator=(Certificate const &) = delete;
Certificate(Certificate const &) = delete;
public:
static Certificate fromPEM(std::string const & filename);
explicit Certificate(X509 * cert_);
Certificate & operator=(Certificate && other);
Certificate(Certificate && other);
~Certificate();
operator X509*();
bool verify(X509_STORE * store, STACK_OF(X509) * chain, STACK_OF(X509) * untrusted);
private:
X509 * cert;
};
}
#endif
| 833
|
C++
|
.h
| 26
| 29.192308
| 88
| 0.726592
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| true
| false
|
1,535,477
|
private_key.hpp
|
falk-werner_zipsign/include/openssl++/private_key.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef OPENSSL_PRIVATE_KEY_HPP
#define OPENSSL_PRIVATE_KEY_HPP
#include <openssl/evp.h>
#include <string>
namespace openssl
{
class PrivateKey
{
PrivateKey& operator=(PrivateKey const &) = delete;
PrivateKey(PrivateKey const &) = delete;
public:
static PrivateKey fromPEM(std::string const & filename);
explicit PrivateKey(EVP_PKEY * key_);
PrivateKey & operator=(PrivateKey && other);
PrivateKey(PrivateKey && other);
~PrivateKey();
operator EVP_PKEY*();
private:
EVP_PKEY * key;
};
}
#endif
| 744
|
C++
|
.h
| 25
| 26.92
| 70
| 0.722925
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| true
| false
| true
| false
|
1,535,478
|
zipsign.hpp
|
falk-werner_zipsign/include/zipsign/zipsign.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef ZIPSIGN_ZIPSIGN_HPP
#define ZIPSIGN_ZIPSIGN_HPP
#include <zipsign/signer.hpp>
#include <zipsign/verifier.hpp>
#include <zipsign/informer.hpp>
#endif
| 366
|
C++
|
.h
| 9
| 39.111111
| 70
| 0.762712
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,479
|
signer.hpp
|
falk-werner_zipsign/include/zipsign/signer.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef ZIPSIGN_SIGNER_HPP
#define ZIPSIGN_SIGNER_HPP
#include <openssl++/openssl++.hpp>
#include <string>
#include <vector>
namespace zipsign
{
class Signer
{
Signer operator=(Signer const &) = delete;
Signer(Signer const &) = delete;
public:
Signer(std::string const & key_file, std::string const & cert_file);
~Signer();
void addSigner(std::string const key_file, std::string const & cert_file);
void addIntermediate(std::string const & filename);
void sign(std::string const & filename);
void setEmbedCerts(bool value = true);
private:
std::string createSignature(std::string const & filename);
std::vector<openssl::PrivateKey> keys;
std::vector<openssl::Certificate> certs;
std::vector<openssl::Certificate> intermediates;
bool embedCerts;
};
}
#endif
| 1,022
|
C++
|
.h
| 30
| 31.033333
| 78
| 0.723858
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,480
|
verifier.hpp
|
falk-werner_zipsign/include/zipsign/verifier.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef ZIPSIGN_VERIFIER_HPP
#define ZIPSIGN_VERIFIER_HPP
#include <openssl++/openssl++.hpp>
#include <string>
#include <vector>
#include <utility>
namespace zipsign
{
class Verifier
{
Verifier operator=(Verifier const &) = delete;
Verifier(Verifier const &) = delete;
public:
enum Result {
Good = 0,
Bad = 1,
BadMissingSignature = 2,
BadInvalidCertificateChain = 3,
BadInvalidSignature = 4
};
Verifier(std::string const & cert_file);
~Verifier();
void addCertificate(std::string const & filename);
Result verify(
std::string const & filename,
std::string const & keyring_path,
bool is_verbose = false,
bool is_self_signed = false);
private:
std::vector<openssl::Certificate> signers;
};
}
#endif
| 1,017
|
C++
|
.h
| 36
| 24.027778
| 70
| 0.68
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,481
|
informer.hpp
|
falk-werner_zipsign/include/zipsign/informer.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef ZIPSIGN_INFORMER_HPP
#define ZIPSIGN_INFORMER_HPP
#include <string>
#include <iostream>
namespace zipsign
{
class Informer
{
Informer operator=(Informer const &) = delete;
Informer(Informer const &) = delete;
public:
Informer();
~Informer();
void print(std::string const & filename, std::ostream & out = std::cout);
};
}
#endif
| 568
|
C++
|
.h
| 20
| 26
| 77
| 0.721402
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,482
|
verb.hpp
|
falk-werner_zipsign/include/cli/verb.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef CLI_VERB_HPP
#define CLI_VERB_HPP
#include <string>
namespace cli
{
class Verb
{
public:
virtual ~Verb() { }
virtual Verb & setHelpText(std::string const & helpText_) = 0;
virtual Verb & addFlag(
char id,
std::string const & name,
std::string const & description = "") = 0;
virtual Verb & addArg(
char id,
std::string const & name,
std::string const & description = "",
bool isRequired = true,
std::string const & defaultValue = "") = 0;
virtual Verb & addList(
char id,
std::string const & name,
std::string const & description = "",
bool isRequired = true,
std::string const & defaultValue = "") = 0;
};
}
#endif
| 960
|
C++
|
.h
| 32
| 24.84375
| 70
| 0.618893
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,483
|
app.hpp
|
falk-werner_zipsign/include/cli/app.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef CLI_APP_HPP
#define CLI_APP_HPP
#include <string>
#include <vector>
#include "cli/verb.hpp"
#include "cli/command.hpp"
namespace cli
{
class App
{
public:
App(std::string const & name_);
~App();
int run(int argc, char* argv[]) const;
Verb & add(std::string const & name, Command command);
App & setCopyright(std::string const & value);
App & setDescription(std::string const & value);
App & setAdditionalInfo(std::string const & value);
private:
class Private;
Private * d;
};
}
#endif
| 751
|
C++
|
.h
| 27
| 24.851852
| 70
| 0.690377
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,484
|
arguments.hpp
|
falk-werner_zipsign/include/cli/arguments.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef CLI_ARGUMENTS_HPP
#define CLI_ARGUMENTS_HPP
#include <string>
#include <vector>
namespace cli
{
class Arguments
{
public:
virtual ~Arguments() { }
virtual bool contains(char id) const = 0;
virtual std::string const & get(char id) const = 0;
virtual std::vector<std::string> const & getList(char id) const = 0;
};
}
#endif
| 558
|
C++
|
.h
| 19
| 27.105263
| 72
| 0.718574
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,485
|
command.hpp
|
falk-werner_zipsign/include/cli/command.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef CLI_COMMAND_HPP
#define CLI_COMMAND_HPP
#include <functional>
#include <cli/arguments.hpp>
namespace cli
{
using Command = std::function<int (Arguments const &)>;
}
#endif
| 392
|
C++
|
.h
| 12
| 31
| 70
| 0.748663
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,486
|
cli.hpp
|
falk-werner_zipsign/include/cli/cli.hpp
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef CLI_HPP
#define CLI_HPP
#include <cli/command.hpp>
#include <cli/app.hpp>
#include <cli/arguments.hpp>
#include <cli/verb.hpp>
#endif
| 351
|
C++
|
.h
| 10
| 33.6
| 70
| 0.739645
|
falk-werner/zipsign
| 36
| 13
| 1
|
MPL-2.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| true
| false
|
1,535,487
|
N2kDataToNMEA0183.cpp
|
AK-Homberger_M5Stack-NMEA-2000-Display-CAN-BUS/M5_NMEA_Display_CAN_Bus/N2kDataToNMEA0183.cpp
|
/*
N2kDataToNMEA0183.cpp
Copyright (c) 2015-2018 Timo Lappalainen, Kave Oy, www.kave.fi
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <N2kMessages.h>
#include <NMEA0183Messages.h>
#include <math.h>
#include "N2kDataToNMEA0183.h"
#include "BoatData.h"
const double radToDeg = 180.0 / M_PI;
//*****************************************************************************
void tN2kDataToNMEA0183::HandleMsg(const tN2kMsg &N2kMsg) {
switch (N2kMsg.PGN) {
case 127250UL: HandleHeading(N2kMsg);
case 127258UL: HandleVariation(N2kMsg);
case 128259UL: HandleBoatSpeed(N2kMsg);
case 128267UL: HandleDepth(N2kMsg);
case 129025UL: HandlePosition(N2kMsg);
case 129026UL: HandleCOGSOG(N2kMsg);
case 129029UL: HandleGNSS(N2kMsg);
case 130306UL: HandleWind(N2kMsg);
case 128275UL: HandleLog(N2kMsg);
case 127245UL: HandleRudder(N2kMsg);
case 130310UL: HandleWaterTemp(N2kMsg);
}
}
//*****************************************************************************
long tN2kDataToNMEA0183::Update(tBoatData *BoatData) {
SendRMC();
if ( LastHeadingTime + 2000 < millis() ) Heading = N2kDoubleNA;
if ( LastCOGSOGTime + 2000 < millis() ) {
COG = N2kDoubleNA;
SOG = N2kDoubleNA;
}
if ( LastPositionTime + 4000 < millis() ) {
Latitude = N2kDoubleNA;
Longitude = N2kDoubleNA;
}
if ( LastWindTime + 2000 < millis() ) {
AWS = N2kDoubleNA;
AWA = N2kDoubleNA;
TWS = N2kDoubleNA;
TWA = N2kDoubleNA;
TWD = N2kDoubleNA;
}
BoatData->Latitude=Latitude;
BoatData->Longitude=Longitude;
BoatData->Altitude=Altitude;
BoatData->Heading=Heading * radToDeg;
BoatData->COG=COG * radToDeg;
BoatData->SOG=SOG * 3600.0/1852.0;
BoatData->STW=STW * 3600.0/1852.0;
BoatData->AWS=AWS * 3600.0/1852.0;
BoatData->TWS=TWS * 3600.0/1852.0;
BoatData->MaxAws=MaxAws * 3600.0/1852.0;;
BoatData->MaxTws=MaxTws * 3600.0/1852.0;;
BoatData->AWA=AWA * radToDeg;
BoatData->TWA=TWA * radToDeg;
BoatData->TWD=TWD * radToDeg;
BoatData->TripLog=TripLog / 1825.0;
BoatData->Log=Log / 1825.0;
BoatData->RudderPosition=RudderPosition * radToDeg;
BoatData->WaterTemperature=KelvinToC(WaterTemperature) ;
BoatData->WaterDepth=WaterDepth;
BoatData->Variation=Variation *radToDeg;
BoatData->GPSTime=SecondsSinceMidnight;
BoatData->DaysSince1970=DaysSince1970;
if (SecondsSinceMidnight!=N2kDoubleNA && DaysSince1970!=N2kUInt16NA){
return((DaysSince1970*3600*24)+SecondsSinceMidnight); // Needed for SD Filename and time
} else {
return(0); // Needed for SD Filename and time
}
}
//*****************************************************************************
void tN2kDataToNMEA0183::SendMessage(const tNMEA0183Msg &NMEA0183Msg) {
if ( pNMEA0183 != 0 ) pNMEA0183->SendMessage(NMEA0183Msg);
if ( SendNMEA0183MessageCallback != 0 ) SendNMEA0183MessageCallback(NMEA0183Msg);
}
//*****************************************************************************
void tN2kDataToNMEA0183::HandleHeading(const tN2kMsg &N2kMsg) {
unsigned char SID;
tN2kHeadingReference ref;
double _Deviation = 0;
double _Variation;
tNMEA0183Msg NMEA0183Msg;
if ( ParseN2kHeading(N2kMsg, SID, Heading, _Deviation, _Variation, ref) ) {
if ( ref == N2khr_magnetic ) {
if ( !N2kIsNA(_Variation) ) Variation = _Variation; // Update Variation
if ( !N2kIsNA(Heading) && !N2kIsNA(Variation) ) Heading -= Variation;
}
LastHeadingTime = millis();
if ( NMEA0183SetHDG(NMEA0183Msg, Heading, _Deviation, Variation) ) {
SendMessage(NMEA0183Msg);
}
}
}
//*****************************************************************************
void tN2kDataToNMEA0183::HandleVariation(const tN2kMsg &N2kMsg) {
unsigned char SID;
tN2kMagneticVariation Source;
uint16_t DaysSince1970;
ParseN2kMagneticVariation(N2kMsg, SID, Source, DaysSince1970, Variation);
}
//*****************************************************************************
void tN2kDataToNMEA0183::HandleBoatSpeed(const tN2kMsg &N2kMsg) {
unsigned char SID;
double WaterReferenced;
double GroundReferenced;
tN2kSpeedWaterReferenceType SWRT;
if ( ParseN2kBoatSpeed(N2kMsg, SID, WaterReferenced, GroundReferenced, SWRT) ) {
tNMEA0183Msg NMEA0183Msg;
STW=WaterReferenced;
double MagneticHeading = ( !N2kIsNA(Heading) && !N2kIsNA(Variation) ? Heading + Variation : NMEA0183DoubleNA);
if ( NMEA0183SetVHW(NMEA0183Msg, Heading, MagneticHeading, WaterReferenced) ) {
SendMessage(NMEA0183Msg);
}
}
}
//*****************************************************************************
void tN2kDataToNMEA0183::HandleDepth(const tN2kMsg &N2kMsg) {
unsigned char SID;
double DepthBelowTransducer;
double Offset;
double Range;
if ( ParseN2kWaterDepth(N2kMsg, SID, DepthBelowTransducer, Offset, Range) ) {
WaterDepth=DepthBelowTransducer+Offset;
tNMEA0183Msg NMEA0183Msg;
if ( NMEA0183SetDPT(NMEA0183Msg, DepthBelowTransducer, Offset) ) {
SendMessage(NMEA0183Msg);
}
if ( NMEA0183SetDBx(NMEA0183Msg, DepthBelowTransducer, Offset) ) {
SendMessage(NMEA0183Msg);
}
}
}
//*****************************************************************************
void tN2kDataToNMEA0183::HandlePosition(const tN2kMsg &N2kMsg) {
if ( ParseN2kPGN129025(N2kMsg, Latitude, Longitude) ) {
LastPositionTime = millis();
}
}
//*****************************************************************************
void tN2kDataToNMEA0183::HandleCOGSOG(const tN2kMsg &N2kMsg) {
unsigned char SID;
tN2kHeadingReference HeadingReference;
tNMEA0183Msg NMEA0183Msg;
if ( ParseN2kCOGSOGRapid(N2kMsg, SID, HeadingReference, COG, SOG) ) {
LastCOGSOGTime = millis();
double MCOG = ( !N2kIsNA(COG) && !N2kIsNA(Variation) ? COG - Variation : NMEA0183DoubleNA );
if ( HeadingReference == N2khr_magnetic ) {
MCOG = COG;
if ( !N2kIsNA(Variation) ) COG -= Variation;
}
if ( NMEA0183SetVTG(NMEA0183Msg, COG, MCOG, SOG) ) {
SendMessage(NMEA0183Msg);
}
}
}
//*****************************************************************************
void tN2kDataToNMEA0183::HandleGNSS(const tN2kMsg &N2kMsg) {
unsigned char SID;
tN2kGNSStype GNSStype;
tN2kGNSSmethod GNSSmethod;
unsigned char nSatellites;
double HDOP;
double PDOP;
double GeoidalSeparation;
unsigned char nReferenceStations;
tN2kGNSStype ReferenceStationType;
uint16_t ReferenceSationID;
double AgeOfCorrection;
if ( ParseN2kGNSS(N2kMsg, SID, DaysSince1970, SecondsSinceMidnight, Latitude, Longitude, Altitude, GNSStype, GNSSmethod,
nSatellites, HDOP, PDOP, GeoidalSeparation,
nReferenceStations, ReferenceStationType, ReferenceSationID, AgeOfCorrection) ) {
LastPositionTime = millis();
}
}
//*****************************************************************************
void tN2kDataToNMEA0183::HandleWind(const tN2kMsg &N2kMsg) {
unsigned char SID;
tN2kWindReference WindReference;
tNMEA0183WindReference NMEA0183Reference = NMEA0183Wind_True;
double x, y;
double WindAngle, WindSpeed;
// double TWS, TWA, AWD;
if ( ParseN2kWindSpeed(N2kMsg, SID, WindSpeed, WindAngle, WindReference) ) {
tNMEA0183Msg NMEA0183Msg;
LastWindTime = millis();
if ( WindReference == N2kWind_Apparent ) {
NMEA0183Reference = NMEA0183Wind_Apparent;
AWA=WindAngle;
AWS=WindSpeed;
if (AWS > MaxAws) MaxAws=AWS;
}
if ( NMEA0183SetMWV(NMEA0183Msg, WindAngle, NMEA0183Reference , WindSpeed)) SendMessage(NMEA0183Msg);
if (WindReference == N2kWind_Apparent && SOG != N2kDoubleNA) { // Lets calculate and send TWS/TWA if SOG is available
AWD=WindAngle*radToDeg + Heading*radToDeg;
if (AWD>360) AWD=AWD-360;
if (AWD<0) AWD=AWD+360;
x = WindSpeed * cos(WindAngle);
y = WindSpeed * sin(WindAngle);
TWA = atan2(y, -SOG + x);
TWS = sqrt(( y*y) + ((-SOG+x)*(-SOG+x)));
if (TWS > MaxTws) MaxTws=TWS;
TWA = TWA * radToDeg +360;
if (TWA>360) TWA=TWA-360;
if (TWA<0) TWA=TWA+360;
NMEA0183Reference = NMEA0183Wind_True;
if ( NMEA0183SetMWV(NMEA0183Msg, TWA, NMEA0183Reference , TWS)) SendMessage(NMEA0183Msg);
if ( !NMEA0183Msg.Init("MWD", "GP") ) return;
if ( !NMEA0183Msg.AddDoubleField(AWD) ) return;
if ( !NMEA0183Msg.AddStrField("T") ) return;
if ( !NMEA0183Msg.AddDoubleField(AWD) ) return;
if ( !NMEA0183Msg.AddStrField("M") ) return;
if ( !NMEA0183Msg.AddDoubleField(TWS/0.514444) ) return;
if ( !NMEA0183Msg.AddStrField("N") ) return;
if ( !NMEA0183Msg.AddDoubleField(TWS) ) return;
if ( !NMEA0183Msg.AddStrField("M") ) return;
SendMessage(NMEA0183Msg);
TWA=TWA/radToDeg;
}
}
}
//*****************************************************************************
void tN2kDataToNMEA0183::SendRMC() {
if ( NextRMCSend <= millis() && !N2kIsNA(Latitude) ) {
tNMEA0183Msg NMEA0183Msg;
if ( NMEA0183SetRMC(NMEA0183Msg, SecondsSinceMidnight, Latitude, Longitude, COG, SOG, DaysSince1970, Variation) ) {
SendMessage(NMEA0183Msg);
}
SetNextRMCSend();
}
}
//*****************************************************************************
void tN2kDataToNMEA0183::HandleLog(const tN2kMsg & N2kMsg) {
uint16_t DaysSince1970;
double SecondsSinceMidnight;
if ( ParseN2kDistanceLog(N2kMsg, DaysSince1970, SecondsSinceMidnight, Log, TripLog) ) {
tNMEA0183Msg NMEA0183Msg;
if ( !NMEA0183Msg.Init("VLW", "GP") ) return;
if ( !NMEA0183Msg.AddDoubleField(Log / 1852.0) ) return;
if ( !NMEA0183Msg.AddStrField("N") ) return;
if ( !NMEA0183Msg.AddDoubleField(TripLog / 1852.0) ) return;
if ( !NMEA0183Msg.AddStrField("N") ) return;
SendMessage(NMEA0183Msg);
}
}
//*****************************************************************************
void tN2kDataToNMEA0183::HandleRudder(const tN2kMsg & N2kMsg) {
unsigned char Instance;
tN2kRudderDirectionOrder RudderDirectionOrder;
double AngleOrder;
if ( ParseN2kRudder(N2kMsg, RudderPosition, Instance, RudderDirectionOrder, AngleOrder) ) {
if(Instance!=0) return;
tNMEA0183Msg NMEA0183Msg;
if ( !NMEA0183Msg.Init("RSA", "GP") ) return;
if ( !NMEA0183Msg.AddDoubleField(RudderPosition * radToDeg) ) return;
if ( !NMEA0183Msg.AddStrField("A") ) return;
if ( !NMEA0183Msg.AddDoubleField(0.0) ) return;
if ( !NMEA0183Msg.AddStrField("A") ) return;
SendMessage(NMEA0183Msg);
}
}
//*****************************************************************************
void tN2kDataToNMEA0183::HandleWaterTemp(const tN2kMsg & N2kMsg) {
unsigned char SID;
double OutsideAmbientAirTemperature;
double AtmosphericPressure;
if ( ParseN2kPGN130310(N2kMsg, SID, WaterTemperature, OutsideAmbientAirTemperature, AtmosphericPressure) ) {
tNMEA0183Msg NMEA0183Msg;
if ( !NMEA0183Msg.Init("MTW", "GP") ) return;
if ( !NMEA0183Msg.AddDoubleField(KelvinToC(WaterTemperature))) return;
if ( !NMEA0183Msg.AddStrField("C") ) return;
SendMessage(NMEA0183Msg);
}
}
| 12,637
|
C++
|
.cpp
| 290
| 37.737931
| 123
| 0.637231
|
AK-Homberger/M5Stack-NMEA-2000-Display-CAN-BUS
| 34
| 7
| 0
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,499
|
portabil.cpp
|
rokzitko_nrgljubljana/test/unit/portabil/portabil.cpp
|
#include <gtest/gtest.h>
#include <portabil.hpp>
#include <sstream>
using namespace NRG;
TEST(stream, parse_string) {
std::stringstream ss;
ss << "keyword: value\n" << "keyword2: value2\n";
ss << "Xkeyword3:Xvalue3Y\n";
{
auto res = parse_string(ss, "keyword"); // find the first occurrence!
EXPECT_TRUE(res);
EXPECT_EQ(res.value(), ": value");
}
{
auto res = parse_string(ss, "keyword2: ");
EXPECT_TRUE(res);
EXPECT_EQ(res.value(), "value2");
}
{
auto res = parse_string(ss, "keyword3");
EXPECT_TRUE(res);
EXPECT_EQ(res.value(), "X:Xvalue3Y");
}
{
auto res = parse_string(ss, "not_present");
EXPECT_FALSE(res);
}
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
| 785
|
C++
|
.cpp
| 32
| 21.375
| 70
| 0.638667
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,500
|
h5.cpp
|
rokzitko_nrgljubljana/test/unit/h5/h5.cpp
|
#include <gtest/gtest.h>
#include <type_traits>
#include <complex>
#include <h5.hpp>
#include "compare.hpp"
TEST(traits, is_same_v) { // NOLINT
{
auto res = std::is_same_v<int, int>;
EXPECT_TRUE(res);
}
{
auto res = std::is_same_v<void, void>;
EXPECT_TRUE(res);
}
{
auto res = std::is_same_v<int, void>;
EXPECT_FALSE(res);
}
}
TEST(h5dump, long_path) { // NOLINT
H5Easy::File file("long_path.h5", H5Easy::File::Overwrite);
const int w = 42;
const std::string path = "/this/is/a/long/path";
H5Easy::dump(file, path, w);
const auto r = H5Easy::load<int>(file, path);
EXPECT_EQ(w, r);
}
TEST(h5dump, scalar_int) { // NOLINT
H5Easy::File file("scalar_int.h5", H5Easy::File::Overwrite);
int w = 42;
H5Easy::dump(file, "/path", w);
auto r = H5Easy::load<int>(file, "/path");
EXPECT_EQ(w, r);
}
TEST(h5dump, scalar_size_t) { // NOLINT
H5Easy::File file("scalar_size_t.h5", H5Easy::File::Overwrite);
size_t w = 42;
H5Easy::dump(file, "/path", w);
auto r = H5Easy::load<size_t>(file, "/path");
EXPECT_EQ(w, r);
}
TEST(h5dump, scalar_double) { // NOLINT
H5Easy::File file("scalar_double.h5", H5Easy::File::Overwrite);
double w = 42.0;
H5Easy::dump(file, "/path", w);
auto r = H5Easy::load<double>(file, "/path");
EXPECT_EQ(w, r);
}
#ifdef H5_COMPLEX_IMPLEMENTED
TEST(h5dump, scalar_complex_double) { // NOLINT
H5Easy::File file("scalar_complex_double.h5", H5Easy::File::Overwrite);
std::complex<double> w = 42.0;
H5Easy::dump(file, "/path", w);
auto r = H5Easy::load<std::complex<double>>(file, "/path");
EXPECT_EQ(w, r);
}
#endif
TEST(h5dump, std_vector_double) { // NOLINT
H5Easy::File file("std_vector_double.h5", H5Easy::File::Overwrite);
std::vector w = {1.0, 2.0, 3.0};
H5Easy::dump(file, "/path", w);
auto r = H5Easy::load<std::vector<double>>(file, "/path");
EXPECT_EQ(w, r);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS(); // NOLINT
}
| 2,004
|
C++
|
.cpp
| 68
| 26.764706
| 73
| 0.644156
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,501
|
h5_Eigen.cpp
|
rokzitko_nrgljubljana/test/unit/h5/h5_Eigen.cpp
|
#include <gtest/gtest.h>
#include <type_traits>
#include <complex>
#include <Eigen/Dense>
#include <traits.hpp>
using namespace NRG;
#define H5_USE_EIGEN
#include <highfive/H5File.hpp>
#include <h5.hpp>
TEST(h5dump, eigen_dump_matrix) { // NOLINT
H5Easy::File file("eigen_matrix.h5", HighFive::File::Overwrite);
const auto nx = 2;
const auto ny = 3;
EigenMatrix<double> w(nx,ny);
double cnt = 1.0;
for (int x = 0; x < nx; x++) {
for (int y = 0; y < ny; y++) {
w(x,y) = cnt;
cnt = cnt + 1.0;
}
}
NRG::h5_dump_matrix(file, "/path", w);
auto r = H5Easy::load<EigenMatrix<double>>(file, "/path");
EXPECT_TRUE(w.isApprox(r));
}
TEST(h5dump, eigen_matrix_real_part_rect) { // NOLINT
H5Easy::File file("eigen_matrix_real_part_rect.h5", HighFive::File::Overwrite);
const auto nx = 2;
const auto ny = 3;
EigenMatrix<std::complex<double>> w(nx,ny);
auto cnt = std::complex<double>(1.0,1.0);
for (int x = 0; x < nx; x++) {
for (int y = 0; y < ny; y++) {
w(x,y) = cnt;
cnt = cnt + 1.0;
}
}
NRG::h5_dump_matrix(file, "/path", w);
auto r = H5Easy::load<EigenMatrix<double>>(file, "/path");
for (int x = 0; x < nx; x++)
for (int y = 0; y < ny; y++)
EXPECT_EQ(w(x,y).real(), r(x,y));
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS(); // NOLINT
}
| 1,385
|
C++
|
.cpp
| 47
| 26.234043
| 81
| 0.608728
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,502
|
basicio.cpp
|
rokzitko_nrgljubljana/test/unit/basicio/basicio.cpp
|
#include <gtest/gtest.h>
#include <sstream>
#include <traits.hpp>
#include <basicio.hpp>
#include "compare.hpp"
using namespace NRG;
TEST(basicio, to_string) {
auto res1 = to_string(std::complex(1.0,2.0));
EXPECT_EQ(res1, "(1,2)");
auto res2 = to_string(std::complex(1.5,2.5));
EXPECT_EQ(res2, "(1.5,2.5)");
}
TEST(basicio, inserters1) {
{
std::stringstream ss;
ss << std::make_pair(1,2);
EXPECT_EQ(ss.str(), "1 2");
}
{
std::stringstream ss;
ss << std::vector<int>({1,2,3,4});
EXPECT_EQ(ss.str(), "1 2 3 4 "); // trailing whitespace
}
{
std::stringstream ss;
std::set s = {1, 2, 3};
ss << s;
EXPECT_EQ(ss.str(), "1 2 3 "); // trailing ws
}
}
TEST(basicio, from_string) {
auto str = "12.3";
auto res = from_string<double>(str);
EXPECT_EQ(res, 12.3);
auto str1 = "true";
auto res1 = from_string<bool>(str1);
EXPECT_EQ(res1, true);
auto str2 = "TRUE";
auto res2 = from_string<bool>(str2);
EXPECT_EQ(res2, true);
auto str3 = "NOTTRUE";
auto res3 = from_string<bool>(str3);
EXPECT_EQ(res3, false);
}
TEST(basicio, output) {
auto res = prec(1.23456, 1);
EXPECT_EQ(res, "1.2");
auto res3 = prec3(1.23456);
EXPECT_EQ(res3, "1.235"); // rounding!
EXPECT_EQ(negligible_imag_part(std::complex(1.0,0.1)), false);
EXPECT_EQ(negligible_imag_part(std::complex(1.0,1e-14)), true);
EXPECT_EQ(negligible_imag_part(std::complex(1.0,1e-14),1e-16), false);
}
TEST(basicio, count_words_in_string) {
EXPECT_EQ(count_words_in_string("one two three four a"s), 5);
EXPECT_EQ(count_words_in_string(""s), 0);
}
TEST(basicio, get_dims) {
auto file = safe_open_for_reading("txt/matrix.txt");
auto const [dim1, dim2] = get_dims(file);
EXPECT_EQ(dim1, 3);
EXPECT_EQ(dim2, 4);
auto file_err = safe_open_for_reading("txt/matrix_err.txt");
EXPECT_THROW(get_dims(file_err), std::runtime_error);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
| 1,991
|
C++
|
.cpp
| 69
| 25.985507
| 72
| 0.645921
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,503
|
matrixio.cpp
|
rokzitko_nrgljubljana/test/unit/basicio/matrixio.cpp
|
#include <gtest/gtest.h>
#include <sstream>
#include <traits.hpp>
#include <io.hpp>
#include "compare.hpp"
using namespace NRG;
TEST(io, read_matrix){
Eigen::Matrix<double, 3, 4> ref_matrix;
ref_matrix << 31,41,53,46,
12,5,1,41,
5,2,4,7;
auto matrix = read_matrix("txt/matrix.txt");
EXPECT_TRUE(matrix.isApprox(ref_matrix));
}
TEST(io, save_matrix){
auto matrix = read_matrix("txt/matrix.txt");
save_matrix("txt/matrix_temp.txt", matrix);
auto matrix_temp = read_matrix("txt/matrix_temp.txt");
EXPECT_TRUE(matrix.isApprox(matrix_temp));
std::remove("txt/matrix_temp.txt");
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
| 735
|
C++
|
.cpp
| 25
| 26
| 56
| 0.677557
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,505
|
numerics.cpp
|
rokzitko_nrgljubljana/test/unit/numerics/numerics.cpp
|
#include <gtest/gtest.h>
// Use the matrix backend settings from traits.hpp
#include <traits.hpp>
#include <numerics.hpp>
#include <io.hpp>
#include "compare.hpp"
using namespace NRG;
using namespace std::complex_literals;
TEST(numerics, reim) {
const auto [r, i] = reim(std::complex(1.0, 2.0));
EXPECT_EQ(r, 1.0);
EXPECT_EQ(i, 2.0);
}
/*
TEST(numerics, reim_non_const) {
auto z = std::complex(1.0, 2.0);
auto [r, i] = reim(z);
EXPECT_EQ(r, 1.0);
EXPECT_EQ(i, 2.0);
r = 3.0;
i = 4.0;
EXPECT_EQ(r, 3.0);
EXPECT_EQ(i, 4.0);
EXPECT_EQ(z.real(), 3.0);
EXPECT_EQ(z.imag(), 4.0);
}
*/
TEST(numerics, sum2){
std::vector v = {std::pair{4,12}, std::pair{51,123}, std::pair{412,441}};
EXPECT_EQ(sum2(v), 576);
std::vector<std::pair<int,int>> empty_vec = {};
EXPECT_EQ(sum2(empty_vec), 0);
}
TEST(numerics, conj_me){
EXPECT_EQ(conj_me(std::complex(5.0,8.0)), std::complex(5.0,-8.0));
EXPECT_EQ(conj_me(5.0), 5.0);
}
TEST(numerica, empty_matrix) {
const auto m = empty_matrix<double>();
EXPECT_EQ(size1(m), 0);
EXPECT_EQ(size2(m), 0);
}
TEST(numerics, finite_size) {
const auto m = empty_matrix<double>();
EXPECT_FALSE(finite_size(m));
const auto f = generate_matrix<double>(1,1);
EXPECT_TRUE(finite_size(f));
}
TEST(numerics, has_lesseq_rows) {
const auto a = generate_matrix<double>(2,4);
const auto b = generate_matrix<double>(3,4);
EXPECT_TRUE(has_lesseq_rows(a,b));
EXPECT_FALSE(has_lesseq_rows(b,a));
const auto c = generate_matrix<double>(2,3);
EXPECT_FALSE(has_lesseq_rows(a,c));
}
TEST(numerics, is_square) {
const auto sq = generate_matrix<double>(2,2);
EXPECT_TRUE(is_square(sq));
const auto re = generate_matrix<double>(2,3);
EXPECT_FALSE(is_square(re));
}
TEST(numerics, is_real) {
EXPECT_TRUE(is_real(2.0));
EXPECT_TRUE(is_real(std::complex(2.0,0.0)));
EXPECT_FALSE(is_real(std::complex(2.0,1.0)));
}
TEST(numerics, check_is_matrix_upper) {
auto m = generate_matrix<double>(2,2);
m(0,0) = m(0,1) = m(1,0) = m(1,1) = 1.0;
EXPECT_FALSE(is_matrix_upper(m));
m(1,0) = 0.0;
EXPECT_TRUE(is_matrix_upper(m));
}
TEST(numerics, bucket){
std::vector v = {std::pair{4.0,12.0}, std::pair{51.0,123.0}, std::pair{412.0,441.0}};
bucket sum(v);
EXPECT_EQ(sum, 576);
sum += 10;
EXPECT_EQ(sum, 586);
}
TEST(numerics, is_even_is_odd){
const int odd = 3;
const int even = 4;
EXPECT_TRUE(is_even(even));
EXPECT_FALSE(is_odd(even));
EXPECT_TRUE(is_odd(odd));
EXPECT_FALSE(is_even(odd));
}
TEST(numerics, my_fcmp){
const double a = 1.0;
const double b = 1.0001;
EXPECT_EQ(my_fcmp(a, b, 0.1, 0.00015), 0);
EXPECT_EQ(my_fcmp(a, b, 0.1, 0.00001), -1);
}
TEST(numerics, num_equal) {
const double a = 1.0;
const double b = a + 1e-4;
EXPECT_FALSE(num_equal(a,b));
EXPECT_TRUE(num_equal(a,b,1e-2));
}
TEST(numerics, chop) {
EXPECT_DOUBLE_EQ(chop(1e-10), 0.0);
EXPECT_DOUBLE_EQ(chop(1.0), 1.0);
}
TEST(numerics, Power) {
EXPECT_EQ(Power(2, 0), 1);
EXPECT_EQ(Power(2, 1), 2);
EXPECT_EQ(Power(2, 2), 4);
EXPECT_EQ(Power(2, 3), 8);
EXPECT_DOUBLE_EQ(Power(2, 0.5), std::sqrt(2.0));
}
TEST(numerics, are_conjugate_re) {
const double x = 1.0;
EXPECT_TRUE(are_conjugate(x,x));
}
TEST(numerics, are_conjugate_cmpl) {
const auto z = std::complex<double>(1.0, 2.0);
const auto zbar = std::conj(z);
EXPECT_TRUE(are_conjugate(z,zbar));
EXPECT_FALSE(are_conjugate(z,z));
}
TEST(numerics, is_unitary_real) {
using T = double;
auto i = generate_matrix<T>(2,2);
i(0,0) = i(1,1) = 1.0;
i(0,1) = i(1,0) = 0.0;
EXPECT_TRUE(is_unitary<T>(i));
auto z = generate_matrix<T>(2,2);
z(0,0) = z(0,1) = z(1,0) = z(1,1) = 0.0;
EXPECT_FALSE(is_unitary<T>(z));
auto m = generate_matrix<T>(2,2);
m(0,0) = 1.0;
m(0,1) = 1.0/sqrt(2.0);
m(1,0) = 0.0;
m(1,1) = 1.0/sqrt(2.0);
EXPECT_FALSE(is_unitary<T>(m));
auto t = generate_matrix<T>(2,2);
t(0,0) = 1.0;
t(0,1) = 0.0;
t(1,0) = 1.0/sqrt(2.0);
t(1,1) = 1.0/sqrt(2.0);
EXPECT_FALSE(is_unitary<T>(t));
}
TEST(numerics, psgn) {
EXPECT_DOUBLE_EQ(psgn(0), 1.0);
EXPECT_DOUBLE_EQ(psgn(1), -1.0);
}
TEST(numerics, frobenius_norm_1) {
auto m1 = generate_matrix<double>(1,1);
m1(0,0) = 1;
EXPECT_DOUBLE_EQ(frobenius_norm(m1), 1.0);
}
TEST(numerics, frobenius_norm_2) {
auto m2 = generate_matrix<double>(2,2);
m2(0,0) = m2(0,1) = m2(1,0) = m2(1,1) = 1.0;
EXPECT_DOUBLE_EQ(frobenius_norm(m2), 4.0);
}
TEST(numerics, generate_matrix) {
const auto m1 = generate_matrix<double>(2,3);
EXPECT_EQ(size1(m1), 2);
EXPECT_EQ(size2(m1), 3);
}
TEST(numerics, zero_matrix) {
const auto m1 = NRG::zero_matrix<double>(2,3);
EXPECT_EQ(size1(m1), 2);
EXPECT_EQ(size2(m1), 3);
EXPECT_DOUBLE_EQ(m1(1,2), 0.0);
}
TEST(numerics, id_matrix) {
const auto m1 = id_matrix<double>(2);
EXPECT_EQ(size1(m1), 2);
EXPECT_EQ(size2(m1), 2);
EXPECT_DOUBLE_EQ(m1(0,0), 1.0);
EXPECT_DOUBLE_EQ(m1(1,1), 1.0);
EXPECT_DOUBLE_EQ(m1(0,1), 0.0);
EXPECT_DOUBLE_EQ(m1(1,0), 0.0);
}
TEST(numerics, dump_matrix) {
const auto m2 = id_matrix<double>(2);
std::ostringstream str;
dump_matrix(m2, str);
//
}
TEST(numerics, dump_diagonal_matrix) {
const auto m2 = id_matrix<double>(2);
std::ostringstream str;
dump_diagonal_matrix(m2, size1(m2), str);
EXPECT_EQ(str.str(), "1 1 \n");
}
TEST(numerics, csqrt) {
const auto z = std::complex<double>(4.0, 0.0);
const auto r = csqrt(z);
EXPECT_DOUBLE_EQ(r.real(), 2.0);
}
TEST(numerics, trans) {
auto m = generate_matrix<double>(2,2);
m(0,0) = m(1,1) = 0.0;
m(0,1) = 2.0;
m(1,0) = 3.0;
const auto mt = trans(m);
EXPECT_DOUBLE_EQ(mt(0,1), 3.0);
EXPECT_DOUBLE_EQ(mt(1,0), 2.0);
}
TEST(numerics, herm) {
auto m = generate_matrix<std::complex<double>>(2,2);
m(0,0) = m(1,1) = 0.0;
m(0,1) = 2.0+1.0i;
m(1,0) = 3.0+4.0i;
const auto mt = herm(m);
EXPECT_EQ(mt(0,1), std::complex(3.0,-4.0));
EXPECT_EQ(mt(1,0), std::complex(2.0,-1.0));
}
TEST(numerics, trace_real) {
auto m = generate_matrix<std::complex<double>>(2,2);
m(0,0) = 1.0;
m(1,1) = 2.0;
EXPECT_DOUBLE_EQ(trace_real(m), 3.0);
}
TEST(numerics, trace_contract) {
auto a = generate_matrix<double>(2,2);
a(0,0) = a(0,1) = a(1,0) = a(1,1) = 1.0;
EXPECT_DOUBLE_EQ(trace_contract(a, a, size1(a)), 4.0);
a(0,0) = 1.0;
a(0,1) = 2.0;
a(1,0) = 3.0;
a(1,1) = 4.0;
EXPECT_DOUBLE_EQ(trace_contract(a, a, size1(a)), 29.0);
}
TEST(numerics, trace_exp) {
std::vector<double> v(2);
v[0] = 1.0;
v[1] = 2.0;
auto m = generate_matrix<double>(2,2);
m(0,0) = m(1,1) = 2.0;
EXPECT_DOUBLE_EQ(trace_exp(v, m, 2.0), 2.0*(exp(-2.0)+exp(-4.0)));
}
TEST(numerics, sum_of_exp) {
std::vector<double> v(2);
v[0] = 1.0;
v[1] = 2.0;
EXPECT_DOUBLE_EQ(sum_of_exp(v, 2.0), exp(-2.0) + exp(-4.0));
}
TEST(numerics, trim_matrix) {
auto a = generate_matrix<double>(2,2);
a(0,0) = 1.0;
a(0,1) = 2.0;
a(1,0) = 3.0;
a(1,1) = 4.0;
const auto b22 = trim_matrix(a, 2, 2);
EXPECT_EQ(size1(b22), 2);
EXPECT_EQ(size2(b22), 2);
EXPECT_DOUBLE_EQ(a(0,0), b22(0,0));
EXPECT_DOUBLE_EQ(a(0,1), b22(0,1));
EXPECT_DOUBLE_EQ(a(1,0), b22(1,0));
EXPECT_DOUBLE_EQ(a(1,1), b22(1,1));
const auto b12 = trim_matrix(a, 1, 2);
EXPECT_EQ(size1(b12), 1);
EXPECT_EQ(size2(b12), 2);
EXPECT_DOUBLE_EQ(a(0,0), b12(0,0));
EXPECT_DOUBLE_EQ(a(0,1), b12(0,1));
const auto b21 = trim_matrix(a, 2, 1);
EXPECT_EQ(size1(b21), 2);
EXPECT_EQ(size2(b21), 1);
EXPECT_DOUBLE_EQ(a(0,0), b21(0,0));
EXPECT_DOUBLE_EQ(a(1,0), b21(1,0));
const auto b11 = trim_matrix(a, 1, 1);
EXPECT_EQ(size1(b11), 1);
EXPECT_EQ(size2(b11), 1);
EXPECT_DOUBLE_EQ(a(0,0), b11(0,0));
}
TEST(numerics, prod_fit_left) {
auto a = generate_matrix<double>(2,2);
auto b = generate_matrix<double>(1,1);
a(0,0) = a(0,1) = a(1,0) = a(1,1) = 2.0;
b(0,0) = 2.0;
const auto m = prod_fit_left(a, b);
EXPECT_EQ(size1(m), 2);
EXPECT_EQ(size2(m), 1);
EXPECT_DOUBLE_EQ(m(0,0), 4.0);
EXPECT_DOUBLE_EQ(m(1,0), 4.0);
}
TEST(numerics, prod_fit_left_22) {
auto a = generate_matrix<double>(2,2);
a(0,0) = 1.0;
a(0,1) = 2.0;
a(1,0) = 3.0;
a(1,1) = 4.0;
const auto m = prod_fit_left(a, a);
EXPECT_EQ(size1(m), 2);
EXPECT_EQ(size2(m), 2);
EXPECT_DOUBLE_EQ(m(0,0), 7.0);
EXPECT_DOUBLE_EQ(m(0,1), 10.0);
EXPECT_DOUBLE_EQ(m(1,0), 15.0);
EXPECT_DOUBLE_EQ(m(1,1), 22.0);
}
TEST(numerics, prod_fit_right) {
auto a = generate_matrix<double>(1,1);
auto b = generate_matrix<double>(2,2);
a(0,0) = 2.0;
b(0,0) = b(0,1) = b(1,0) = b(1,1) = 2.0;
const auto m = prod_fit_right(a, b);
EXPECT_EQ(size1(m), 1);
EXPECT_EQ(size2(m), 2);
EXPECT_DOUBLE_EQ(m(0,0), 4.0);
EXPECT_DOUBLE_EQ(m(0,1), 4.0);
}
TEST(numerics, prod_fit_right_22) {
auto a = generate_matrix<double>(2,2);
a(0,0) = 1.0;
a(0,1) = 2.0;
a(1,0) = 3.0;
a(1,1) = 4.0;
const auto m = prod_fit_right(a, a);
EXPECT_EQ(size1(m), 2);
EXPECT_EQ(size2(m), 2);
EXPECT_DOUBLE_EQ(m(0,0), 7.0);
EXPECT_DOUBLE_EQ(m(0,1), 10.0);
EXPECT_DOUBLE_EQ(m(1,0), 15.0);
EXPECT_DOUBLE_EQ(m(1,1), 22.0);
}
TEST(numerics, prod_fit) {
auto a = generate_matrix<double>(2,2);
a(0,0) = 1.0;
a(0,1) = 2.0;
a(1,0) = 3.0;
a(1,1) = 4.0;
const auto m = prod_fit(a, a);
EXPECT_EQ(size1(m), 2);
EXPECT_EQ(size2(m), 2);
EXPECT_DOUBLE_EQ(m(0,0), 7.0);
EXPECT_DOUBLE_EQ(m(0,1), 10.0);
EXPECT_DOUBLE_EQ(m(1,0), 15.0);
EXPECT_DOUBLE_EQ(m(1,1), 22.0);
}
TEST(numerics, prod_adj_fit_left) {
auto a = generate_matrix<double>(2,2);
auto b = generate_matrix<double>(1,1);
a(0,0) = a(0,1) = a(1,0) = a(1,1) = 2.0;
b(0,0) = 2.0;
const auto m = prod_adj_fit_left(a, b);
EXPECT_EQ(size1(m), 2);
EXPECT_EQ(size2(m), 1);
EXPECT_DOUBLE_EQ(m(0,0), 4.0);
EXPECT_DOUBLE_EQ(m(1,0), 4.0);
}
TEST(numerics, prod_adj_fit_left_re) {
auto a = generate_matrix<double>(2,2);
auto b = generate_matrix<double>(2,2);
a(0,0) = 1.0;
a(0,1) = 2.0;
a(1,0) = 3.0;
a(1,1) = 4.0;
b(0,0) = 5.0;
b(0,1) = 6.0;
b(1,0) = 7.0;
b(1,1) = 8.0;
const auto m = prod_adj_fit_left(a, b);
EXPECT_EQ(size1(m), 2);
EXPECT_EQ(size2(m), 2);
EXPECT_DOUBLE_EQ(m(0,0), 26.0);
EXPECT_DOUBLE_EQ(m(0,1), 30.0);
EXPECT_DOUBLE_EQ(m(1,0), 38.0);
EXPECT_DOUBLE_EQ(m(1,1), 44.0);
}
TEST(numerics, prod_adj_fit_left_cx) {
using T = std::complex<double>;
auto a = generate_matrix<T>(2,2);
auto b = generate_matrix<T>(2,2);
a(0,0) = T(0.0,1.0);
a(0,1) = T(0.0,2.0);
a(1,0) = T(0.0,3.0);
a(1,1) = T(0.0,4.0);
b(0,0) = 5.0;
b(0,1) = 6.0;
b(1,0) = 7.0;
b(1,1) = 8.0;
const auto m = prod_adj_fit_left(a, b);
EXPECT_EQ(size1(m), 2);
EXPECT_EQ(size2(m), 2);
EXPECT_DOUBLE_EQ(m(0,0).imag(), -26.0);
EXPECT_DOUBLE_EQ(m(0,1).imag(), -30.0);
EXPECT_DOUBLE_EQ(m(1,0).imag(), -38.0);
EXPECT_DOUBLE_EQ(m(1,1).imag(), -44.0);
}
TEST(numerics, chit_weight) {
EXPECT_DOUBLE_EQ(chit_weight(2.0, 1.0, 1.0), exp(-1.0)-exp(-2.0));
EXPECT_DOUBLE_EQ(chit_weight(2.0, 1.0, 0.5), (exp(-0.5)-exp(-1.0))/0.5);
EXPECT_DOUBLE_EQ(chit_weight(1.0, 1.0, 1.0), exp(-1.0));
EXPECT_DOUBLE_EQ(chit_weight(3.0, 1.0, 1.0), (exp(-1.0)-exp(-3.0))/2.0);
EXPECT_DOUBLE_EQ(chit_weight(3.0, 3.0, 1.0), exp(-3.0));
}
TEST(numerics, save_r) {
auto m1 = generate_matrix<double>(10,20);
for (size_t i = 0; i < 10*20; i++) m1(i) = (double)i;
{
std::ofstream f("test", std::ios::binary | std::ios::out);
EXPECT_TRUE(f);
boost::archive::binary_oarchive oa(f);
NRG::save(oa, m1);
EXPECT_FALSE(f.bad());
}
{
std::ifstream f("test", std::ios::binary | std::ios::in);
EXPECT_TRUE(f);
boost::archive::binary_iarchive ia(f);
const auto m2 = NRG::load<double>(ia);
EXPECT_TRUE(m1.isApprox(m2));
EXPECT_FALSE(f.bad());
}
}
TEST(numerics, save_c) {
auto m1 = generate_matrix<std::complex<double>>(10,20);
for (size_t i = 0; i < 10*20; i++) m1(i) = (double)i + 1.0i;
{
std::ofstream f("test", std::ios::binary | std::ios::out);
EXPECT_TRUE(f);
boost::archive::binary_oarchive oa(f);
NRG::save(oa, m1);
EXPECT_FALSE(f.bad());
}
{
std::ifstream f("test", std::ios::binary | std::ios::in);
EXPECT_TRUE(f);
boost::archive::binary_iarchive ia(f);
const auto m2 = NRG::load<std::complex<double>>(ia);
EXPECT_TRUE(m1.isApprox(m2));
EXPECT_FALSE(f.bad());
}
}
TEST(numerics, product) {
auto a = generate_matrix<double>(2,2);
auto b = generate_matrix<double>(2,2);
a(0,0) = a(0,1) = a(1,0) = a(1,1) = 1;
b(0,0) = b(0,1) = b(1,0) = b(1,1) = 1;
auto r = NRG::zero_matrix<double>(2,2);
auto ref = generate_matrix<double>(2,2);
ref(0,0) = ref(0,1) = ref(1,0) = ref(1,1) = 2;
product<double>(r, 1.0, a, b);
EXPECT_TRUE(r.isApprox(ref));
}
TEST(numerics, transform) {
auto a = generate_matrix<double>(2,2);
auto b = generate_matrix<double>(2,2);
auto c = generate_matrix<double>(2,2);
a(0,0) = a(0,1) = a(1,0) = a(1,1) = 1;
b(0,0) = b(0,1) = b(1,0) = b(1,1) = 1;
c(0,0) = c(0,1) = c(1,0) = c(1,1) = 1;
auto r = NRG::zero_matrix<double>(2,2);
auto ref = generate_matrix<double>(2,2);
ref(0,0) = ref(0,1) = ref(1,0) = ref(1,1) = 4;
transform<double>(r, 1.0, a, b, c);
EXPECT_TRUE(r.isApprox(ref));
}
TEST(numerics, rotate) {
auto a = generate_matrix<double>(2,2);
auto b = generate_matrix<double>(2,2);
a(0,0) = a(0,1) = a(1,0) = a(1,1) = 1;
b(0,0) = b(0,1) = b(1,0) = b(1,1) = 1;
auto r = NRG::zero_matrix<double>(2,2);
auto ref = generate_matrix<double>(2,2);
ref(0,0) = ref(0,1) = ref(1,0) = ref(1,1) = 4;
rotate<double>(r, 1.0, a, b);
EXPECT_TRUE(r.isApprox(ref));
}
TEST(numerics, matrix_prod) {
auto a = generate_matrix<double>(2,2);
auto b = generate_matrix<double>(2,2);
a(0,0) = a(0,1) = a(1,0) = a(1,1) = 1;
b(0,0) = b(0,1) = b(1,0) = b(1,1) = 1;
auto ref = generate_matrix<double>(2,2);
ref(0,0) = ref(0,1) = ref(1,0) = ref(1,1) = 2;
const auto r = matrix_prod<double>(a, b);
EXPECT_TRUE(r.isApprox(ref));
}
TEST(numerics, matrix_adj_prod) {
auto a = generate_matrix<double>(2,2);
auto b = generate_matrix<double>(2,2);
a(0,0) = a(0,1) = a(1,0) = a(1,1) = 1;
b(0,0) = b(0,1) = b(1,0) = b(1,1) = 1;
auto ref = generate_matrix<double>(2,2);
ref(0,0) = ref(0,1) = ref(1,0) = ref(1,1) = 2;
const auto r = matrix_adj_prod<double>(a, b);
EXPECT_TRUE(r.isApprox(ref));
}
| 14,282
|
C++
|
.cpp
| 485
| 26.678351
| 87
| 0.600568
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,506
|
numerics_Eigen.cpp
|
rokzitko_nrgljubljana/test/unit/numerics/numerics_Eigen.cpp
|
#include <gtest/gtest.h>
#define USE_EIGEN
#include <traits.hpp>
#include <numerics.hpp>
#include "compare.hpp"
using namespace NRG;
using namespace std::complex_literals;
TEST(Eigen, default_constructor) {
EigenMatrix<double> md;
EigenMatrix<std::complex<double>> mc;
}
TEST(eigen, basic_vector) {
Eigen::Vector3i a(5,7,9);
std::vector b = {5,7,9};
compare(a,b);
Eigen::Vector3i c;
c << 5,7,9;
compare(c,b);
}
TEST(eigen, matrix_ops) {
EigenMatrix<double> m(2,2);
EXPECT_EQ(size1(m), 2);
EXPECT_EQ(size2(m), 2);
m(0,0) = 1;
m(0,1) = 2;
m(1,0) = 3;
m(1,1) = 4;
EXPECT_DOUBLE_EQ(m(0,0), 1);
EXPECT_DOUBLE_EQ(m(0,1), 2);
EXPECT_DOUBLE_EQ(m(1,0), 3);
EXPECT_DOUBLE_EQ(m(1,1), 4);
}
TEST(numerics_Eigen, zero_matrix) {
const size_t dim1 = 4;
const size_t dim2 = 2;
const size_t dim3 = 3;
EigenMatrix<double> zero_m1 = NRG::zero_matrix<double>(dim1,dim2);
EigenMatrix<double> zero_m2 = NRG::zero_matrix<double>(dim3);
ASSERT_EQ(zero_m1.rows(), dim1);
ASSERT_EQ(zero_m1.cols(), dim2);
ASSERT_EQ(zero_m2.rows(), dim3);
ASSERT_EQ(zero_m2.cols(), dim3);
for(size_t i = 0; i < dim1; i++)
for(size_t j = 0; j < dim2; j++)
EXPECT_EQ(zero_m1(i,j), 0);
for(size_t i = 0; i < dim3; i++)
for(size_t j = 0; j < dim3; j++)
EXPECT_EQ(zero_m2(i,j), 0);
// Test modification
zero_m1(0,0) = 1;
EXPECT_EQ(zero_m1(0,0), 1);
}
TEST(numerics_Eigen, trace_exp_real) {
using T = double;
const size_t N = 3;
Eigen::VectorX<T> v(N);
for(size_t i = 0; i < N ; i++)
v(i) = i + 1;
EigenMatrix<T> m(N, N);
for(size_t i = 0; i < N*N; i++)
m(i) = i + 1;
T expected = 0;
for(size_t i = 0; i < N; i++)
expected += exp(-2.5 * v[i]) * m(i, i);
compare(expected, trace_exp(v, m, 2.5));
}
TEST(numerics_Eigen, trace_exp_complex) {
using T = std::complex<double>;
const size_t N = 3;
Eigen::VectorX<T> v(N);
for(size_t j = 0; j < N ; j++)
v(j) = j + 1.0 + 3i * (double)j;
EigenMatrix<T> m(N, N);
for(size_t j = 0; j < N*N; j++)
m(j) = j + 3.0 + 2i * (double)j;
T expected = 0;
for(size_t i = 0; i < N; i++)
expected += exp(- 2.5 * v(i)) * m(i, i);
compare(expected, trace_exp(v, m, 2.5));
}
TEST(numerics_Eigen, submatrix1) {
const size_t N = 3;
EigenMatrix<double> a(N,N);
for(size_t i = 0; i < N; i++)
for(size_t j = 0; j < N; j++)
a(i, j) = i+10*j;
EigenMatrix<double> dg(N-1, N-1);
dg(0,0) = 0;
dg(0,1) = 10;
dg(1,0) = 1;
dg(1,1) = 11;
const EigenMatrix<double> dg_result = submatrix(a, {0, 2}, {0, 2});
EXPECT_TRUE(dg.isApprox(dg_result));
}
TEST(numerics_Eigen, submatrix2) {
EigenMatrix<double> m(2,2);
m(0,0) = m(0,1) = m(1,0) = m(1,1) = 0;
auto sub = submatrix(m, {1,2}, {1,2});
EXPECT_EQ(size1(sub), 1);
EXPECT_EQ(size2(sub), 1);
sub(0,0) = 1;
EXPECT_DOUBLE_EQ(m(1,1), 1);
}
TEST(numerics_Eigen, submatrix3) {
using T = std::complex<double>;
const size_t dim1 = 15;
const size_t dim2 = 25;
EigenMatrix<T> m(dim1, dim2);
EXPECT_EQ(size1(m), dim1);
EXPECT_EQ(size2(m), dim2);
for (size_t i = 0; i < dim1; i++)
for (size_t j = 0; j < dim2; j++)
m(i, j) = T(i, j);
for (size_t offset1 = 0; offset1 < dim1; offset1++) {
for (size_t offset2 = 0; offset2 < dim2; offset2++) {
for (size_t sz1 = 0; sz1 < std::min(dim1, dim1-offset1); sz1++) {
for (size_t sz2 = 0; sz2 < std::min(dim2, dim2-offset2); sz2++) {
const auto sm1 = m.block(offset1, offset2, sz1, sz2);
//EXPECT_EQ(size1(sm1), sz1);
//EXPECT_EQ(size2(sm1), sz2);
for (size_t i = 0; i < sz1; i++)
for (size_t j = 0; j < sz2; j++)
EXPECT_EQ(sm1(i,j), T(offset1+i, offset2+j));
const auto sm2 = submatrix_const(m, {offset1, offset1+sz1}, {offset2, offset2+sz2});
EXPECT_EQ(size1(sm2), sz1);
EXPECT_EQ(size2(sm2), sz2);
for (size_t i = 0; i < sz1; i++)
for (size_t j = 0; j < sz2; j++)
EXPECT_EQ(sm2(i,j), T(offset1+i, offset2+j));
}
}
}
}
}
TEST(numerics_Eigen, data_r) {
EigenMatrix<double> m(2, 3);
m(0,0) = 1.;
m(0,1) = 2.;
m(0,2) = 3.;
m(1,0) = 4.;
m(1,1) = 5.;
m(1,2) = 6.;
double * d = data(m);
if (is_row_ordered(m)) {
EXPECT_EQ(*(d+0), 1.);
EXPECT_EQ(*(d+1), 2.);
EXPECT_EQ(*(d+2), 3.);
EXPECT_EQ(*(d+3), 4.);
EXPECT_EQ(*(d+4), 5.);
EXPECT_EQ(*(d+5), 6.);
} else {
EXPECT_EQ(*(d+0), 1.);
EXPECT_EQ(*(d+1), 4.);
EXPECT_EQ(*(d+2), 2.);
EXPECT_EQ(*(d+3), 5.);
EXPECT_EQ(*(d+4), 3.);
EXPECT_EQ(*(d+5), 6.);
}
}
TEST(numerics_Eigen, data_c) {
EigenMatrix<std::complex<double>> m(2, 3);
m(0,0) = 1.*(1.+1.i);
m(0,1) = 2.*(1.+1.i);
m(0,2) = 3.*(1.+1.i);
m(1,0) = 4.*(1.+1.i);
m(1,1) = 5.*(1.+1.i);
m(1,2) = 6.*(1.+1.i);
std::complex<double> * d = data(m);
if (is_row_ordered(m)) {
EXPECT_EQ(*(d+0), 1.*(1.+1.i));
EXPECT_EQ(*(d+1), 2.*(1.+1.i));
EXPECT_EQ(*(d+2), 3.*(1.+1.i));
EXPECT_EQ(*(d+3), 4.*(1.+1.i));
EXPECT_EQ(*(d+4), 5.*(1.+1.i));
EXPECT_EQ(*(d+5), 6.*(1.+1.i));
} else {
EXPECT_EQ(*(d+0), 1.*(1.+1.i));
EXPECT_EQ(*(d+1), 4.*(1.+1.i));
EXPECT_EQ(*(d+2), 2.*(1.+1.i));
EXPECT_EQ(*(d+3), 5.*(1.+1.i));
EXPECT_EQ(*(d+4), 3.*(1.+1.i));
EXPECT_EQ(*(d+5), 6.*(1.+1.i));
}
}
| 5,339
|
C++
|
.cpp
| 184
| 25.059783
| 94
| 0.534644
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,507
|
values.cpp
|
rokzitko_nrgljubljana/test/unit/eigen/values.cpp
|
#include <string>
#include <sstream>
#include <gtest/gtest.h>
#include "compare.hpp"
#include "test_common.hpp"
#include <traits.hpp>
#include <eigen.hpp>
using namespace NRG;
TEST(Values, access) { // NOLINT
Values<double> values;
std::vector v = {1.0, 2.0, 3.0, 4.0, 5.0};
values.set(v);
EXPECT_EQ(values.size(), 5);
EXPECT_DOUBLE_EQ(values.lowest_rel(), 1.0);
EXPECT_DOUBLE_EQ(values.rel(0), 1.0);
EXPECT_DOUBLE_EQ(values.rel(4), 5.0);
values.set_shift(1.0);
EXPECT_DOUBLE_EQ(values.rel_zero(0), 0.0);
EXPECT_DOUBLE_EQ(values.rel_zero(4), 4.0);
values.set_scale(2.0);
EXPECT_DOUBLE_EQ(values.abs(0), 2.0);
EXPECT_DOUBLE_EQ(values.abs(4), 10.0);
EXPECT_DOUBLE_EQ(values.abs_zero(0), 0.0);
EXPECT_DOUBLE_EQ(values.abs_zero(1), 2.0);
EXPECT_DOUBLE_EQ(values.abs_zero(2), 4.0);
EXPECT_DOUBLE_EQ(values.abs_zero(3), 6.0);
EXPECT_DOUBLE_EQ(values.abs_zero(4), 8.0);
values.set_T_shift(1.0);
EXPECT_DOUBLE_EQ(values.abs_T(0), 1.0);
EXPECT_DOUBLE_EQ(values.abs_T(1), 3.0);
// values.set_GS_energy(0.5);
// EXPECT_DOUBLE_EQ(values.absG(0), 1.5);
// EXPECT_DOUBLE_EQ(values.absG(4), 9.5);
const auto vv = values.all_rel();
VECTOR_DOUBLE_EQ(v, vv);
const auto vz = values.all_rel_zero();
EXPECT_DOUBLE_EQ(vz[0], 0.0);
EXPECT_DOUBLE_EQ(vz[1], 1.0);
EXPECT_DOUBLE_EQ(vz[2], 2.0);
EXPECT_DOUBLE_EQ(vz[3], 3.0);
EXPECT_DOUBLE_EQ(vz[4], 4.0);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS(); // NOLINT
}
| 1,516
|
C++
|
.cpp
| 46
| 30.413043
| 45
| 0.667806
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,508
|
eigenapi.cpp
|
rokzitko_nrgljubljana/test/unit/eigen/eigenapi.cpp
|
#include <string>
#include <sstream>
#include <gtest/gtest.h>
#include "compare.hpp"
#include "test_common.hpp"
#include <traits.hpp>
#include <eigen.hpp>
using namespace NRG;
TEST(RawEigen, constructor) { // NOLINT
{
NRG::RawEigen<double> e;
EXPECT_EQ(e.getnrcomputed(), 0);
EXPECT_EQ(e.getdim(), 0);
}
{
NRG::RawEigen<double> e(2,3);
EXPECT_EQ(e.getnrcomputed(), 2);
EXPECT_EQ(e.getdim(), 3);
}
}
TEST(Eigen, get) { // NOLINT
NRG::Eigen<double> e(5,8);
e.truncate_prepare(2);
EXPECT_EQ(e.getnrcomputed(), 5);
EXPECT_EQ(e.getdim(), 8);
EXPECT_EQ(e.getnrall(), 5);
EXPECT_EQ(e.getnrkept(), 2);
EXPECT_EQ(e.getnrdiscarded(), 3);
EXPECT_EQ(e.all(), range0(5));
EXPECT_EQ(e.kept(), range0(2));
EXPECT_EQ(e.discarded(), boost::irange(2,5) );
EXPECT_EQ(e.getnrstored(), 5);
EXPECT_EQ(e.stored(), range0(5));
}
using EVEC = evec_traits<double>;
TEST(Eigen, diagonal) { // NOLINT
NRG::Eigen<double> e(3,3);
EVEC v = { 1.0, 2.0, 3.0 };
e.diagonal(v);
EXPECT_DOUBLE_EQ(e.values.rel(0), 1.0);
const auto m = e.vectors.get();
EXPECT_DOUBLE_EQ(m(0,0), 1.0); // identity matrix
EXPECT_DOUBLE_EQ(m(0,1), 0.0);
EXPECT_DOUBLE_EQ(m(1,1), 1.0);
EXPECT_DOUBLE_EQ(m(2,2), 1.0);
}
TEST(Eigen, constructor_diagonal) { // NOLINT
EVEC v = { 1.0, 2.0, 3.0 };
NRG::Eigen<double> e(v, 1.0, false);
EXPECT_DOUBLE_EQ(e.values.rel(0), 1.0);
const auto m = e.vectors.get();
EXPECT_DOUBLE_EQ(m(0,0), 1.0); // identity matrix
EXPECT_DOUBLE_EQ(m(0,1), 0.0);
EXPECT_DOUBLE_EQ(m(1,1), 1.0);
EXPECT_DOUBLE_EQ(m(2,2), 1.0);
EXPECT_DOUBLE_EQ(e.values.rel_zero(0), 1.0);
e.subtract_Egs(1.0);
EXPECT_DOUBLE_EQ(e.values.rel_zero(0), 0.0);
// todo: check scales, etc.
}
TEST(Eigen, io) { // NOLINT
NRG::Eigen<double> e(2,2);
EVEC v1 = { 1.0, 2.0 };
EVEC v2 = { 3.0, 4.0 };
e.diagonal(v1);
EXPECT_DOUBLE_EQ(e.values.rel(0), 1.0);
std::ostringstream oss;
boost::archive::binary_oarchive oa(oss);
e.save(oa);
e.diagonal(v2);
EXPECT_DOUBLE_EQ(e.values.rel(0), 3.0);
std::istringstream iss(oss.str());
boost::archive::binary_iarchive ia(iss);
e.load(ia);
EXPECT_DOUBLE_EQ(e.values.rel(0), 1.0); // original value
}
TEST(Eigen, hdf5io) { // NOLINT
NRG::Eigen<double> e(2,2);
EVEC v1 = { 1.0, 2.0 };
e.diagonal(v1);
// e.values.set_scale(1.0);
auto h5 = H5Easy::File("Eigen.h5", H5Easy::File::Overwrite);
e.h5save(h5, "test");
}
TEST(io, read_std_vector) { // NOLINT
std::string data = "5 1 2 3 4 5";
std::istringstream ss(data);
auto vec = read_std_vector<double>(ss);
std::vector<double> ref(5);
ref[0] = 1; ref[1] = 2; ref[2] = 3; ref[3] = 4; ref[4] = 5;
VECTOR_EQ(vec, ref);
}
template<typename T>
auto range_size(T t)
{
size_t ctr = 0;
for([[maybe_unused]] const auto &x: t) ctr++;
return ctr;
}
TEST(Diag, constructor) { // NOLINT
std::string data =
"0 1\n"
"2 1 2\n"
"1 2\n"
"3 4 5 6\n";
std::istringstream ss(data);
Params P;
auto Sym = setup_Sym<double>(P); // need working Invar
P.absolute = true; // disable any energy rescaling
DiagInfo<double> diag(ss, 2, P);
EXPECT_EQ(diag.size(), 2);
EXPECT_EQ(range_size(diag.subspaces()), 2);
EXPECT_EQ(range_size(diag.eigs()), 2);
EXPECT_DOUBLE_EQ(diag.find_groundstate(), 1.0);
// std::vector ref_energies = { 1.0, 2.0, 4.0, 5.0, 6.0 };
// EXPECT_EQ(diag.sorted_energies_rel(), ref_energies);
EXPECT_EQ(diag.size_subspace(Invar(0,1)), 2);
EXPECT_EQ(diag.size_subspace(Invar(1,2)), 3);
EXPECT_EQ(diag.size_subspace(Invar(0,0)), 0);
EXPECT_EQ(diag.dims(Invar(0,1),Invar(1,2)), std::make_pair(2ul,3ul));
EXPECT_EQ(diag.count_states(Sym->multfnc()), 2*1+3*2); // multiplicity!
EXPECT_DOUBLE_EQ(diag.trace([](const auto x){ return 1; }, 1.0, Sym->multfnc()),
exp(-1.0)+exp(-2.0)+2*exp(-4.0)+2*exp(-5.0)+2*exp(-6.0));
// diag.save(3, P);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS(); // NOLINT
}
| 3,996
|
C++
|
.cpp
| 129
| 28.069767
| 83
| 0.630514
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,509
|
symmetry.cpp
|
rokzitko_nrgljubljana/test/unit/symmetry/symmetry.cpp
|
#include <string>
#include <sstream>
#include <gtest/gtest.h>
#include "test_common.hpp"
#include <invar.hpp>
#include <symmetry.hpp>
using namespace NRG;
TEST(Symmetry, Symmetry) { // NOLINT
Params P;
auto Sym = set_symmetry<double>(P, "QS", 1);
EXPECT_EQ(P.combs, 4);
EXPECT_EQ(Sym->nr_combs(), 4);
const auto In = Sym->input_subspaces();
// Differences wrt QNs in the previous shell to get *new* (Q,Sz)
EXPECT_EQ(In[0], Invar(1,0)); // empty: (Q+1,Sz) -> (Q,Sz)
EXPECT_EQ(In[1], Invar(0,-1)); // spin up: (Q,Sz-1/2) -> (Q,Sz)
EXPECT_EQ(In[2], Invar(0,1)); // spin down: (Q,Sz+1/2) -> (Q,Sz)
EXPECT_EQ(In[3], Invar(-1,0)); // double occupancy: (Q-1,Sz) -> (Q,Sz)
// (Q,S) quantum numbers of of the new site
EXPECT_EQ(Sym->QN_subspace(0), Invar(-1,1));
EXPECT_EQ(Sym->QN_subspace(1), Invar(0,2));
EXPECT_EQ(Sym->QN_subspace(2), Invar(0,2));
EXPECT_EQ(Sym->QN_subspace(3), Invar(1,1));
EXPECT_EQ(Sym->ancestor(Invar(0,2), 0), Invar(1,2));
EXPECT_EQ(Sym->ancestor(Invar(0,2), 1), Invar(0,1));
EXPECT_EQ(Sym->ancestor(Invar(0,2), 2), Invar(0,3));
EXPECT_EQ(Sym->ancestor(Invar(0,2), 3), Invar(-1,2));
auto anc = Sym->ancestors(Invar(0,2));
EXPECT_EQ(anc[0], Invar(1,2));
EXPECT_EQ(anc[1], Invar(0,1));
EXPECT_EQ(anc[2], Invar(0,3));
EXPECT_EQ(anc[3], Invar(-1,2));
auto subs = Sym->new_subspaces(Invar(0,0));
EXPECT_EQ(subs[0], Invar(-1,0));
EXPECT_EQ(subs[1], Invar(0,1));
EXPECT_EQ(subs[2], Invar(0,-1));
EXPECT_EQ(subs[3], Invar(1,0));
EXPECT_EQ(Sym->mult(Invar(1,2)), 2);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS(); // NOLINT
}
| 1,658
|
C++
|
.cpp
| 43
| 35.930233
| 72
| 0.627561
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,510
|
time_mem.cpp
|
rokzitko_nrgljubljana/test/unit/time_mem/time_mem.cpp
|
#include <string>
#include <sstream>
#include <gtest/gtest.h>
#include <time_mem.hpp>
using namespace NRG;
TEST(Time_Mem, MemTime) { // NOLINT
MemTime mt;
mt.brief_report();
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS(); // NOLINT
}
| 300
|
C++
|
.cpp
| 13
| 21
| 42
| 0.70318
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,511
|
openmp_test.cpp
|
rokzitko_nrgljubljana/test/unit/openmp_test/openmp_test.cpp
|
#include <gtest/gtest.h>
#include <openmp.hpp>
using namespace NRG;
TEST(openMP, report) {
report_openMP();
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
| 221
|
C++
|
.cpp
| 10
| 20
| 42
| 0.706731
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,512
|
matrix_wrapping.cpp
|
rokzitko_nrgljubljana/test/unit/traits/matrix_wrapping.cpp
|
#include <gtest/gtest.h>
#include <type_traits>
#include <complex>
#include <traits.hpp>
#include <numerics.hpp>
using namespace NRG;
TEST(generate, Eigen_d) {
const auto m = generate_Eigen<double>(5, 10);
EXPECT_EQ(nrvec(m), 5);
EXPECT_EQ(dim(m), 10);
EXPECT_EQ(size1(m), 5);
EXPECT_EQ(size2(m), 10);
}
TEST(generate, Eigen_c) {
const auto m = generate_Eigen<std::complex<double>>(5, 10);
EXPECT_EQ(nrvec(m), 5);
EXPECT_EQ(dim(m), 10);
EXPECT_EQ(size1(m), 5);
EXPECT_EQ(size2(m), 10);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
| 619
|
C++
|
.cpp
| 24
| 23.5
| 61
| 0.674576
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,513
|
traits.cpp
|
rokzitko_nrgljubljana/test/unit/traits/traits.cpp
|
#include <gtest/gtest.h>
#include <type_traits>
#include <traits.hpp>
using namespace NRG;
TEST(traits, double) {
const bool b = std::is_same_v<typename traits<double>::t_matel, double>; // cannot be embedded in EXPECT_EQ
EXPECT_EQ(b, true);
}
TEST(traits, complex) {
const bool b = std::is_same_v<typename traits<std::complex<double>>::t_matel, std::complex<double>>;
EXPECT_EQ(b, true);
}
template<matrix M> void foo(const M &m) {
EXPECT_EQ(size1(m), 0);
EXPECT_EQ(size2(m), 0);
}
TEST(traits, matrix) {
EigenMatrix<double> er;
foo(er);
EigenMatrix<std::complex<double>> ec;
foo(ec);
}
template<real_matrix RM> void foo_r(const RM &m) {
EXPECT_EQ(size1(m), 0);
EXPECT_EQ(size2(m), 0);
}
TEST(traits, real_matrix) {
EigenMatrix<double> er;
foo_r(er);
}
template<complex_matrix CM> void foo_c(const CM &m) {
EXPECT_EQ(size1(m), 0);
EXPECT_EQ(size2(m), 0);
}
TEST(traits, complex_matrix) {
EigenMatrix<std::complex<double>> ec;
foo_c(ec);
}
template<Eigen_matrix EM> void foo_e(const EM &m) {
EXPECT_EQ(size1(m), 0);
EXPECT_EQ(size2(m), 0);
}
TEST(traits, Eigen_matrix) {
EigenMatrix<double> er;
foo_e(er);
EigenMatrix<std::complex<double>> ec;
foo_e(ec);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
| 1,323
|
C++
|
.cpp
| 52
| 23.173077
| 109
| 0.686259
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,514
|
mp.cpp
|
rokzitko_nrgljubljana/test/unit/mp/mp.cpp
|
#include <gtest/gtest.h>
#include <mp.hpp>
using namespace NRG;
TEST(mp, mp) {
my_mpf object;
vmpf vector_of_objects;
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
| 233
|
C++
|
.cpp
| 11
| 19
| 42
| 0.69863
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,515
|
basic.cpp
|
rokzitko_nrgljubljana/test/unit/basic/basic.cpp
|
#include <gtest/gtest.h>
#include <nrg-general.hpp> // common
#include <nrg-lib.hpp> // exposed in library
// Test inclusion of exposed headers: does this compile?
TEST(Add, int) { // NOLINT
int a{0};
int b{2};
auto c = a + b;
EXPECT_EQ(c, b); // NOLINT
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS(); // NOLINT
}
| 392
|
C++
|
.cpp
| 14
| 25.428571
| 56
| 0.649733
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,516
|
params.cpp
|
rokzitko_nrgljubljana/test/unit/params/params.cpp
|
#include <string>
#include <sstream>
#include <gtest/gtest.h>
#include "test_common.hpp"
#include <params.hpp>
using namespace std::string_literals;
using namespace NRG;
TEST(params, parser) {
{
std::list<parambase *> all;
param<double> p{"p", "Testing parameter", "1.0", all};
EXPECT_EQ(p.getkeyword(), "p"s);
EXPECT_EQ(p.getdesc(), "Testing parameter"s);
std::ostringstream ss;
p.dump(ss);
EXPECT_EQ(ss.str(), "p=1\n"s);
EXPECT_EQ(p, 1.0);
EXPECT_EQ(p.value(), 1.0);
p.set_str("2.0");
std::ostringstream ss2;
p.dump(ss2);
EXPECT_EQ(ss2.str(), "p=2 *\n"s);
EXPECT_EQ(p, 2.0);
EXPECT_EQ(p.value(), 2.0);
p = 3.0;
EXPECT_EQ(p, 3.0);
EXPECT_EQ(p.value(), 3.0);
EXPECT_EQ(p == 3.0, true);
EXPECT_EQ(p == 4.0, false);
EXPECT_THROW(param<double>("p", "another one", "1.0", all), std::runtime_error);
}
}
TEST(params, DiagParams) { // NOLINT
Params P;
auto DP = DiagParams(P, 0.5);
}
TEST(params, Defaults) {
// check important default values
Params P;
EXPECT_EQ(P.symtype, ""s);
EXPECT_EQ(P.Lambda, 2.0);
EXPECT_EQ(P.z, 1.0);
EXPECT_EQ(P.bandrescale, 1.0);
EXPECT_EQ(P.diag, "default"s);
EXPECT_EQ(P.keepenergy, -1.0);
EXPECT_EQ(P.keepmin, 0ul);
EXPECT_EQ(P.fixeps, 1e-15);
EXPECT_EQ(P.dm, false);
EXPECT_EQ(P.strategy, "kept");
EXPECT_EQ(P.bins, 1000ul);
EXPECT_EQ(P.discard_trim, 1e-16);
EXPECT_EQ(P.discard_immediately, 1e-16);
EXPECT_EQ(P.prec_td, 10);
EXPECT_EQ(P.prec_custom, 10);
EXPECT_EQ(P.prec_xy, 10);
EXPECT_EQ(P.done, true);
EXPECT_EQ(P.calc0, true);
EXPECT_EQ(P.lastall, false);
EXPECT_EQ(P.lastalloverride, false);
EXPECT_EQ(P.Ninit.value(), 0);
EXPECT_EQ(P.Nmax, 0ul);
EXPECT_EQ(P.ZBW(), true);
}
TEST(params, set_channels_and_combs) {
Params P;
EXPECT_EQ(P.symtype, ""s);
P.set_channels_and_combs(1);
EXPECT_EQ(P.channels, 1);
EXPECT_EQ(P.coeffactor, 1);
EXPECT_EQ(P.coefchannels, 1);
EXPECT_EQ(P.perchannel, 1);
EXPECT_EQ(P.spin, 2);
EXPECT_EQ(P.combs, 4);
}
template< class InputIt, class UnaryPredicate>
bool one_of(InputIt first, InputIt last, UnaryPredicate p)
{
return std::count(first, last, p) == 1;
}
template< class InputIt>
bool one_of(InputIt first, InputIt last)
{
return std::count(first, last, true) == 1;
}
void check_recalc(const Params &P, const RUNTYPE runtype)
{
std::vector v = { P.do_recalc_kept(runtype), P.do_recalc_all(runtype), P.do_recalc_none() };
EXPECT_EQ(one_of(v.begin(), v.end()), true);
}
void check_recalc(const Params &P)
{
check_recalc(P, RUNTYPE::NRG);
check_recalc(P, RUNTYPE::DMNRG);
}
TEST(params, flags_none) {
Params P;
P.Nmax = 1;
EXPECT_EQ(P.cfs_flags(), false);
EXPECT_EQ(P.fdm_flags(), false);
EXPECT_EQ(P.dmnrg_flags(), false);
EXPECT_EQ(P.cfs_or_fdm_flags(), false);
EXPECT_EQ(P.dm_flags(), false);
EXPECT_EQ(P.keep_all_states_in_last_step(), false);
EXPECT_EQ(P.need_rho(), false);
EXPECT_EQ(P.need_rhoFDM(), false);
EXPECT_EQ(P.do_recalc_kept(RUNTYPE::NRG), true);
EXPECT_EQ(P.do_recalc_kept(RUNTYPE::DMNRG), true);
check_recalc(P);
}
TEST(params, flags_cfs) {
Params P;
P.Nmax = 1;
P.cfs = true;
EXPECT_EQ(P.cfs_flags(), true);
EXPECT_EQ(P.fdm_flags(), false);
EXPECT_EQ(P.dmnrg_flags(), false);
EXPECT_EQ(P.cfs_or_fdm_flags(), true);
EXPECT_EQ(P.dm_flags(), true);
EXPECT_EQ(P.keep_all_states_in_last_step(), true);
EXPECT_EQ(P.need_rho(), true);
EXPECT_EQ(P.need_rhoFDM(), false);
EXPECT_EQ(P.do_recalc_kept(RUNTYPE::NRG), true);
EXPECT_EQ(P.do_recalc_all(RUNTYPE::DMNRG), true);
check_recalc(P);
}
TEST(params, flags_fdm) {
Params P;
P.Nmax = 1;
P.fdm = true;
EXPECT_EQ(P.cfs_flags(), false);
EXPECT_EQ(P.fdm_flags(), true);
EXPECT_EQ(P.dmnrg_flags(), false);
EXPECT_EQ(P.cfs_or_fdm_flags(), true);
EXPECT_EQ(P.dm_flags(), true);
EXPECT_EQ(P.keep_all_states_in_last_step(), true);
EXPECT_EQ(P.need_rho(), false);
EXPECT_EQ(P.need_rhoFDM(), true);
EXPECT_EQ(P.do_recalc_kept(RUNTYPE::NRG), true);
EXPECT_EQ(P.do_recalc_all(RUNTYPE::DMNRG), true);
check_recalc(P);
}
TEST(params, flags_fdmexpv) {
Params P;
P.Nmax = 1;
P.fdmexpv = true;
EXPECT_EQ(P.cfs_flags(), false);
EXPECT_EQ(P.fdm_flags(), true);
EXPECT_EQ(P.dmnrg_flags(), false);
EXPECT_EQ(P.cfs_or_fdm_flags(), true);
EXPECT_EQ(P.dm_flags(), true);
EXPECT_EQ(P.keep_all_states_in_last_step(), true);
EXPECT_EQ(P.need_rho(), false);
EXPECT_EQ(P.need_rhoFDM(), true);
EXPECT_EQ(P.do_recalc_kept(RUNTYPE::NRG), true);
EXPECT_EQ(P.do_recalc_all(RUNTYPE::DMNRG), true);
check_recalc(P);
}
TEST(params, flags_dmnrg) {
Params P;
P.Nmax = 1;
P.dmnrg = true;
EXPECT_EQ(P.cfs_flags(), false);
EXPECT_EQ(P.fdm_flags(), false);
EXPECT_EQ(P.dmnrg_flags(), true);
EXPECT_EQ(P.cfs_or_fdm_flags(), false);
EXPECT_EQ(P.dm_flags(), true);
EXPECT_EQ(P.keep_all_states_in_last_step(), false);
EXPECT_EQ(P.need_rho(), true);
EXPECT_EQ(P.need_rhoFDM(), false);
EXPECT_EQ(P.do_recalc_kept(RUNTYPE::NRG), true);
EXPECT_EQ(P.do_recalc_kept(RUNTYPE::DMNRG), true);
check_recalc(P);
}
TEST(params, scale) {
Params P;
EXPECT_EQ(P.Lambda, 2.0);
EXPECT_EQ(P.discretization, "Z"s);
const double s1 = (1.0-1.0/2.0)/std::log(2.0);
EXPECT_LT(std::abs(P.SCALE(1) - s1), 1e-10);
const double s2 = s1/sqrt(2.0);
EXPECT_LT(std::abs(P.SCALE(2) - s2), 1e-10);
EXPECT_LT(std::abs(P.nrg_step_scale_factor() - sqrt(2.0)), 1e-10);
P.Nmax = 1;
EXPECT_LT(std::abs(P.last_step_scale() - s1), 1e-10);
P.absolute = true;
EXPECT_EQ(P.nrg_step_scale_factor(), 1.0);
}
TEST(params, E) {
Params P;
P.Nmax = 1;
EXPECT_EQ(P.getEfactor(), sqrt(2.0));
EXPECT_EQ(P.getE0(), 2.0);
EXPECT_EQ(P.getEmin(), 2.0);
EXPECT_EQ(P.getEx(), 2.0*sqrt(2.0));
EXPECT_LT(std::abs(P.getEmax()-4.0), 1e-10);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
| 5,939
|
C++
|
.cpp
| 200
| 26.79
| 94
| 0.653711
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,517
|
misc.cpp
|
rokzitko_nrgljubljana/test/unit/misc/misc.cpp
|
#include <gtest/gtest.h>
#include <complex>
#include <list>
#include <map>
#include <sstream>
#include <string>
using namespace std::string_literals;
#include <fstream>
#include <exception>
#include <misc.hpp>
#include "compare.hpp"
using namespace NRG;
TEST(misc, containers) {
{
std::list l = {1, 2, 3, 4};
auto b = get_back(l);
EXPECT_EQ(b, 4);
auto f = get_front(l);
EXPECT_EQ(f, 1);
}
}
TEST(misc, strings) {
{
auto str = "123 "s;
auto res = strip_trailing_whitespace(str);
EXPECT_EQ(res, "123"s);
}
}
TEST(misc, tokenizer) {
{
auto str = "1 2 3 4"s;
string_token st(str);
EXPECT_EQ(st.find("1"), true);
EXPECT_EQ(st.find("2"), true);
EXPECT_EQ(st.find("3"), true);
EXPECT_EQ(st.find("4"), true);
EXPECT_EQ(st.find("5"), false);
}
{
auto str = "ab cd ef";
string_token st(str);
EXPECT_EQ(st.find("ab"), true);
EXPECT_EQ(st.find("cd"), true);
EXPECT_EQ(st.find("ef"), true);
EXPECT_EQ(st.find("gh"), false);
}
}
TEST(misc, get_back) {
std::list a = {31,44,55,66,71,82};
const auto b = get_back(a);
EXPECT_EQ(std::size(a), 5);
EXPECT_EQ(b, 82);
EXPECT_EQ(a.back(), 71);
EXPECT_EQ(a.front(), 31);
std::list<int> empty_list;
EXPECT_THROW(get_back(empty_list), std::runtime_error);
}
TEST(misc, get_front) {
std::list a = {31,44,55,66,71,82};
const auto b = get_front(a);
EXPECT_EQ(std::size(a), 5);
EXPECT_EQ(b, 31);
EXPECT_EQ(a.back(), 82);
EXPECT_EQ(a.front(), 44);
}
TEST(misc, switch3) {
EXPECT_EQ(switch3(2,3,10,4,15,2,88),88);
EXPECT_EQ(switch3(3,3,10,4,15,2,88),10);
EXPECT_EQ(switch3(4,3,10,4,15,2,88),15);
EXPECT_THROW(switch3(1,3,10,4,15,2,88), std::runtime_error);
}
TEST(misc, nextline) {
auto file = safe_open_for_reading("txt/nextline.txt");
const std::vector<std::string> result = {"zdravo", "nekaj", "adijo"};
for(int i = 0; i < 3; i++){
EXPECT_EQ(nextline(file),result[i]);
}
EXPECT_EQ(nextline(file), std::nullopt);
auto empty_file = safe_open_for_reading("txt/empty.txt");
EXPECT_EQ(nextline(empty_file), std::nullopt);
}
TEST(misc, strip_trailing_whitespace) {
const auto a = " test \t \n ";
EXPECT_EQ(strip_trailing_whitespace(a), " test");
EXPECT_EQ(strip_trailing_whitespace(" \t \n "s), ""s);
}
TEST(misc, block) {
const auto nekaj = parser("txt/block.txt", "nekaj");
const auto nekaj_drugega = parser("txt/block.txt", "nekaj drugega");
const auto nekaj_tretjega = parser("txt/block.txt", "nekaj tretjega");
const std::map nekaj_map = {std::pair("a"s, "2"s), std::pair("b"s, "3"s), std::pair("c"s, "4"s)};
const std::map nekaj_drugega_map = {std::pair("d"s, "5"s), std::pair("c"s, "6"s), std::pair("e"s, "10"s)};
const std::map nekaj_tretjega_map = {std::pair("abs"s, "79"s)};
compare(nekaj, nekaj_map);
compare(nekaj_drugega, nekaj_drugega_map);
compare(nekaj_tretjega, nekaj_tretjega_map);
EXPECT_THROW(parser("txt/block.txt", "zadeva"), std::runtime_error);
}
TEST(misc, skip_comments){
auto file = safe_open_for_reading("txt/nextline.txt");
const std::vector result = {"zdravo"s, "nekaj"s, "adijo"s};
std::string line;
for(int i = 0; i < 3; i++){
skip_comments(file);
std::getline(file,line);
EXPECT_EQ(line,result[i]);
}
skip_comments(file);
std::getline(file,line);
EXPECT_EQ(nextline(file),std::nullopt);
}
TEST(misc, sortfirst){
std::vector a = {std::pair(12,5), std::pair(12,4), std::pair(99, 122), std::pair(-10, 41), std::pair(42, 21)};
std::sort(a.begin(), a.end(), sortfirst());
const std::vector mpkey = {-10, 12, 12, 42, 99};
const std::vector mpval = {41, 5, 4, 21, 122};
auto i = 0;
for (const auto& [key, val]: a) {
EXPECT_EQ(mpkey[i], key);
EXPECT_EQ(mpval[i], val);
i++;
}
}
TEST(misc, vector_of_keys){
const std::map a = {std::pair(12,5), std::pair(99, 122), std::pair(-10, 41), std::pair(42, 21)};
const auto b = vector_of_keys(a);
const std::vector expected = {-10,12,42,99};
ASSERT_EQ(b, expected);
}
| 4,005
|
C++
|
.cpp
| 129
| 28.147287
| 111
| 0.631552
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,518
|
coef.cpp
|
rokzitko_nrgljubljana/test/unit/coef/coef.cpp
|
#include <string>
#include <sstream>
#include <gtest/gtest.h>
#include "test_common.hpp"
#include "test_clean.hpp"
#include <coef.hpp>
using namespace NRG;
TEST(Coef, parse) { // NOLINT
Params P;
auto SymSP = setup_Sym<double>(P);
auto Sym = SymSP.get();
auto diag = setup_diag_clean<double>(P, Sym);
Coef<double> coef(P);
std::string str =
"1\n"
"0.54528747084262258072\n"
"0.41550946829175445321\n"
"1\n"
"0.e-999\n"
"0.e-998\n";
std::istringstream ss(str);
EXPECT_EQ(P.coefchannels, 1);
coef.xi.read(ss, P.coefchannels);
coef.zeta.read(ss, P.coefchannels);
EXPECT_EQ(coef.xi.nr_tabs(), 1);
EXPECT_EQ(coef.zeta.nr_tabs(), 1);
EXPECT_EQ(coef.xi.max(0), 1);
EXPECT_EQ(coef.zeta.max(0), 1);
EXPECT_EQ(coef.xi(0,0), 0.54528747084262258072);
EXPECT_EQ(coef.xi(1,0), 0.41550946829175445321); // first index: N, second index: alpha (channel number)
EXPECT_EQ(coef.zeta(0,0), 0);
EXPECT_EQ(coef.zeta(1,0), 0);
coef.xi.set(0, 0, 42.);
EXPECT_EQ(coef.xi(0,0), 42.);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS(); // NOLINT
}
| 1,150
|
C++
|
.cpp
| 39
| 26.435897
| 106
| 0.666969
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,519
|
lambda.cpp
|
rokzitko_nrgljubljana/test/unit/adapt/lambda.cpp
|
#include <gtest/gtest.h>
#include <cmath>
#include <adapt/lambda.hpp>
using namespace NRG::Adapt;
TEST(Lambda, basic) { // NOLINT
LAMBDA Lambda(2.0);
EXPECT_EQ(Lambda, 2.0); // NOLINT
EXPECT_EQ(Lambda.logL(), log(2.0));
EXPECT_EQ(Lambda.factor(), (1.0-1.0/2.0)/log(2.0));
EXPECT_EQ(Lambda.power(2.0), pow(2.0, 2.0));
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS(); // NOLINT
}
| 449
|
C++
|
.cpp
| 15
| 27.6
| 53
| 0.665116
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,520
|
operators.cpp
|
rokzitko_nrgljubljana/test/unit/operators/operators.cpp
|
#include <string>
#include <sstream>
#include <gtest/gtest.h>
#include "test_common.hpp"
#include "test_clean.hpp"
#include <operators.hpp>
using namespace NRG;
TEST(Operators, MatrixElements) { // NOLINT
Params P;
auto SymSP = setup_Sym<double>(P);
auto Sym = SymSP.get();
auto diag = setup_diag<double>(P, Sym);
{
std::string str =
"1\n"
"0 1 0 1\n"
"1 2\n 3 4\n";
std::istringstream ss(str);
auto me = MatrixElements(ss, diag);
std::cout << me << std::endl;
}
{
std::string str =
"2\n"
"0 1 0 1\n"
"1 2\n 3 4\n"
"1 2 1 2\n"
"1 2 3\n 4 5 6\n 7 8 9\n";
std::istringstream ss(str);
auto me = MatrixElements(ss, diag);
std::cout << me << std::endl;
}
{
std::string str =
"1\n"
"0 1 1 2\n"
"1 2 3\n 4 5 6\n";
std::istringstream ss(str);
auto me = MatrixElements(ss, diag);
std::cout << me << std::endl;
}
{
std::string str =
"1\n"
"0 1 0 99\n"
"1 2\n 3 4\n";
std::istringstream ss(str);
EXPECT_THROW(MatrixElements(ss, diag), std::runtime_error);
}
}
TEST(Operators, Opch_empty) { // NOLINT
Params P;
auto SymSP = setup_Sym<double>(P);
auto Sym = SymSP.get();
auto diag = setup_diag<double>(P, Sym);
{
auto o = Opch<double>(P);
o.dump();
EXPECT_EQ(o.size(), 1); // channels
EXPECT_EQ(o[0].size(), 1); // perchannel
EXPECT_EQ(o[0][0].size(), 0);
}
}
TEST(Operators, Opch_clean) { // NOLINT
Params P;
auto SymSP = setup_Sym<double>(P);
auto Sym = SymSP.get();
auto diag = setup_diag_clean<double>(P, Sym);
diag.states_report(Sym->multfnc());
std::string str =
"f 0 0\n"
"2\n"
"1 1 0 2\n"
"1.4142135623730951\n"
"0 2 -1 1\n"
"1.\n";
std::istringstream ss(str);
auto o = Opch<double>(ss, diag, P);
o.dump();
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS(); // NOLINT
}
| 1,968
|
C++
|
.cpp
| 84
| 19.404762
| 63
| 0.579872
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,521
|
recalc.cpp
|
rokzitko_nrgljubljana/test/unit/recalc/recalc.cpp
|
#include <gtest/gtest.h>
#include <recalc.hpp>
#include "test_common.hpp"
TEST(recalc, split_in_blocks_Eigen) { // NOLINT
Params P;
auto SymSP = setup_Sym<double>(P);
auto Sym = SymSP.get();
auto diag = setup_diag(P, Sym);
#ifdef DISABLED_DUE_TO_ISSUES
SubspaceStructure substruct{diag, Sym};
substruct.dump();
#endif
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS(); // NOLINT
}
| 451
|
C++
|
.cpp
| 17
| 24.294118
| 47
| 0.705336
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,522
|
subspaces.cpp
|
rokzitko_nrgljubljana/test/unit/subspaces/subspaces.cpp
|
#include <string>
#include <sstream>
#include <gtest/gtest.h>
#include "test_common.hpp"
#include <invar.hpp>
#include <subspaces.hpp>
#include <core.hpp>
using namespace NRG;
TEST(Subspaces, SubspaceStructure) { // NOLINT
Params P;
auto SymSP = setup_Sym<double>(P);
auto Sym = SymSP.get();
auto diag = setup_diag(P, Sym);
SubspaceStructure substruct{diag, Sym};
TaskList tasklist{substruct};
auto tasks = tasklist.get();
std::cout << tasks << std::endl;
substruct.dump();
EXPECT_EQ(substruct.at_or_null(Invar(0,2)).total(), 5);
EXPECT_EQ(substruct.at_or_null(Invar(0,0)).total(), 0);
}
TEST(Subspaces, SubspaceDimensions) { // NOLINT
Params P;
auto SymSP = setup_Sym<double>(P);
auto Sym = SymSP.get();
auto diag = setup_diag3(P, Sym);
Invar I(2,1);
InvarVec ancestors = { Invar(0,1), Invar(1,2), Invar(2,1) }; // dims 2,3,4
SubspaceDimensions sd(I, ancestors, diag, Sym, true); // true = ignore triangle inequality
sd.dump();
EXPECT_EQ(sd.combs(), 3);
EXPECT_EQ(sd.rmax(0), 2);
EXPECT_EQ(sd.rmax(1), 3);
EXPECT_EQ(sd.rmax(2), 4);
EXPECT_EQ(sd[0], 2);
EXPECT_EQ(sd[1], 3);
EXPECT_EQ(sd[2], 4);
EXPECT_EQ(sd.exists(0), true);
EXPECT_EQ(sd.exists(1), true);
EXPECT_EQ(sd.exists(2), true);
EXPECT_EQ(sd.offset(0), 0);
EXPECT_EQ(sd.offset(1), 2);
EXPECT_EQ(sd.offset(2), 5);
EXPECT_EQ(sd.chunk(1), std::make_pair(0ul,2ul)); // first=first element, second=length
EXPECT_EQ(sd.chunk(2), std::make_pair(2ul,3ul));
EXPECT_EQ(sd.chunk(3), std::make_pair(5ul,4ul));
EXPECT_EQ(sd.total(), 9);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS(); // NOLINT
}
| 1,679
|
C++
|
.cpp
| 52
| 29.692308
| 92
| 0.67016
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,523
|
diag_test_Eigen.cpp
|
rokzitko_nrgljubljana/test/unit/diag_test/diag_test_Eigen.cpp
|
#include <string>
#include <sstream>
#include <cmath>
#include <gtest/gtest.h>
#include "test_common.hpp"
#include <params.hpp>
#include <diag.hpp>
using namespace NRG;
const double sq2 = 1.0/sqrt(2.0);
TEST(Diag, dsyev) {
{
EigenMatrix<double> m(2,2); // Sigma_Z
m(0,0) = -1.0; m(1,1) = +1.0;
m(1,0) = m(0,1) = 0.0;
const auto res = diagonalise_dsyev(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), 1.0);
EXPECT_DOUBLE_EQ(res.vec(0,1), 0.0);
EXPECT_DOUBLE_EQ(res.vec(1,0), 0.0);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), 1.0);
}
{
EigenMatrix<double> m(2,2); // Sigma_X
m(0,0) = m(1,1) = m(1,0) = 0.0;
m(0,1) = 1.0;
const auto res = diagonalise_dsyev(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), sq2);
EXPECT_DOUBLE_EQ(res.vec(0,0)*res.vec(0,1), -0.5);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), sq2);
EXPECT_DOUBLE_EQ(res.vec(1,0)*res.vec(1,1), +0.5);
}
}
TEST(Diag, dsyevr) {
{
EigenMatrix<double> m(2,2); // Sigma_Z
m(0,0) = -1.0; m(1,1) = +1.0;
m(1,0) = m(0,1) = 0.0;
const auto res = diagonalise_dsyevr(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), 1.0);
EXPECT_DOUBLE_EQ(res.vec(0,1), 0.0);
EXPECT_DOUBLE_EQ(res.vec(1,0), 0.0);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), 1.0);
}
{
EigenMatrix<double> m(2,2); // Sigma_X
m(0,0) = m(1,1) = m(1,0) = 0.0;
m(0,1) = 1.0;
const auto res = diagonalise_dsyevr(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), sq2);
EXPECT_DOUBLE_EQ(res.vec(0,0)*res.vec(0,1), -0.5);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), sq2);
EXPECT_DOUBLE_EQ(res.vec(1,0)*res.vec(1,1), +0.5);
}
}
TEST(Diag, dsyevd) {
{
EigenMatrix<double> m(2,2); // Sigma_Z
m(0,0) = -1.0; m(1,1) = +1.0;
m(1,0) = m(0,1) = 0.0;
const auto res = diagonalise_dsyevd(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), 1.0);
EXPECT_DOUBLE_EQ(res.vec(0,1), 0.0);
EXPECT_DOUBLE_EQ(res.vec(1,0), 0.0);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), 1.0);
}
{
EigenMatrix<double> m(2,2); // Sigma_X
m(0,0) = m(1,1) = m(1,0) = 0.0;
m(0,1) = 1.0;
const auto res = diagonalise_dsyevd(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), sq2);
EXPECT_DOUBLE_EQ(res.vec(0,0)*res.vec(0,1), -0.5);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), sq2);
EXPECT_DOUBLE_EQ(res.vec(1,0)*res.vec(1,1), +0.5);
}
}
TEST(Diag, zheev) {
{
EigenMatrix<std::complex<double>> m(2,2); // Sigma_Z
m(0,0) = -1.0; m(1,1) = +1.0;
m(1,0) = m(0,1) = 0.0;
const auto res = diagonalise_zheev(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), 1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), 0.0);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), 0.0);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), 1.0);
}
{
EigenMatrix<std::complex<double>> m(2,2); // Sigma_X
m(0,0) = m(1,1) = m(1,0) = 0.0;
m(0,1) = 1.0;
const auto res = diagonalise_zheev(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(0,0)*res.vec(0,1)).real(), -0.5);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(1,0)*res.vec(1,1)).real(), +0.5);
}
{
EigenMatrix<std::complex<double>> m(2,2); // Sigma_Y
m(0,0) = m(1,1) = m(1,0) = 0.0;
m(0,1) = std::complex<double>(0.0,1.0);
const auto res = diagonalise_zheev(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(0,0)*res.vec(0,1)).imag(), -0.5);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(1,0)*res.vec(1,1)).imag(), +0.5);
}
}
TEST(Diag, zheevr) {
{
EigenMatrix<std::complex<double>> m(2,2); // Sigma_Z
m(0,0) = -1.0; m(1,1) = +1.0;
m(1,0) = m(0,1) = 0.0;
const auto res = diagonalise_zheevr(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), 1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), 0.0);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), 0.0);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), 1.0);
}
{
EigenMatrix<std::complex<double>> m(2,2); // Sigma_X
m(0,0) = m(1,1) = m(1,0) = 0.0;
m(0,1) = 1.0;
const auto res = diagonalise_zheevr(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(0,0)*res.vec(0,1)).real(), -0.5);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(1,0)*res.vec(1,1)).real(), +0.5);
}
{
EigenMatrix<std::complex<double>> m(2,2); // Sigma_Y
m(0,0) = m(1,1) = m(1,0) = 0.0;
m(0,1) = std::complex<double>(0.0,1.0);
const auto res = diagonalise_zheevr(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(0,0)*res.vec(0,1)).imag(), -0.5);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(1,0)*res.vec(1,1)).imag(), +0.5);
}
}
TEST(Diag, diagonalise_complex) {
Params P;
auto DP = DiagParams(P, 0.5);
{
EigenMatrix<std::complex<double>> m(2,2); // Sigma_Z
m(0,0) = -1.0; m(1,1) = +1.0;
m(1,0) = m(0,1) = 0.0;
const auto res = diagonalise(m, DP, -1);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), 1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), 0.0);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), 0.0);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), 1.0);
}
{
EigenMatrix<std::complex<double>> m(2,2); // Sigma_X
m(0,0) = m(1,1) = m(1,0) = 0.0;
m(0,1) = 1.0;
const auto res = diagonalise(m, DP, -1);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(0,0)*res.vec(0,1)).real(), -0.5);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(1,0)*res.vec(1,1)).real(), +0.5);
}
{
EigenMatrix<std::complex<double>> m(2,2); // Sigma_Y
m(0,0) = m(1,1) = m(1,0) = 0.0;
m(0,1) = std::complex<double>(0.0,1.0);
const auto res = diagonalise(m, DP, -1);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(0,0)*res.vec(0,1)).imag(), -0.5);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(1,0)*res.vec(1,1)).imag(), +0.5);
}
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS(); // NOLINT
}
| 9,095
|
C++
|
.cpp
| 255
| 31.243137
| 63
| 0.584324
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,524
|
diag_test.cpp
|
rokzitko_nrgljubljana/test/unit/diag_test/diag_test.cpp
|
#include <string>
#include <sstream>
#include <cmath>
#include <gtest/gtest.h>
#include "test_common.hpp"
#include <params.hpp>
#include <diag.hpp>
using namespace NRG;
TEST(Diag, diagonalise) { // NOLINT
Params P;
auto DP = DiagParams(P, 0.5);
}
TEST(Diag, to_matel) {
lapack_complex_double z1 {2.0, 1.0};
const auto z2 = to_matel(z1);
EXPECT_DOUBLE_EQ(z2.real(), 2.0);
EXPECT_DOUBLE_EQ(z2.imag(), 1.0);
}
TEST(Diag, copy_val) {
const std::vector<double> s { 1.0, 2.0, 3.0 };
std::vector<double> d;
copy_val(s, d, s.size());
EXPECT_EQ(d.size(), 3);
EXPECT_DOUBLE_EQ(d[0], 1.0);
EXPECT_DOUBLE_EQ(d[1], 2.0);
EXPECT_DOUBLE_EQ(d[2], 3.0);
}
TEST(Diag, copy_val_M) {
const std::vector<double> s { 1.0, 2.0, 3.0 };
std::vector<double> d;
copy_val(s, d, 2);
EXPECT_EQ(d.size(), 2);
EXPECT_DOUBLE_EQ(d[0], 1.0);
EXPECT_DOUBLE_EQ(d[1], 2.0);
}
TEST(Diag, copy_vec) {
const std::vector<double> m { 1.0, 2.0, 3.0, 4.0 };
Matrix_traits<double> vec;
copy_vec(data(m), vec, 2, 2);
EXPECT_EQ(size1(vec), 2);
EXPECT_EQ(size2(vec), 2);
EXPECT_DOUBLE_EQ(vec(0,0), 1.0);
EXPECT_DOUBLE_EQ(vec(0,1), 2.0);
EXPECT_DOUBLE_EQ(vec(1,0), 3.0);
EXPECT_DOUBLE_EQ(vec(1,1), 4.0);
}
TEST(Diag, copy_results) {
const std::vector<double> val { 1.0, 2.0 };
const std::vector<double> vec { 1.0, 0.0, 0.0, 1.0 };
const auto res = copy_results<double>(val, data(vec), 'V', 2, 2);
res.check_diag();
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
}
TEST(Diag, check_is_matrix_upper) {
Matrix_traits<double> m(2,2);
m(0,0) = m(0,1) = m(1,1) = 1.0;
m(1,0) = 0.0;
EXPECT_TRUE(is_square(m));
EXPECT_TRUE(is_matrix_upper(m));
}
const double sq2 = 1.0/sqrt(2.0);
TEST(Diag, dsyev) {
{
Matrix_traits<double> m(2,2); // Sigma_Z
m(0,0) = -1.0; m(1,1) = +1.0;
m(1,0) = m(0,1) = 0.0;
const auto res = diagonalise_dsyev(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), 1.0);
EXPECT_DOUBLE_EQ(res.vec(0,1), 0.0);
EXPECT_DOUBLE_EQ(res.vec(1,0), 0.0);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), 1.0);
}
{
Matrix_traits<double> m(2,2); // Sigma_X
m(0,0) = m(1,1) = m(1,0) = 0.0;
m(0,1) = 1.0;
const auto res = diagonalise_dsyev(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), sq2);
EXPECT_DOUBLE_EQ(res.vec(0,0)*res.vec(0,1), -0.5);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), sq2);
EXPECT_DOUBLE_EQ(res.vec(1,0)*res.vec(1,1), +0.5);
}
}
TEST(Diag, dsyevr) {
{
Matrix_traits<double> m(2,2); // Sigma_Z
m(0,0) = -1.0; m(1,1) = +1.0;
m(1,0) = m(0,1) = 0.0;
const auto res = diagonalise_dsyevr(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), 1.0);
EXPECT_DOUBLE_EQ(res.vec(0,1), 0.0);
EXPECT_DOUBLE_EQ(res.vec(1,0), 0.0);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), 1.0);
}
{
Matrix_traits<double> m(2,2); // Sigma_X
m(0,0) = m(1,1) = m(1,0) = 0.0;
m(0,1) = 1.0;
const auto res = diagonalise_dsyevr(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), sq2);
EXPECT_DOUBLE_EQ(res.vec(0,0)*res.vec(0,1), -0.5);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), sq2);
EXPECT_DOUBLE_EQ(res.vec(1,0)*res.vec(1,1), +0.5);
}
}
TEST(Diag, dsyevd) {
{
Matrix_traits<double> m(2,2); // Sigma_Z
m(0,0) = -1.0; m(1,1) = +1.0;
m(1,0) = m(0,1) = 0.0;
const auto res = diagonalise_dsyevd(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), 1.0);
EXPECT_DOUBLE_EQ(res.vec(0,1), 0.0);
EXPECT_DOUBLE_EQ(res.vec(1,0), 0.0);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), 1.0);
}
{
Matrix_traits<double> m(2,2); // Sigma_X
m(0,0) = m(1,1) = m(1,0) = 0.0;
m(0,1) = 1.0;
const auto res = diagonalise_dsyevd(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), sq2);
EXPECT_DOUBLE_EQ(res.vec(0,0)*res.vec(0,1), -0.5);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), sq2);
EXPECT_DOUBLE_EQ(res.vec(1,0)*res.vec(1,1), +0.5);
}
}
TEST(Diag, zheev) {
{
Matrix_traits<std::complex<double>> m(2,2); // Sigma_Z
m(0,0) = -1.0; m(1,1) = +1.0;
m(1,0) = m(0,1) = 0.0;
const auto res = diagonalise_zheev(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), 1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), 0.0);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), 0.0);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), 1.0);
}
{
Matrix_traits<std::complex<double>> m(2,2); // Sigma_X
m(0,0) = m(1,1) = m(1,0) = 0.0;
m(0,1) = 1.0;
const auto res = diagonalise_zheev(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(0,0)*res.vec(0,1)).real(), -0.5);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(1,0)*res.vec(1,1)).real(), +0.5);
}
{
Matrix_traits<std::complex<double>> m(2,2); // Sigma_Y
m(0,0) = m(1,1) = m(1,0) = 0.0;
m(0,1) = std::complex<double>(0.0,1.0);
const auto res = diagonalise_zheev(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(0,0)*res.vec(0,1)).imag(), -0.5);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(1,0)*res.vec(1,1)).imag(), +0.5);
}
}
TEST(Diag, zheevr) {
{
Matrix_traits<std::complex<double>> m(2,2); // Sigma_Z
m(0,0) = -1.0; m(1,1) = +1.0;
m(1,0) = m(0,1) = 0.0;
const auto res = diagonalise_zheevr(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), 1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), 0.0);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), 0.0);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), 1.0);
}
{
Matrix_traits<std::complex<double>> m(2,2); // Sigma_X
m(0,0) = m(1,1) = m(1,0) = 0.0;
m(0,1) = 1.0;
const auto res = diagonalise_zheevr(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(0,0)*res.vec(0,1)).real(), -0.5);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(1,0)*res.vec(1,1)).real(), +0.5);
}
{
Matrix_traits<std::complex<double>> m(2,2); // Sigma_Y
m(0,0) = m(1,1) = m(1,0) = 0.0;
m(0,1) = std::complex<double>(0.0,1.0);
const auto res = diagonalise_zheevr(m);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(0,0)*res.vec(0,1)).imag(), -0.5);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(1,0)*res.vec(1,1)).imag(), +0.5);
}
}
TEST(Diag, diagonalise_complex) {
Params P;
auto DP = DiagParams(P, 0.5);
{
Matrix_traits<std::complex<double>> m(2,2); // Sigma_Z
m(0,0) = -1.0; m(1,1) = +1.0;
m(1,0) = m(0,1) = 0.0;
const auto res = diagonalise(m, DP, -1);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), 1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), 0.0);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), 0.0);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), 1.0);
}
{
Matrix_traits<std::complex<double>> m(2,2); // Sigma_X
m(0,0) = m(1,1) = m(1,0) = 0.0;
m(0,1) = 1.0;
const auto res = diagonalise(m, DP, -1);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(0,0)*res.vec(0,1)).real(), -0.5);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(1,0)*res.vec(1,1)).real(), +0.5);
}
{
Matrix_traits<std::complex<double>> m(2,2); // Sigma_Y
m(0,0) = m(1,1) = m(1,0) = 0.0;
m(0,1) = std::complex<double>(0.0,1.0);
const auto res = diagonalise(m, DP, -1);
EXPECT_EQ(res.getnrcomputed(), 2);
EXPECT_EQ(res.getdim(), 2);
EXPECT_DOUBLE_EQ(res.val[0], -1.0);
EXPECT_DOUBLE_EQ(res.val[1], +1.0);
EXPECT_DOUBLE_EQ(abs(res.vec(0,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(0,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(0,0)*res.vec(0,1)).imag(), -0.5);
EXPECT_DOUBLE_EQ(abs(res.vec(1,0)), sq2);
EXPECT_DOUBLE_EQ(abs(res.vec(1,1)), sq2);
EXPECT_DOUBLE_EQ((res.vec(1,0)*res.vec(1,1)).imag(), +0.5);
}
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS(); // NOLINT
}
| 10,644
|
C++
|
.cpp
| 308
| 30.448052
| 67
| 0.588098
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,525
|
resample2.cpp
|
rokzitko_nrgljubljana/test/unit/resample/resample2.cpp
|
#include <gtest/gtest.h>
#include <resample/resample.hpp>
template<typename T1, typename T2, typename S1, typename S2>
void compare(const std::vector<std::pair<T1,T2>> &a, const std::vector<std::pair<S1,S2>> &b) {
ASSERT_EQ(a.size(), b.size());
for(int i = 0; i < a.size(); i++){
EXPECT_DOUBLE_EQ(a[i].first, b[i].first);
EXPECT_DOUBLE_EQ(a[i].second, b[i].second);
}
}
using namespace NRG;
TEST(resample, basic){
NRG::Resample::Resample<double> resample("txt/resample_input.txt", "txt/resample_grid.txt");
auto const output = resample.run();
auto const output_compare = readtable<double,double>("txt/resample_output.txt");
compare(*output, output_compare);
}
| 686
|
C++
|
.cpp
| 17
| 37.823529
| 94
| 0.699248
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,526
|
invar.cpp
|
rokzitko_nrgljubljana/test/unit/invar/invar.cpp
|
#include <gtest/gtest.h>
#include <invar.hpp>
#include <mk_sym.hpp>
#include "test_common.hpp"
TEST(Invar, initInvar) { // NOLINT
Params P;
auto Sym = setup_Sym<double>(P);
// should have Q and SS degrees of freedom
EXPECT_EQ(Invar::invdim, 2);
EXPECT_EQ(Invar::qntype.size(), 2);
EXPECT_EQ(Invar::qntype[0], additive);
EXPECT_EQ(Invar::qntype[1], additive);
EXPECT_EQ(Invar::names.size(), 2);
EXPECT_EQ(Invar::names["Q"], 0);
EXPECT_EQ(Invar::names["SS"], 1);
}
TEST(Invar, InvarQS) { // NOLINT
Params P;
auto Sym = setup_Sym<double>(P);
{
Invar I(1,2);
}
{
std::string str = "1 2";
std::istringstream ss(str);
Invar I;
ss >> I;
EXPECT_EQ(I, Invar(1,2));
}
{
std::ostringstream ss;
Invar I(1,2);
ss << I;
EXPECT_EQ(ss.str(), "1 2"s);
}
{
Invar I(1,2);
EXPECT_EQ(I.str(), "1 2"s);
EXPECT_EQ(I.name(), "1_2"s);
}
{
Invar I1(1);
EXPECT_EQ(I1.str(), "1"s);
Invar I2(1,2);
EXPECT_EQ(I2.str(), "1 2"s);
Invar I3(1,2,3);
EXPECT_EQ(I3.str(), "1 2 3"s);
}
{
Invar I1(1,1);
Invar I2(1,2);
EXPECT_EQ(I1 == I1, true);
EXPECT_EQ(I1 != I2, true);
EXPECT_EQ(I1 < I2, true);
}
{
Invar I(1,2);
EXPECT_EQ(I.getqn(0), 1);
EXPECT_EQ(I.getqn(1), 2);
}
{
Invar I1(2,2);
Invar I2(3,2);
I1.combine(I2);
EXPECT_EQ(I1, Invar(5, 4));
}
{
Invar I(1,2);
I.inverse();
EXPECT_EQ(I, Invar(-1,-2)); // both additive!
}
{
Invar I(1,2);
EXPECT_EQ(I.get("Q"), 1);
EXPECT_EQ(I.get("SS"), 2);
}
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS(); // NOLINT
}
| 1,692
|
C++
|
.cpp
| 81
| 17.148148
| 49
| 0.561294
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
1,535,527
|
outfield.cpp
|
rokzitko_nrgljubljana/test/unit/outfield/outfield.cpp
|
#include <gtest/gtest.h>
#include <string>
using namespace std::string_literals;
#include <fstream>
#include <exception>
#include <fmt/format.h>
#include <outfield.hpp>
constexpr int prec = 10;
constexpr int width = 20;
TEST(outfield, Outfield) {
const auto desc = "Sz"s;
auto o = NRG::Outfield(desc, prec, width);
EXPECT_EQ(o.get_desc(), desc);
const auto ref_h = fmt::format("{:>20} ", desc);
std::ostringstream s_header;
o.put_header(s_header);
EXPECT_EQ(ref_h, s_header.str());
const auto ref_v_nul = fmt::format("{:>20} ", ""s);
std::ostringstream s_value_nul;
o.put_value(s_value_nul);
EXPECT_EQ(ref_v_nul, s_value_nul.str());
constexpr auto val = 12.0;
o.set_value(val);
const auto ref_v = fmt::format("{:>20.10} ", val);
std::ostringstream s_value;
o.put_value(s_value);
EXPECT_EQ(ref_v, s_value.str());
}
TEST(outfield, many) {
const std::vector fields = { "A"s, "B"s, "C"s };
NRG::Allfields all(fields, prec, width);
std::ostringstream s_header;
all.save_header(s_header);
const auto ref_h = fmt::format("#{:>20} {:>20} {:>20} \n", "A"s, "B"s, "C"s);
EXPECT_EQ(ref_h, s_header.str());
all.set("A"s, 1.0);
all.set("B"s, 2.0);
all.set("C"s, 3.0);
const auto ref_v = fmt::format(" {:>20.10} {:>20.10} {:>20.10} \n", 1.0, 2.0, 3.0);
std::ostringstream s_values;
all.save_values(s_values);
EXPECT_EQ(ref_v, s_values.str());
EXPECT_THROW(all.set("D"s, 4.0), std::runtime_error);
}
TEST(outfield, add1) {
const std::vector fields = { "A"s, "C"s };
NRG::Allfields all(fields, prec, width);
all.add("B", 1);
std::ostringstream s_header;
all.save_header(s_header);
const auto ref_h = fmt::format("#{:>20} {:>20} {:>20} \n", "A"s, "B"s, "C"s);
EXPECT_EQ(ref_h, s_header.str());
}
TEST(outfield, add2) {
const std::vector fields = { "A"s, "D"s };
NRG::Allfields all(fields, prec, width);
all.add("B", 1);
all.add("C", 2);
std::ostringstream s_header;
all.save_header(s_header);
const auto ref_h = fmt::format("#{:>20} {:>20} {:>20} {:>20} \n", "A"s, "B"s, "C"s, "D"s);
EXPECT_EQ(ref_h, s_header.str());
}
TEST(outfield, merge1) {
const std::vector fields1 = { "A"s, "D"s };
NRG::Allfields all(fields1, prec, width);
const std::vector fields2 = { "B"s, "C"s };
NRG::Allfields more(fields2, prec, width);
all.add(more);
std::ostringstream s_header;
all.save_header(s_header);
const auto ref_h = fmt::format("#{:>20} {:>20} {:>20} {:>20} \n", "A"s, "D"s, "B"s, "C"s);
EXPECT_EQ(ref_h, s_header.str());
}
TEST(outfield, merge2) {
const std::vector fields1 = { "A"s, "D"s };
NRG::Allfields all(fields1, prec, width);
const std::vector fields2 = { "B"s, "C"s };
NRG::Allfields more(fields2, prec, width);
all.add(more, 1);
std::ostringstream s_header;
all.save_header(s_header);
const auto ref_h = fmt::format("#{:>20} {:>20} {:>20} {:>20} \n", "A"s, "B"s, "C"s, "D"s);
EXPECT_EQ(ref_h, s_header.str());
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
| 3,055
|
C++
|
.cpp
| 89
| 31.561798
| 92
| 0.620455
|
rokzitko/nrgljubljana
| 31
| 8
| 4
|
GPL-3.0
|
9/20/2024, 10:44:18 PM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.