Skip to content

Commit

Permalink
Some examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePhD committed Nov 9, 2019
1 parent 027705c commit a7dbc1c
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/foo.txt
@@ -0,0 +1 @@
Foo
10 changes: 10 additions & 0 deletions examples/foo/art.txt
@@ -0,0 +1,10 @@
__ _
.-.' `; `-._ __ _
(_, .-:' `; `-._
,'o"( (_, )
(__,-' ,'o"( )>
( (__,-' )
`-'._.--._( )
||| |||`-'._.--._.-'
||| |||

11 changes: 11 additions & 0 deletions examples/main0.art.c
@@ -0,0 +1,11 @@
// :D

#include <stdio.h>

int main () {
const char str_data[] =
#embed_str "foo/art.txt"
;
puts(str_data);
return 0;
}
18 changes: 18 additions & 0 deletions examples/main0.c
@@ -0,0 +1,18 @@
// :D

#include <stdio.h>

int main () {
const unsigned char data[] =
#embed __FILE__
;
const char str_data[] =
#embed_str __FILE__
;
puts(str_data);
return !(str_data[0] == '/'
&& str_data[1] == '/'
&& str_data[2] == ' '
&& data[3] == ':'
&& data[4] == 'D');
}
7 changes: 7 additions & 0 deletions examples/main1.art.cpp
@@ -0,0 +1,7 @@
#include <phd/embed.hpp>


int main () {
constexpr std::span<const char> data = std::embed("foo/art.txt");
return data[0];
}
19 changes: 19 additions & 0 deletions examples/main1.c
@@ -0,0 +1,19 @@
// :D

#include <stdio.h>

int main () {
const unsigned char data[] =
#embed 5 __FILE__
;
const char str_data[] =
#embed_str 3 __FILE__
;
puts(str_data);
return !(str_data[0] == '/'
&& str_data[1] == '/'
&& str_data[2] == ' '
&& str_data[3] == '\0'
&& data[3] == ':'
&& data[4] == 'D');
}
13 changes: 13 additions & 0 deletions examples/main1.rnd.c
@@ -0,0 +1,13 @@
// :D

#include <stdio.h>

int main () {
const unsigned char data[] =
#embed 5 "/dev/urandom"
;
const char str_data[] =
#embed_str 3 "/dev/urandom"
;
return str_data[0] + data[0];
}
6 changes: 6 additions & 0 deletions examples/main2.art.cpp
@@ -0,0 +1,6 @@
#include <phd/embed.hpp>

int main () {
constexpr std::span<const char> data = std::embed("art.txt");
return data[0];
}
20 changes: 20 additions & 0 deletions examples/main2.cpp
@@ -0,0 +1,20 @@
// >:3

#include <cstddef>
#include <utility>
#include <cassert>

int main () {
const char* data = nullptr;
const char* trunc_data = nullptr;
const size_t data_size = __builtin_embed("foo.txt.txt", 7, &data);
const size_t trunc_data_size = __builtin_embed_n("foo.txt.wdhawdkaw", 7, &trunc_data, 1);
int arr[trunc_data_size]{};
assert(data_size == 3);
assert(data[0] == 'F');
assert(data[1] == 'o');
assert(data[2] == 'o');
assert(trunc_data_size == 1);
assert(trunc_data[0] == 'F');
return arr[0];
}
11 changes: 11 additions & 0 deletions examples/main3.cpp
@@ -0,0 +1,11 @@
// Nice.

#include <cstddef>
#include <cassert>

int main () {
const char* data = nullptr;
std::size_t data_size = __builtin_embed_n("/dev/urandom", 12, &data, 1);
assert(data_size == 1);
return data[0];
}
6 changes: 6 additions & 0 deletions examples/main4.cpp
@@ -0,0 +1,6 @@
#include <phd/embed.hpp>

int main () {
constexpr std::span<const char> data = std::embed("foo.txt");
return data[0];
}
25 changes: 25 additions & 0 deletions examples/main5.cpp
@@ -0,0 +1,25 @@
#include <phd/embed.hpp>

#include <cstdint>

constexpr std::uint64_t val_64_const = 0xcbf29ce484222325;
constexpr std::uint64_t prime_64_const = 0x100000001b3;

inline constexpr std::uint64_t
hash_64_fnv1a_const(const char* const ptr, std::size_t ptr_size, const std::uint64_t value = val_64_const) noexcept {
return (ptr_size == 1)
? value :
hash_64_fnv1a_const(&ptr[1], ptr_size - 1, (value ^ static_cast<std::uint64_t>(ptr[0])) * prime_64_const);
}

#include <iostream>

int main () {
constexpr std::span<const char> art_data = std::embed("art.txt");
constexpr std::uint64_t expected = 12781078433878002033;
constexpr std::uint64_t actual = hash_64_fnv1a_const(art_data.data(), art_data.size());

static_assert(expected == actual, "🚨 SUSPICIOUS ART SUSPICIOUS ART 🚨");

return 0;
}
26 changes: 26 additions & 0 deletions examples/main6.cpp
@@ -0,0 +1,26 @@
#include <phd/embed.hpp>

#include <cstdint>

inline constexpr std::uint64_t
compute_stuff(const char* const ptr, std::size_t ptr_size, std::uint64_t seed = 0, std::size_t limit = 5) noexcept {
const char* first = ptr;
const char* last = ptr + ptr_size;
for (const char* first = ptr; first != last; ++first) {
const char val = *first;
if (limit > 1 && val > 126) {
std::span<const char> more = std::embed("foo.txt");
seed = compute_stuff(more.data(), more.size(), seed, limit - 1);
}
seed ^= *first;
}
return seed;
}

int main () {
constexpr std::span<const char> art_data = std::embed("foo/art.txt");
constexpr std::uint64_t actual = compute_stuff(art_data.data(), art_data.size());
static_assert(actual == 103);

return static_cast<int>(actual);
}

0 comments on commit a7dbc1c

Please sign in to comment.