about summary refs log tree commit diff stats
path: root/tests/unittests/test_common.h
Commit message (Collapse)AuthorAgeFilesLines
* Apply coding styleMichael Vetter2020-07-071-41/+41
|
* Revert "Apply coding style"Michael Vetter2020-07-071-41/+41
| | | | | | This reverts commit 9b55f2dec0ea27a9ce4856e303425e12f866cea2. Sorting the includes creates some problems.
* Apply coding styleMichael Vetter2020-07-071-41/+41
| | | | Regards https://github.com/profanity-im/profanity/issues/1396
* Move p_sha1_hash() to stanza.cMichael Vetter2018-09-061-8/+0
| | | | | | | | | | | | Move `p_sha1_hash()` from `common.c` to `xmpp/stanza.c` as it is only used in this file and now depends on libstrophe so xmpp is a better namespace folder. Renaming it as `_stanza_create_sha1_hash()`. And making static since only used here. The function cannot be tested in the unit tests anymore. Once functional tests are working again we should write a test for the sha1 functionality.
* Move window functions to window_list.cJames Booth2016-07-241-19/+0
|
* Added whole word matches for room mentionJames Booth2016-04-071-1/+2
|
* Added prof_strstr functionJames Booth2016-04-031-0/+1
|
* Fixed OTR decryption checkJames Booth2015-08-271-0/+1
|
* Added str_contains_str to commonJames Booth2015-08-261-0/+7
|
* Moved all tests to tests folderJames Booth2015-06-121-0/+58
ref='#n219'>219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
//: Transform to maintain multiple variants of a recipe depending on the
//: number and types of the ingredients and products. Allows us to use nice
//: names like 'print' or 'length' in many mutually extensible ways.

:(scenario static_dispatch)
def main [
  7:num/raw <- test 3
]
def test a:num -> z:num [
  z <- copy 1
]
def test a:num, b:num -> z:num [
  z <- copy 2
]
+mem: storing 1 in location 7

//: When loading recipes, accumulate variants if headers don't collide, and
//: flag an error if headers collide.

:(before "End Globals")
map<string, vector<recipe_ordinal> > Recipe_variants;
:(before "End One-time Setup")
put(Recipe_variants, "main", vector<recipe_ordinal>());  // since we manually added main to Recipe_ordinal

:(before "End Globals")
map<string, vector<recipe_ordinal> > Recipe_variants_snapshot;
:(before "End save_snapshots")
Recipe_variants_snapshot = Recipe_variants;
:(before "End restore_snapshots")
Recipe_variants = Recipe_variants_snapshot;

:(before "End Load Recipe Header(result)")
// there can only ever be one variant for main
if (result.name != "main" && contains_key(Recipe_ordinal, result.name)) {
  const recipe_ordinal r = get(Recipe_ordinal, result.name);
  if (!contains_key(Recipe, r) || get(Recipe, r).has_header) {
    string new_name = matching_variant_name(result);
    if (new_name.empty()) {
      // variant doesn't already exist
      new_name = next_unused_recipe_name(result.name);
      put(Recipe_ordinal, new_name, Next_recipe_ordinal++);
      get_or_insert(Recipe_variants, result.name).push_back(get(Recipe_ordinal, new_name));
    }
    trace("load") << "switching " << result.name << " to " << new_name << end();
    result.name = new_name;
    result.is_autogenerated = true;
  }
}
else {
  // save first variant
  put(Recipe_ordinal, result.name, Next_recipe_ordinal++);
  get_or_insert(Recipe_variants, result.name).push_back(get(Recipe_ordinal, result.name));
}

:(code)
string matching_variant_name(const recipe& rr) {
  const vector<recipe_ordinal>& variants = get_or_insert(Recipe_variants, rr.name);
  for (int i = 0;  i < SIZE(variants);  ++i) {
    if (!contains_key(Recipe, variants.at(i))) continue;
    const recipe& candidate = get(Recipe, variants.at(i));
    if (!all_reagents_match(rr, candidate)) continue;
    return candidate.name;
  }
  return "";
}

