Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
akokoshn authored and akokoshn committed Nov 17, 2023
1 parent 23e3483 commit 63d5736
Show file tree
Hide file tree
Showing 23 changed files with 314 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tests/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,16 @@ add_zkllvm_unit_test("algebra/de_composition/decomposition/lsb")
#multi provers tests
add_zkllvm_unit_test("multi_provers/shift_add")

#customer's tests
add_zkllvm_unit_test("custom_cases/getelementptr_as_constant/getelementptr_as_constant")

#libc functions
add_zkllvm_unit_test("libc/memset/memset")
add_zkllvm_unit_test("libc/strlen/strlen")
add_zkllvm_unit_test("libc/strcmp/strcmp")
add_zkllvm_unit_test("libc/strncmp/strncmp")
add_zkllvm_unit_test("libc/strcpy/strcpy")
add_zkllvm_unit_test("libc/strncpy/strncpy")

add_dependencies(all_for_test_run all_circuit_tests)
add_dependencies(all_for_test_run all_expected_res_tests)
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef __ZKLLVM__
#include "../../read_boost_json.hpp"
#include <cstdint>
#include <fstream>
#endif

#include <nil/crypto3/algebra/curves/pallas.hpp>

struct lumpinfo_s
{
lumpinfo_s * next;
lumpinfo_s * prev;
int data1;
};

lumpinfo_s first;

[[circuit]] int list_demo(int unused) {

first.next = first.prev = &first;

#ifndef __ZKLLVM__
std::cout << 0 <<std::endl;
#endif

return 0;
}

#ifndef __ZKLLVM__

int main (int argc, char *argv[]){
if (argc != 2) {
std::cerr << "one command line argument must be provided\n";
std::abort();
}

boost::json::value input_json = read_boost_json(std::string(argv[1]));

int a = read_uint<int>(input_json, 0);

list_demo(a);
return 0;
}
#endif
41 changes: 41 additions & 0 deletions tests/cpp/libc/memset/memset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef __ZKLLVM__
#include "../../read_boost_json.hpp"
#include <cstdint>
#include <fstream>
#endif

#include <nil/crypto3/algebra/curves/pallas.hpp>

[[circuit]] uint32_t test_func(char *buf) {
uint32_t out = 0;
memset((void*)buf, 5, 2);
out = buf[0] + buf[1];

#ifndef __ZKLLVM__
std::cout << out <<std::endl;
#endif

return out;
}

#ifndef __ZKLLVM__

int main (int argc, char *argv[]){
if (argc != 2) {
std::cerr << "one command line argument must be provided\n";
std::abort();
}

boost::json::value input_json = read_boost_json(std::string(argv[1]));

std::string s = read_string(input_json, 0);

char* buf = new char[s.size()];
strcpy(buf, s.c_str());

test_func(buf);

delete[] buf;
return 0;
}
#endif
36 changes: 36 additions & 0 deletions tests/cpp/libc/strcmp/strcmp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef __ZKLLVM__
#include "../../read_boost_json.hpp"
#include <cstdint>
#include <fstream>
#endif

#include <nil/crypto3/algebra/curves/pallas.hpp>

[[circuit]] int test_func(const char *s1, const char *s2) {
int out = strcmp(s1, s2);

#ifndef __ZKLLVM__
std::cout << out <<std::endl;
#endif

return out;
}

#ifndef __ZKLLVM__

int main (int argc, char *argv[]){
if (argc != 2) {
std::cerr << "one command line argument must be provided\n";
std::abort();
}

boost::json::value input_json = read_boost_json(std::string(argv[1]));

std::string s1 = read_string(input_json, 0);
std::string s2 = read_string(input_json, 1);

test_func(s1.c_str(), s2.c_str());

return 0;
}
#endif
40 changes: 40 additions & 0 deletions tests/cpp/libc/strcpy/strcpy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef __ZKLLVM__
#include "../../read_boost_json.hpp"
#include <cstdint>
#include <fstream>
#endif

#include <nil/crypto3/algebra/curves/pallas.hpp>

[[circuit]] uint32_t test_func(char *dst, const char *src) {
uint32_t out = 0;
char* ch = strcpy(dst, src);
out = ch[0] + ch[1];

#ifndef __ZKLLVM__
std::cout << out <<std::endl;
#endif

return out;
}

#ifndef __ZKLLVM__

int main (int argc, char *argv[]){
if (argc != 2) {
std::cerr << "one command line argument must be provided\n";
std::abort();
}

boost::json::value input_json = read_boost_json(std::string(argv[1]));

std::string s = read_string(input_json, 0);

char* buf = new char[s.size()];

test_func(buf, s.c_str());

delete[] buf;
return 0;
}
#endif
36 changes: 36 additions & 0 deletions tests/cpp/libc/strlen/strlen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef __ZKLLVM__
#include "../../read_boost_json.hpp"
#include <cstdint>
#include <fstream>
#endif

#include <nil/crypto3/algebra/curves/pallas.hpp>

[[circuit]] uint32_t test_func(const char *buf) {
uint32_t out = 0;
out = strlen(buf);

#ifndef __ZKLLVM__
std::cout << out <<std::endl;
#endif

return out;
}

