END_TEST START_TEST(Read_nonzero_float) { const double nums[] = { 0.5, 1.0, 1.5, -0.5, -1.0, -1.5, 0.0625, 10.0, }; const char* formats[] = { "%.4f x", "%f x", "%e x", }; for (size_t i = 0; i < arr_size(nums); ++i) { for (size_t k = 0; k < arr_size(formats); ++k) { char data[128] = ""; sprintf(data, formats[k], nums[i]); Streader* sr = init_with_cstr(data); double num = NAN; fail_if(!Streader_read_float(sr, &num), "Could not read float from \"%s\": %s", data, Streader_get_error_desc(sr)); fail_if(num != nums[i], "Streader stored %f instead of %.6f from \"%s\"", num, nums[i]); fail_if(!Streader_match_char(sr, 'x'), "Streader did not consume float from \"%s\" correctly", data); } } }
int Set_Union() { /* Get length of arrays */ int lenA = arr_size(SetA); int lenB = arr_size(SetB); /* Sort arrays */ BubbleSort(SetA, lenA); //Sort SetA BubbleSort(SetB, lenB); //Sort SetB /* Get the union */ int i = 0; int j = 0; int unionSize = 0; while(i < lenA && j < lenB) { if(SetA[i] < SetB[j]) { printf(" %d ", SetA[i++]); } else if(SetA[i] > SetB[j]) { printf(" %d ", SetB[j++]); } else { printf(" %d ", SetB[j++]); i++; } unionSize++; } /* Print remaining elements of larger array */ while(i < lenA) { printf(" %d ", SetA[i++]); unionSize++; } while(j < lenB) { printf(" %d ", SetB[j++]); unionSize++; } /* return the union size */ return unionSize; }
inline optional get_sym(table *t, char *id) { if (*id == '$') { return arr_get(t->tmps, (unsigned) atoi(&id[1])); } else if (isdigit(*id) || *id == '.') { unsigned idx; optional opt; for (idx = 0; idx < arr_size(t->lits); idx++) { if (strcmp(((sym *) (opt = arr_get(t->lits, idx)).val)->id, id) == 0) { return opt; } } opt.e = false; opt.err = element_not_found; return opt; } else { unsigned idx; optional opt; for (idx = 0; idx < arr_size(t->syms); idx++) { if (cmp((opt = arr_get(t->syms, idx)).val, id)) { return opt; } } opt.e = false; opt.err = element_not_found; return opt; } }
END_TEST START_TEST(Read_zero_float) { const char* zeros[] = { "0 x", "0.0 x", "0e0 x", "0.0e0 x", "0.0e+0 x", "0.0e-0 x", }; for (size_t i = 0; i < arr_size(zeros); ++i) { Streader* sr = init_with_cstr(zeros[i]); double num = NAN; fail_if(!Streader_read_float(sr, &num), "Could not read 0 from \"%s\": %s", zeros[i], Streader_get_error_desc(sr)); fail_if(num != 0, "Streader stored %f instead of 0 from \"%s\"", num, zeros[i]); fail_if(!Streader_match_char(sr, 'x'), "Streader did not consume 0 from \"%s\" correctly: %s", zeros[i], Streader_get_error_desc(sr)); } // TODO: The code below does not test the sign of negative zero // as C99 doesn't guarantee it. // Revisit when migrating to emulated floats. const char* neg_zeros[] = { "-0 x", "-0.0 x", "-0e0 x", "-0.0e0 x", "-0.0e+0 x", "-0.0e-0 x", }; for (size_t i = 0; i < arr_size(neg_zeros); ++i) { Streader* sr = init_with_cstr(neg_zeros[i]); double num = NAN; fail_if(!Streader_read_float(sr, &num), "Could not read -0 from \"%s\": %s", neg_zeros[i], Streader_get_error_desc(sr)); fail_if(num != 0, "Streader stored %f instead of -0 from \"%s\"", num, neg_zeros[i]); fail_if(!Streader_match_char(sr, 'x'), "Streader did not consume -0 from \"%s\" correctly", neg_zeros[i]); } }
END_TEST START_TEST(Matching_visible_characters_succeed) { static const char* ones[] = { "1", " 1", " 1 ", }; for (size_t i = 0; i < arr_size(ones); ++i) { Streader* sr = init_with_cstr(ones[i]); fail_if(!Streader_match_char(sr, '1'), "Could not match '1' in \"%s\"", ones[i]); fail_if(Streader_is_error_set(sr), "Error set after a successful match in \"%s\"", ones[i]); fail_if(Streader_match_char(sr, '1'), "Streader did not move forwards after a successful match" " in \"%s\"", ones[i]); } static const char* exprs[] = { "1+2", " 1+2", "1 +2", " 1 +2", "1+ 2", "1 + 2", " 1 + 2 ", }; for (size_t i = 0; i < arr_size(exprs); ++i) { Streader* sr = init_with_cstr(exprs[i]); fail_if(!Streader_match_char(sr, '1'), "Could not match '1' in \"%s\"", exprs[i]); fail_if(Streader_is_error_set(sr), "Error set after a successful match in \"%s\"", exprs[i]); fail_if(!Streader_match_char(sr, '+'), "Could not match '+' in \"%s\"", exprs[i]); fail_if(Streader_is_error_set(sr), "Error set after a successful match in \"%s\"", exprs[i]); fail_if(!Streader_match_char(sr, '2'), "Could not match '2' in \"%s\"", exprs[i]); fail_if(Streader_is_error_set(sr), "Error set after a successful match in \"%s\"", exprs[i]); } }
int check_builtins(t_cpe *cpe) { check_cmd_path(cpe); if (ft_strcmp(CMD, "exit") == 0) return (exec_err(cpe)); if (ft_strcmp(CMD, "echo") == 0) return (ft_echo(cpe) + 10); else if (ft_strcmp(CMD, "cd") == 0) return (ft_chdir(cpe)); else if (ft_strcmp(CMD, "env") == 0) return (check_envi(cpe)); else if (ft_strcmp(CMD, "setenv") == 0) { if (!PRM) return (print_arr(ENV) + 10); if (arr_size(PRM) > 2) { ft_putendl("setenv: Too many arguments"); ft_putendl("usage: setenv [name[value]]"); return (11); } return (ft_setenv(cpe) + 10); } else if (ft_strcmp(CMD, "unsetenv") == 0) return (ft_unsetenv(cpe) + 10); else return (-1); }
END_TEST #define make_str(x) #x START_TEST(Reading_invalid_piref_fails) { const char* data[] = { "0, 0]", "[0 0]", "[0, 0", "[0, -1]", "[-1, 0]", "[0, " make_str(KQT_PAT_INSTANCES_MAX) "]", "[" make_str(KQT_PATTERNS_MAX) ", 0]", }; for (size_t i = 0; i < arr_size(data); ++i) { Streader* sr = init_with_cstr(data[i]); Pat_inst_ref* result = PAT_INST_REF_AUTO; fail_if(Streader_read_piref(sr, result), "Streader accepted `%s` as a valid pattern instance reference", data[i]); } }
END_TEST START_TEST(Read_nonzero_int) { char data[128] = ""; static const int64_t nums[] = { 1, 19, INT64_MAX, -1, -19, INT64_MIN }; for (size_t i = 0; i < arr_size(nums); ++i) { sprintf(data, "%" PRId64 " x", nums[i]); Streader* sr = init_with_cstr(data); int64_t num = 0; fail_if(!Streader_read_int(sr, &num), "Could not read %" PRId64, nums[i]); fail_if(num != nums[i], "Streader stored %" PRId64 " instead of %" PRId64, num, nums[i]); fail_if(!Streader_match_char(sr, 'x'), "Streader did not consume %" PRId64 " correctly", nums[i]); } }
END_TEST #undef make_str START_TEST(Read_empty_list) { static const char* lists[] = { "[] x", "[ ]x", "[ ] x", }; for (size_t i = 0; i < arr_size(lists); ++i) { Streader* sr = init_with_cstr(lists[i]); fail_if(!Streader_read_list(sr, NULL, NULL), "Could not read empty list from `%s`: %s", lists[i], Streader_get_error_desc(sr)); fail_if(!Streader_match_char(sr, 'x'), "Streader did not consume empty list from `%s` correctly", lists[i]); } }
END_TEST START_TEST(Characters_past_specified_length_are_ignored) { static struct { const char* str; int len; } nums[] = { { "123456", 3 }, { "123 456", 4 }, { "12 3456", 4 }, }; for (size_t i = 0; i < arr_size(nums); ++i) { Streader* sr = Streader_init(STREADER_AUTO, nums[i].str, nums[i].len); fail_if(!Streader_match_char(sr, '1'), "Could not match '1' in \"%s\"", nums[i].str); fail_if(!Streader_match_char(sr, '2'), "Could not match '2' in \"%s\"", nums[i].str); fail_if(!Streader_match_char(sr, '3'), "Could not match '3' in \"%s\"", nums[i].str); fail_if(Streader_match_char(sr, '4'), "Matched '4' located in \"%s\" after given length %d", nums[i].str, nums[i].len); } }
inline void Evaluate(void) { while (arr_size(operators) != 0) { eval_current(); } arr_reset(operands); arr_reset(operators); //printf("Reset operators and operands\n"); }
inline optional create_sym(table *t, char *id, value val) { optional opt; if (isdigit(*id) || *id == '.') { sym *s = malloc(sizeof(sym)); memset(s, 0, sizeof(sym)); strcpy(s->id, id); s->val = val; opt.e = true; opt.val = s; unsigned idx; unsigned n = arr_size(t->lits); for (idx = 0; idx < n; idx++) { if (strcmp(((sym *) arr_get(t->lits, idx).val)->id, id) == 0) { break; } } if (idx >= n) { arr_push(t->lits, s); } } else { unsigned idx; opt.e = true; for (idx = 0; idx < arr_size(t->syms); idx++) { if (strcmp(((sym *) (opt = arr_get(t->syms, idx)).val)->id, id) == 0) { opt.e = false; opt.err = element_not_found; } } if (opt.e) { sym *s = malloc(sizeof(sym)); memset(s, 0, sizeof(sym)); strcpy(s->id, id); s->val = val; opt.val = s; arr_push(t->syms, s); } } return opt; }
int ft_setenv(t_cpe *cpe) { int i; char *tmp; i = 0; if (err_check(cpe) == 1) return (1); i = re_setenv(cpe, 0); if (i == 1 || i == 0) return (i); ENV = ft_initenv(ENV, 1, cpe); if (arr_size(PRM) == 2) { i = arr_size(ENV) - 1; tmp = ft_strjoin(ENV[i], PRM[1]); free(ENV[i]); ENV[i] = tmp; } return (0); }
void sexp_print_array(secd_t *secd, cell_t *p, const cell_t *cell) { const cell_t *arr = arr_val(cell, 0); const size_t len = arr_size(secd, cell); size_t i; secd_pprintf(secd, p, "#("); for (i = cell->as.arr.offset; i < len; ++i) { sexp_pprint(secd, p, arr + i); secd_pprintf(secd, p, " "); } secd_pprintf(secd, p, ")"); }
END_TEST START_TEST(Read_valid_piref) { static const struct { const char* data; const Pat_inst_ref expected; } pirefs[] = { { "[0, 0]", { 0, 0 } }, { "[2,5]", { 2, 5 } }, { " [2,5]", { 2, 5 } }, { "[ 2,5]", { 2, 5 } }, { "[2 ,5]", { 2, 5 } }, { "[2, 5]", { 2, 5 } }, { "[2,5 ]", { 2, 5 } }, { "[0, 1023]", { 0, KQT_PAT_INSTANCES_MAX - 1 } }, { "[1023, 0]", { KQT_PATTERNS_MAX - 1, 0 } }, { "[1023, 1023]", { KQT_PATTERNS_MAX - 1, KQT_PAT_INSTANCES_MAX - 1 } }, }; for (size_t i = 0; i < arr_size(pirefs); ++i) { char data[128] = ""; sprintf(data, "%s x", pirefs[i].data); Streader* sr = init_with_cstr(data); Pat_inst_ref* result = PAT_INST_REF_AUTO; fail_if(!Streader_read_piref(sr, result), "Could not read pattern instance refernce " PRIpi " from `%s`: %s", PRIVALpi(pirefs[i].expected), data, Streader_get_error_desc(sr)); fail_if(Pat_inst_ref_cmp(result, &pirefs[i].expected) != 0, "Streader stored " PRIpi " instead of " PRIpi " when reading `%s`", PRIVALpi(*result), PRIVALpi(pirefs[i].expected), data); fail_if(!Streader_match_char(sr, 'x'), "Streader did not consume pattern instance from `%s` correctly", data); } }
inline optional create_temp(table *t, value val) { sym *s = malloc(sizeof(sym)); memset(s, 0, sizeof(sym)); sprintf(s->id, "$%u", arr_size(t->tmps)); s->val = val; optional opt; if ((opt.e = arr_push(t->tmps, s))) { opt.val = s; } else { opt.err = malloc_fail; free(s); } return opt; }
/* Driver function to test above function */ int main() { unsigned int x = 0xf0000000; char ch, *chp, **chpp, ***chppp, ****chpppp; printf("Size of int is %ld\n", sizeof(x)); printf("%x\n", reverseBits(x)); chp = &ch; chpp = &chp; chppp = &chpp; chpppp = &chppp; foo (chp, chpp, chpp, chppp); arr_size(); test_operator(); return 1; }
END_TEST START_TEST(Read_valid_string) { static const struct { const char* data; const char* expected; } strings[] = { { "\"\"", "" }, { "\" \"", " " }, { "\" \"", " " }, { "\"\\\"\"", "\"" }, { "\"\\\\\"", "\\" }, { "\"\\/\"", "/" }, { "\"/\"", "/" }, /* { "\"\\b\"", "\b" }, { "\"\\f\"", "\f" }, { "\"\\n\"", "\n" }, { "\"\\r\"", "\r" }, { "\"\\t\"", "\t" }, */ { "\"abc def\"", "abc def" }, }; for (size_t i = 0; i < arr_size(strings); ++i) { char data[128] = ""; sprintf(data, "%s x", strings[i].data); Streader* sr = init_with_cstr(data); char result[128] = "zzz"; fail_if(!Streader_read_string(sr, 128, result), "Could not read string `%s`: %s", data, Streader_get_error_desc(sr)); fail_if(strcmp(result, strings[i].expected) != 0, "Streader stored `%s` instead of `%s`", result, strings[i].expected); fail_if(!Streader_match_char(sr, 'x'), "Streader did not consume string `%s` correctly"); } }
END_TEST START_TEST(Read_valid_tstamp) { static const struct { const char* data; const Tstamp expected; } tstamps[] = { { "[0, 0]", { 0, 0 } }, { "[2,5]", { 2, 5 } }, { " [2,5]", { 2, 5 } }, { "[ 2,5]", { 2, 5 } }, { "[2 ,5]", { 2, 5 } }, { "[2, 5]", { 2, 5 } }, { "[2,5 ]", { 2, 5 } }, { "[-1, 3]", { -1, 3 } }, { "[0, 882161279]", { 0, KQT_TSTAMP_BEAT - 1 } }, }; for (size_t i = 0; i < arr_size(tstamps); ++i) { char data[128] = ""; sprintf(data, "%s x", tstamps[i].data); Streader* sr = init_with_cstr(data); Tstamp* result = Tstamp_set(TSTAMP_AUTO, 99, 99); fail_if(!Streader_read_tstamp(sr, result), "Could not read timestamp " PRIts " from `%s`: %s", PRIVALts(tstamps[i].expected), data, Streader_get_error_desc(sr)); fail_if(Tstamp_cmp(result, &tstamps[i].expected) != 0, "Streader stored " PRIts " instead of " PRIts " when reading `%s`", PRIVALts(*result), PRIVALts(tstamps[i].expected), data); fail_if(!Streader_match_char(sr, 'x'), "Streader did not consume timestamp from `%s` correctly", data); } }
inline void OperatorFound(OpCode_type op) { /* if (op_func_is_binary[op]) { //printf("add operator %s\n", opcode_str[op]); } */ optional opt; while (arr_size(operators) > 0 && (opt = arr_peek(operators)).e && operatorPrecedence[(OpCode_type) opt.val] < operatorPrecedence[op]) { eval_current(); } if (!opt.e) { stop(); } arr_push(operators, (void *) op); }
static void avl_clear_intern (avl_t avl, int free_data) { arr_t arr; avl_entry_t curr; size_t size; /* Create a new array to be used as a stack */ arr = arr_init (); /* Iterate over every node */ arr_insert_back (arr, avl->root); while ((size = arr_size (arr)) > 0) { /* Get the current element from the stack */ curr = (avl_entry_t) arr_get (arr, size-1); arr_remove (arr, size-1); /* Check for invalid nodes */ if (curr == NULL) continue; /* Push children onto the stack */ arr_insert_back (arr, curr->left); arr_insert_back (arr, curr->right); /* Free parent */ if (free_data && curr->data != NULL) free (curr->data); avl->free (curr->key); free (curr); } /* Cleanup the array */ arr_destroy (arr); /* Update AVL Struct */ avl->size = 0; avl->root = NULL; return; }
END_TEST START_TEST(Reading_invalid_string_fails) { const char* data[] = { "abc\"", "\"abc", "abc", "\"\\z\"", "\"\n\"", }; for (size_t i = 0; i < arr_size(data); ++i) { Streader* sr = init_with_cstr(data[i]); char str[128] = ""; fail_if(Streader_read_string(sr, 128, str), "Streader accepted `%s` as a valid string", data[i]); } }
int re_setenv(t_cpe *cpe, int i) { char *tmp; while (ENV[i]) { if (ft_strncmp(ENV[i], PRM[0], ft_strlen(PRM[0])) == 0 && ENV[i][ft_strlen(PRM[0])] == '=') { free(ENV[i]); if (arr_size(PRM) == 2) return (new_env_val(cpe, i)); else { tmp = ft_strjoin(PRM[0], "="); ENV[i] = tmp; return (0); } } i++; } return (-1); }
END_TEST START_TEST(Read_empty_dict) { static const char* dicts[] = { "{} x", "{ }x", "{ } x", }; for (size_t i = 0; i < arr_size(dicts); ++i) { Streader* sr = init_with_cstr(dicts[i]); fail_if(!Streader_read_dict(sr, NULL, NULL), "Could not read empty dictionary from `%s`: %s", dicts[i], Streader_get_error_desc(sr)); fail_if(!Streader_match_char(sr, 'x'), "Streader did not consume empty dictionary from `%s` correctly", dicts[i]); } }
END_TEST START_TEST(Reading_invalid_tstamp_fails) { const char* data[] = { "0, 0]", "[0 0]", "[0, 0", "[0, -1]", "[0, 882161280]", }; for (size_t i = 0; i < arr_size(data); ++i) { Streader* sr = init_with_cstr(data[i]); Tstamp* result = TSTAMP_AUTO; fail_if(Streader_read_tstamp(sr, result), "Streader accepted `%s` as a valid timestamp", data[i]); } }
static avl_error_t avl_insert_intern (avl_t avl, void * key, void * data, int replace) { arr_t stack; size_t size; avl_entry_t *curr, tmp; int ret; /* Create a stack for saving the path */ stack = arr_init (); if (stack == NULL) return AVL_MALLOC_FAILED; /* Iterate down the the insertion point and update the balance */ curr = &avl->root; while (*curr != NULL) { /* Insert the node into the backlog */ arr_insert_back (stack, curr); /* Determine the correct traveral direction */ ret = avl->compare (key, (*curr)->key); if (ret == 0) { arr_destroy (stack); if (replace) { (*curr)->data = data; return AVL_OK; } else return AVL_KEY_EXISTS; } else if (ret < 0) { (*curr)->balance--; curr = &(*curr)->left; } else { (*curr)->balance++; curr = &(*curr)->right; } } /* Create the new entry */ tmp = (avl_entry_t) malloc (sizeof (struct _avl_entry_t)); if (tmp == NULL) return AVL_MALLOC_FAILED; tmp->left = NULL; tmp->right = NULL; tmp->balance = 0; tmp->key = key; tmp->data = data; /* Insert the entry */ *curr = tmp; /* Rebalance the tree upward */ while ((size = arr_size (stack)) > 0) { break; } /* Increase the Tree Size */ avl->size++; /* Destroy the path stack */ arr_destroy (stack); return AVL_OK; }
void _string() { // revline ("hello world"); arr_size(); }
int main() { int a[] = {2,3,4,5}; std::cout << arr_size(a) << std::endl; }
EIF_BOOLEAN basic_exec_posix_execute(se_exec_data_t*data, char*prog, char**args, EIF_BOOLEAN keep_env, char**add_env, int* in_fd, int* out_fd, int* err_fd) { int id = fork(); if (id == 0) { /* child */ if(in_fd) { dup2(in_fd[0], 0); close(in_fd[1]); } if(out_fd) { dup2(out_fd[1], 1); close(out_fd[0]); } if(err_fd) { dup2(err_fd[1], 2); close(err_fd[0]); } if (prog == NULL && args == NULL) { data->running = 1; data->child = 1; #ifdef SE_SEDB sedb_duplicate(); #endif return 1; } else { if (add_env == NULL && keep_env) { execvp(prog, args); /* NO RETURN in child */ se_print_run_time_stack(); exit(1); }else{ char** new_env; char** old_env; int old_size, add_size; int src, dest = 0; if(keep_env){ old_env = environ; }else{ old_env = envp(); } old_size = arr_size(old_env); add_size = arr_size(add_env); new_env = malloc(sizeof(void*) * (old_size + add_size)); /* we first copy the pointers from the old env */ for(src = 0; src < old_size; src++){ new_env[dest++] = old_env[src]; } /* now the ones from add_env */ for(src = 0; src < add_size; src++){ int override = find_variable(old_env, add_env[src]); if (override >= 0){ new_env[override] = add_env[src]; }else{ new_env[dest++] = add_env[src]; } } execve(prog, args, new_env); /* NO RETURN in child */ se_print_run_time_stack(); exit(1); } } }
no_inline void rnd (u32 num) { /* template parameter */ T_int_t* arr = mem_alloc_auto (arr_size (arr, num)); }