bool all_reagents_match(const recipe& r1, const recipe& r2) {
  if (SIZE(r1.ingredients) != SIZE(r2.ingredients)) return false;
  if (SIZE(r1.products) != SIZE(r2.products)) return false;
  for (int i = 0;  i < SIZE(r1.ingredients);  ++i) {
    expand_type_abbreviations(r1.ingredients.at(i).type);
    expand_type_abbreviations(r2.ingredients.at(i).type);
    if (!deeply_equal_type_names(r1.ingredients.at(i), r2.ingredients.at(i)))
      return false;
  }
  for (int i = 0;  i < SIZE(r1.products);  ++i) {
    expand_type_abbreviations(r1.products.at(i).type);
    expand_type_abbreviations(r2.products.at(i).type);
    if (!deeply_equal_type_names(r1.products.at(i), r2.products.at(i)))
      return false;
  }
  return true;
}

:(before "End Globals")
set<string> Literal_type_names;
:(before "End One-time Setup")
Literal_type_names.insert("number");
Literal_type_names.insert("character");
:(code)
bool deeply_equal_type_names(const reagent& a, const reagent& b) {
  return deeply_equal_type_names(a.type, b.type);
}
bool deeply_equal_type_names(const type_tree* a, const type_tree* b) {
  if (!a) return !b;
  if (!b) return !a;
  if (a->atom != b->atom) return false;
  if (a->atom) {
    if (a->name == "literal" && b->name == "literal")
      return true;
    if (a->name == "literal")
      return Literal_type_names.find(b->name) != Literal_type_names.end();
    if (b->name == "literal")
      return Literal_type_names.find(a->name) != Literal_type_names.end();
    return a->name == b->name;
  }
  return deeply_equal_type_names(a->left, b->left)
      && deeply_equal_type_names(a->right, b->right);
}

string next_unused_recipe_name(const string& recipe_name) {
  for (int i = 2;  /*forever*/;  ++i) {
    ostringstream out;
    out << recipe_name << '_' << i;
    if (!contains_key(Recipe_ordinal, out.str()))
      return out.str();
  }
}

//: Once all the recipes are loaded, transform their bodies to replace each
//: call with the most suitable variant.

:(scenario static_dispatch_picks_most_similar_variant)
def main [
  7:num/raw <- test 3, 4, 5
]
def test a:num -> z:num [
  z <- copy 1
]
def test a:num, b:num -> z:num [
  z <- copy 2
]
+mem: storing 2 in location 7

//: support recipe headers in a previous transform to fill in missing types
:(before "End check_or_set_invalid_types")
for (int i = 0;  i < SIZE(caller.ingredients);  ++i)
  check_or_set_invalid_types(caller.ingredients.at(i).type, maybe(caller.name), "recipe header ingredient");
for (int i = 0;  i < SIZE(caller.products);  ++i)
  check_or_set_invalid_types(caller.products.at(i).type, maybe(caller.name), "recipe header product");

//: save original name of recipes before renaming them
:(before "End recipe Fields")
string original_name;
//: original name is only set during load
:(before "End Load Recipe Name")
result.original_name = result.name;

//: after filling in all missing types (because we'll be introducing 'blank' types in this transform in a later layer, for shape-shifting recipes)
:(after "Transform.push_back(transform_names)")
Transform.push_back(resolve_ambiguous_calls);  // idempotent

//: In a later layer we'll introduce recursion in resolve_ambiguous_calls, by
//: having it generate code for shape-shifting recipes and then transform such
//: code. This data structure will help error messages be more useful.
//:
//: We're punning the 'call' data structure just because it has slots for
//: calling recipe and calling instruction.
:(before "End Globals")
list<call> Resolve_stack;

:(code)
void resolve_ambiguous_calls(const recipe_ordinal r) {
  recipe& caller_recipe = get(Recipe, r);
  trace(9991, "transform") << "--- resolve ambiguous calls for recipe " << caller_recipe.name << end();
  for (int index = 0;  index < SIZE(caller_recipe.steps);  ++index) {
    instruction& inst = caller_recipe.steps.at(index);
    if (inst.is_label) continue;
    resolve_ambiguous_call(r, index, inst, caller_recipe);
  }
}

void resolve_ambiguous_call(const recipe_ordinal r, int index, instruction& inst, const recipe& caller_recipe) {
  // End resolve_ambiguous_call(r, index, inst, caller_recipe) Special-cases
  if (non_ghost_size(get_or_insert(Recipe_variants, inst.name)) == 0) return;
  trace(9992, "transform") << "instruction " << to_original_string(inst) << end();
  Resolve_stack.push_front(call(r, index));
  string new_name = best_variant(inst, caller_recipe);
  if (!new_name.empty())
    inst.name = new_name;
  assert(Resolve_stack.front().running_recipe == r);
  assert(Resolve_stack.front().running_step_index == index);
  Resolve_stack.pop_front();
}