#ifndef __ZKLLVM__

int main (int argc, char *argv[]){
if (argc != 2) {
std::cerr << "one command line argument must be provided\n";
std::abort();
}

boost::json::value input_json = read_boost_json(std::string(argv[1]));

std::string s = read_string(input_json, 0);

test_func(s.c_str());

return 0;
}
#endif
38 changes: 38 additions & 0 deletions tests/cpp/libc/strncmp/strncmp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef __ZKLLVM__
#include "../../read_boost_json.hpp"
#include <cstdint>
#include <fstream>
#endif

#include <nil/crypto3/algebra/curves/pallas.hpp>

[[circuit]] int test_func(const char *s1, const char *s2, uint32_t n) {
int out = strncmp(s1, s2, n);

#ifndef __ZKLLVM__
std::cout << out <<std::endl;
#endif

return out;
}

#ifndef __ZKLLVM__

int main (int argc, char *argv[]){
if (argc != 2) {
std::cerr << "one command line argument must be provided\n";
std::abort();
}

boost::json::value input_json = read_boost_json(std::string(argv[1]));

std::string s1 = read_string(input_json, 0);
std::string s2 = read_string(input_json, 1);
uint32_t n = read_uint<uint32_t>(input_json, 2);


test_func(s1.c_str(), s2.c_str(), n);

return 0;
}
#endif
42 changes: 42 additions & 0 deletions tests/cpp/libc/strncpy/strncpy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef __ZKLLVM__
#include "../../read_boost_json.hpp"
#include <cstdint>
#include <fstream>
#endif

#include <nil/crypto3/algebra/curves/pallas.hpp>

[[circuit]] uint32_t test_func(char *dst, const char *src, uint32_t n) {
uint32_t out = 0;
char* ch = strncpy(dst, src, n);
out = ch[0] + ch[1];

#ifndef __ZKLLVM__
std::cout << out <<std::endl;
#endif

return out;
}

#ifndef __ZKLLVM__

int main (int argc, char *argv[]){
if (argc != 2) {
std::cerr << "one command line argument must be provided\n";
std::abort();
}

boost::json::value input_json = read_boost_json(std::string(argv[1]));

std::string s = read_string(input_json, 0);
uint32_t n = read_uint<uint32_t>(input_json, 2);

char* buf = new char[s.size()];
memset((void*)buf, 0, s.size());

test_func(buf, s.c_str(), n);

delete[] buf;
return 0;
}
#endif
12 changes: 12 additions & 0 deletions tests/cpp/read_boost_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,16 @@ PointType read_curve(boost::json::value& input_json_value, std::size_t position)
}
}
return res;
}

std::string read_string(boost::json::value& input_json_value, std::size_t position) {

const boost::json::object &current_value = input_json_value.as_array()[position].as_object();
if(!current_value.contains("string"))
assert(false && "json value must contain \"string\"");
if (current_value.at("string").is_string()) {
assert(false && "value is not string.");
}

return current_value.at("string").as_string().c_str();
}
1 change: 1 addition & 0 deletions tests/disabled_inputs/libc/strcmp/0_1.inp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ {"string": "abc"}, {"string": "abx"}]
1 change: 1 addition & 0 deletions tests/disabled_inputs/libc/strncmp/0_1.inp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ {"string": "abc"}, {"string": "abx"}, {"int": 3}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ {"int": 10}]
1 change: 1 addition & 0 deletions tests/inputs/libc/memset/0_0.inp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ {"string": "ab"}]
1 change: 1 addition & 0 deletions tests/inputs/libc/strcmp/0_0.inp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ {"string": "abc"}, {"string": "abc"}]
1 change: 1 addition & 0 deletions tests/inputs/libc/strcmp/0_2.inp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ {"string": "abx"}, {"string": "abc"}]
1 change: 1 addition & 0 deletions tests/inputs/libc/strcmp/0_3.inp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ {"string": "abc"}, {"string": "ab"}]
1 change: 1 addition & 0 deletions tests/inputs/libc/strcpy/0_0.inp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ {"string": "abc"}, {"string": "abc"}]
1 change: 1 addition & 0 deletions tests/inputs/libc/strlen/0_0.inp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ {"string": "abc"}]
1 change: 1 addition & 0 deletions tests/inputs/libc/strncmp/0_0.inp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ {"string": "abc"}, {"string": "abc"}, {"int": 3}]
1 change: 1 addition & 0 deletions tests/inputs/libc/strncmp/0_2.inp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ {"string": "abx"}, {"string": "abc"}, {"int": 3}]
1 change: 1 addition & 0 deletions tests/inputs/libc/strncmp/0_3.inp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ {"string": "abc"}, {"string": "abx"}, {"int": 2}]
1 change: 1 addition & 0 deletions tests/inputs/libc/strncpy/0_0.inp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ {"string": "ab"}, {"string": "ab"}, {"int": 3}]
1 change: 1 addition & 0 deletions tests/inputs/libc/strncpy/0_1.inp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ {"string": "abc"}, {"string": "abc"}, {"int": 2}]

0 comments on commit 63d5736

Please sign in to comment.