Skip to content

Commit 617a1ff

Browse files
committed
add tests
1 parent 4c09328 commit 617a1ff

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

common/arg.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ struct common_hf_file_res {
162162

163163
#ifdef LLAMA_USE_CURL
164164

165+
bool common_has_curl() {
166+
return true;
167+
}
168+
165169
#ifdef __linux__
166170
#include <linux/limits.h>
167171
#elif defined(_WIN32)
@@ -534,6 +538,7 @@ std::pair<long, std::vector<char>> common_remote_get_content(const std::string &
534538

535539
curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str());
536540
curl_easy_setopt(curl.get(), CURLOPT_NOPROGRESS, 1L);
541+
curl_easy_setopt(curl.get(), CURLOPT_FOLLOWLOCATION, 1L);
537542
typedef size_t(*CURLOPT_WRITEFUNCTION_PTR)(void * ptr, size_t size, size_t nmemb, void * data);
538543
auto write_callback = [](void * ptr, size_t size, size_t nmemb, void * data) -> size_t {
539544
auto data_vec = static_cast<std::vector<char> *>(data);
@@ -561,7 +566,7 @@ std::pair<long, std::vector<char>> common_remote_get_content(const std::string &
561566

562567
if (res != CURLE_OK) {
563568
std::string error_msg = curl_easy_strerror(res);
564-
throw std::runtime_error("error: cannot make GET request : " + error_msg);
569+
throw std::runtime_error("error: cannot make GET request: " + error_msg);
565570
}
566571

567572
long res_code;
@@ -642,6 +647,10 @@ static struct common_hf_file_res common_get_hf_file(const std::string & hf_repo_
642647

643648
#else
644649

650+
bool common_has_curl() {
651+
return false;
652+
}
653+
645654
static bool common_download_file_single(const std::string &, const std::string &, const std::string &) {
646655
LOG_ERR("error: built without CURL, cannot download model from internet\n");
647656
return false;

common/arg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ bool common_params_parse(int argc, char ** argv, common_params & params, llama_e
7878

7979
// function to be used by test-arg-parser
8080
common_params_context common_params_parser_init(common_params & params, llama_example ex, void(*print_usage)(int, char **) = nullptr);
81+
bool common_has_curl();
8182

8283
struct common_remote_params {
8384
std::vector<std::string> headers;

tests/test-arg-parser.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,53 @@ int main(void) {
126126
assert(params.cpuparams.n_threads == 1010);
127127
#endif // _WIN32
128128

129+
if (common_has_curl()) {
130+
printf("test-arg-parser: test curl-related functions\n\n");
131+
const char * GOOD_URL = "https://raw.githubusercontent.com/ggml-org/llama.cpp/refs/heads/master/README.md";
132+
const char * BAD_URL = "https://www.google.com/404";
133+
const char * BIG_FILE = "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-large-v1.bin";
134+
135+
{
136+
printf("test-arg-parser: test good URL\n\n");
137+
auto res = common_remote_get_content(GOOD_URL, {});
138+
assert(res.first == 200);
139+
assert(res.second.size() > 0);
140+
std::string str(res.second.data(), res.second.size());
141+
assert(str.find("llama.cpp") != std::string::npos);
142+
}
143+
144+
{
145+
printf("test-arg-parser: test bad URL\n\n");
146+
auto res = common_remote_get_content(BAD_URL, {});
147+
assert(res.first == 404);
148+
}
149+
150+
{
151+
printf("test-arg-parser: test max size error\n");
152+
common_remote_params params;
153+
params.max_size = 1;
154+
try {
155+
common_remote_get_content(GOOD_URL, params);
156+
assert(false && "it should throw an error");
157+
} catch (std::exception & e) {
158+
printf(" expected error: %s\n\n", e.what());
159+
}
160+
}
161+
162+
{
163+
printf("test-arg-parser: test timeout error\n");
164+
common_remote_params params;
165+
params.timeout = 1;
166+
try {
167+
common_remote_get_content(BIG_FILE, params);
168+
assert(false && "it should throw an error");
169+
} catch (std::exception & e) {
170+
printf(" expected error: %s\n\n", e.what());
171+
}
172+
}
173+
} else {
174+
printf("test-arg-parser: no curl, skipping curl-related functions\n");
175+
}
129176

130177
printf("test-arg-parser: all tests OK\n\n");
131178
}

0 commit comments

Comments
 (0)