string best_variant(const instruction& inst, const recipe& caller_recipe) {
  const vector<recipe_ordinal>& variants = get(Recipe_variants, inst.name);
  vector<recipe_ordinal> candidates;

  // Static Dispatch Phase 1
//?   cerr << inst.name << " phase 1\n";
  candidates = strictly_matching_variants(inst, variants);
  if (!candidates.empty()) return best_variant(inst, candidates).name;

//?   cerr << inst.name << " phase 3\n";
  // Static Dispatch Phase 2
  //: (shape-shifting recipes in a later layer)
  // End Static Dispatch Phase 2

  // Static Dispatch Phase 3
//?   cerr << inst.name << " phase 4\n";
  candidates = matching_variants(inst, variants);
  if (!candidates.empty()) return best_variant(inst, candidates).name;

  // error messages
  if (!is_primitive(get(Recipe_ordinal, inst.name))) {  // we currently don't check types for primitive variants
    if (SIZE(variants) == 1) {
      raise << maybe(caller_recipe.name) << "types don't match in call for '" << to_original_string(inst) << "'\n" << end();
      raise << "  which tries to call '" << original_header_label(get(Recipe, variants.at(0))) << "'\n" << end();
    }
    else {
      raise << maybe(caller_recipe.name) << "failed to find a matching call for '" << to_original_string(inst) << "'\n" << end();
      raise << "  available variants are:\n" << end();
      for (int i = 0;  i < SIZE(variants);  ++i)
        raise << "    " << original_header_label(get(Recipe, variants.at(i))) << '\n' << end();
    }
    for (list<call>::iterator p = /*skip*/++Resolve_stack.begin();  p != Resolve_stack.end();  ++p) {
      const recipe& specializer_recipe = get(Recipe, p->running_recipe);
      const instruction& specializer_inst = specializer_recipe.steps.at(p->running_step_index);
      if (specializer_recipe.name != "interactive")
        raise << "  (from '" << to_original_string(specializer_inst) << "' in " << specializer_recipe.name << ")\n" << end();
      else
        raise << "  (from '" << to_original_string(specializer_inst) << "')\n" << end();
      // One special-case to help with the rewrite_stash transform. (cross-layer)
      if (specializer_inst.products.at(0).name.find("stash_") == 0) {
        instruction stash_inst;
        if (next_stash(*p, &stash_inst)) {
          if (specializer_recipe.name != "interactive")
            raise << "  (part of '" << to_original_string(stash_inst) << "' in " << specializer_recipe.name << ")\n" << end();
          else
            raise << "  (part of '" << to_original_string(stash_inst) << "')\n" << end();
        }
      }
    }
  }
  return "";
}

// phase 1
vector<recipe_ordinal> strictly_matching_variants(const instruction& inst, const vector<recipe_ordinal>& variants) {
  vector<recipe_ordinal> result;
  for (int i = 0;  i < SIZE(variants);  ++i) {
    if (variants.at(i) == -1) continue;
    trace(9992, "transform") << "checking variant (strict) " << i << ": " << header_label(variants.at(i)) << end();
    if (all_header_reagents_strictly_match(inst, get(Recipe, variants.at(i))))
      result.push_back(variants.at(i));
  }
  return result;
}

bool all_header_reagents_strictly_match(const instruction& inst, const recipe& variant) {
  for (int i = 0;  i < min(SIZE(inst.ingredients), SIZE(variant.ingredients));  ++i) {
    if (!types_strictly_match(variant.ingredients.at(i), inst.ingredients.at(i))) {
      trace(9993, "transform") << "strict match failed: ingredient " << i << end();
      return false;
    }
  }
  for (int i = 0;  i < min(SIZE(inst.products), SIZE(variant.products));  ++i) {
    if (is_dummy(inst.products.at(i))) continue;
    if (!types_strictly_match(variant.products.at(i), inst.products.at(i))) {
      trace(9993, "transform") << "strict match failed: product " << i << end();
      return false;
    }
  }
  return true;
}

// phase 3
vector<recipe_ordinal> matching_variants(const instruction& inst, const vector<recipe_ordinal>& variants) {
  vector<recipe_ordinal> result;
  for (int i = 0;  i < SIZE(variants);  ++i) {
    if (variants.at(i) == -1) continue;
    trace(9992, "transform") << "checking variant " << i << ": " << header_label(variants.at(i)) << end();
    if (all_header_reagents_match(inst, get(Recipe, variants.at(i))))
      result.push_back(variants.at(i));
  }
  return result;
}

bool all_header_reagents_match(const instruction& inst, const recipe& variant) {
  for (int i = 0;  i < min(SIZE(inst.ingredients), SIZE(variant.ingredients));  ++i) {
    if (!types_match(variant.ingredients.at(i), inst.ingredients.at(i))) {
      trace(9993, "transform") << "match failed: ingredient " << i << end();
      return false;
    }
  }
  for (int i = 0;  i < min(SIZE(variant.products), SIZE(inst.products));  ++i) {
    if (is_dummy(inst.products.at(i))) continue;
    if (!types_match(variant.products.at(i), inst.products.at(i))) {
      trace(9993, "transform") << "match failed: product " << i << end();
      return false;
    }
  }
  return true;
}

// tie-breaker for each phase
const recipe& best_variant(const instruction& inst, vector<recipe_ordinal>& candidates) {
  assert(!candidates.empty());
  if (SIZE(candidates) == 1) return get(Recipe, candidates.at(0));
  int min_score = 999;
  int min_index = 0;
  for (int i = 0;  i < SIZE(candidates);  ++i) {
    const recipe& candidate = get(Recipe, candidates.at(i));
    // prefer variants without extra or missing ingredients or products
    int score = abs(SIZE(candidate.products)-SIZE(inst.products))
                          + abs(SIZE(candidate.ingredients)-SIZE(inst.ingredients));
    // prefer variants with non-address ingredients or products
    for (int j = 0;  j < SIZE(candidate.ingredients);  ++j) {
      if (is_mu_address(candidate.ingredients.at(j)))
        ++score;
    }
    for (int j = 0;  j < SIZE(candidate.products);  ++j) {
      if (is_mu_address(candidate.products.at(j)))
        ++score;
    }
    assert(score < 999);
    if (score < min_score) {
      min_score = score;
      min_index = i;
    }
  }
  return get(Recipe, candidates.at(min_index));
}

int non_ghost_size(vector<recipe_ordinal>& variants) {
  int result = 0;
  for (int i = 0;  i < SIZE(variants);  ++i)
    if (variants.at(i) != -1) ++result;
  return result;
}

bool next_stash(const call& c, instruction* stash_inst) {
  const recipe& specializer_recipe = get(Recipe, c.running_recipe);
  int index = c.running_step_index;
  for (++index;  index < SIZE(specializer_recipe.steps);  ++index) {
    const instruction& inst = specializer_recipe.steps.at(index);
    if (inst.name == "stash") {
      *stash_inst = inst;
      return true;
    }
  }
  return false;
}

:(scenario static_dispatch_disabled_in_recipe_without_variants)
def main [
  1:num <- test 3
]
def test [
  2:num <- next-ingredient  # ensure no header
  return 34
]
+mem: storing 34 in location 1

:(scenario static_dispatch_disabled_on_headerless_definition)
% Hide_errors = true;
def test a:num -> z:num [
  z <- copy 1
]
def test [
  return 34
]
+error: redefining recipe test

:(scenario static_dispatch_disabled_on_headerless_definition_2)
% Hide_errors = true;
def test [
  return 34
]
def test a:num -> z:num [
  z <- copy 1
]
+error: redefining recipe test

:(scenario static_dispatch_on_primitive_names)
def main [
  1:num <- copy 34
  2:num <- copy 34
  3:bool <- equal 1:num, 2:num
  4:bool <- copy false
  5:bool <- copy false
  6:bool <- equal 4:bool, 5:bool
]
# temporarily hardcode number equality to always fail
def equal x:num, y:num -> z:bool [
  local-scope
  load-ingredients
  z <- copy false
]
# comparing numbers used overload
+mem: storing 0 in location 3
# comparing booleans continues to use primitive
+mem: storing 1 in location 6

:(scenario static_dispatch_works_with_dummy_results_for_containers)
def main [
  _ <- test 3, 4
]
def test a:num -> z:point [
  local-scope
  load-ingredients
  z <- merge a, 0
]
def test a:num, b:num -> z:point [
  local-scope
  load-ingredients
  z <- merge a, b
]
$error: 0

:(scenario static_dispatch_works_with_compound_type_containing_container_defined_after_first_use)
def main [
  x:&:foo <- new foo:type
  test x
]
container foo [
  x:num
]
def test a:&:foo -> z:num [
  local-scope
  load-ingredients
  z:num <- get *a, x:offset
]
$error: 0

:(scenario static_dispatch_works_with_compound_type_containing_container_defined_after_second_use)
def main [
  x:&:foo <- new foo:type
  test x
]
def test a:&:foo -> z:num [
  local-scope
  load-ingredients
  z:num <- get *a, x:offset
]
container foo [
  x:num
]
$error: 0

:(scenario static_dispatch_on_non_literal_character_ignores_variant_with_numbers)
% Hide_errors = true;
def main [
  local-scope
  x:char <- copy 10/newline
  1:num/raw <- foo x
]
def foo x:num -> y:num [
  load-ingredients
  return 34
]
+error: main: ingredient 0 has the wrong type at '1:num/raw <- foo x'
-mem: storing 34 in location 1

:(scenario static_dispatch_dispatches_literal_to_character)
def main [
  1:num/raw <- foo 97
]
def foo x:char -> y:num [
  local-scope
  load-ingredients
  return 34
]
# character variant is preferred
+mem: storing 34 in location 1

:(scenario static_dispatch_dispatches_literal_to_number_if_at_all_possible)
def main [
  1:num/raw <- foo 97
]
def foo x:char -> y:num [
  local-scope
  load-ingredients
  return 34
]
def foo x:num -> y:num [
  local-scope
  load-ingredients
  return 35
]
# number variant is preferred
+mem: storing 35 in location 1

:(replace{} "string header_label(const recipe_ordinal r)")
string header_label(const recipe_ordinal r) {
  return header_label(get(Recipe, r));
}
:(code)
string header_label(const recipe& caller) {
  ostringstream out;
  out << "recipe " << caller.name;
  for (int i = 0;  i < SIZE(caller.ingredients);  ++i)
    out << ' ' << to_string(caller.ingredients.at(i));
  if (!caller.products.empty()) out << " ->";
  for (int i = 0;  i < SIZE(caller.products);  ++i)
    out << ' ' << to_string(caller.products.at(i));
  return out.str();
}

string original_header_label(const recipe& caller) {
  ostringstream out;
  out << "recipe " << caller.original_name;
  for (int i = 0;  i < SIZE(caller.ingredients);  ++i)
    out << ' ' << caller.ingredients.at(i).original_string;
  if (!caller.products.empty()) out << " ->";
  for (int i = 0;  i < SIZE(caller.products);  ++i)
    out << ' ' << caller.products.at(i).original_string;
  return out.str();
}

:(scenario reload_variant_retains_other_variants)
def main [
  1:num <- copy 34
  2:num <- foo 1:num
]
def foo x:num -> y:num [
  local-scope
  load-ingredients
  return 34
]
def foo x:&:num -> y:num [
  local-scope
  load-ingredients
  return 35
]
def! foo x:&:num -> y:num [
  local-scope
  load-ingredients
  return 36
]
+mem: storing 34 in location 2
$error: 0

:(scenario dispatch_errors_come_after_unknown_name_errors)
% Hide_errors = true;
def main [
  y:num <- foo x
]
def foo a:num -> b:num [
  local-scope
  load-ingredients
  return 34
]
def foo a:bool -> b:num [
  local-scope
  load-ingredients
  return 35
]
+error: main: missing type for 'x' in 'y:num <- foo x'
+error: main: failed to find a matching call for 'y:num <- foo x'

:(scenario override_methods_with_type_abbreviations)
def main [
  local-scope
  s:text <- new [abc]
  1:num/raw <- foo s
]
def foo a:address:array:character -> result:number [
  return 34
]
# identical to previous variant once you take type abbreviations into account
def! foo a:text -> result:num [
  return 35
]
+mem: storing 35 in location 1

:(scenario ignore_static_dispatch_in_type_errors_without_overloading)
% Hide_errors = true;
def main [
  local-scope
  x:&:num <- copy 0
  foo x
]
def foo x:&:char [
  local-scope
  load-ingredients
]
+error: main: types don't match in call for 'foo x'
+error:   which tries to call 'recipe foo x:&:char'

:(scenario show_available_variants_in_dispatch_errors)
% Hide_errors = true;
def main [
  local-scope
  x:&:num <- copy 0
  foo x
]
def foo x:&:char [
  local-scope
  load-ingredients
]
def foo x:&:bool [
  local-scope
  load-ingredients
]
+error: main: failed to find a matching call for 'foo x'
+error:   available variants are:
+error:     recipe foo x:&:char
+error:     recipe foo x:&:bool

:(before "End Includes")
using std::abs;