summary refs log tree commit diff stats
path: root/tests/stdlib/tsystem.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tests/stdlib/tsystem.nim')
-rw-r--r--tests/stdlib/tsystem.nim200
1 files changed, 200 insertions, 0 deletions
diff --git a/tests/stdlib/tsystem.nim b/tests/stdlib/tsystem.nim
new file mode 100644
index 000000000..f634ce0c2
--- /dev/null
+++ b/tests/stdlib/tsystem.nim
@@ -0,0 +1,200 @@
+discard """
+  matrix: "--mm:refc; --mm:orc"
+  targets: "c cpp js"
+"""
+
+import stdtest/testutils
+import std/[assertions, formatfloat]
+
+# TODO: in future work move existing `system` tests here, where they belong
+
+
+template main =
+  block: # closure
+    proc outer() =
+      var a = 0
+      proc inner1 = a.inc
+      proc inner2 = discard
+      doAssert inner1 is "closure"
+      doAssert inner2 isnot "closure"
+      doAssert inner1 is (proc)
+      doAssert inner2 is (proc)
+      let inner1b = inner1
+      doAssert inner1b is "closure"
+      doAssert inner1b == inner1
+    outer()
+
+  block: # rawProc, rawProc, bug #17911
+    proc outer() =
+      var a = 0
+      var b = 0
+      proc inner1() = a.inc
+      proc inner2() = a += 2
+      proc inner3() = b.inc
+      let inner1b = inner1
+      doAssert inner2 != inner1
+      doAssert inner3 != inner1
+      whenVMorJs: discard
+      do:
+        doAssert rawProc(inner1b) == rawProc(inner1)
+        doAssert rawProc(inner2) != rawProc(inner1)
+        doAssert rawProc(inner3) != rawProc(inner1)
+
+        doAssert rawEnv(inner1b) == rawEnv(inner1)
+        doAssert rawEnv(inner2) == rawEnv(inner1) # because both use `a`
+        # doAssert rawEnv(inner3) != rawEnv(inner1) # because `a` vs `b` # this doesn't hold
+    outer()
+
+  block: # system.delete
+    block:
+      var s = @[1]
+      s.delete(0)
+      doAssert s == @[]
+
+    block:
+      var s = @["foo", "bar"]
+      s.delete(1)
+      doAssert s == @["foo"]
+
+    when false:
+      var s: seq[string]
+      doAssertRaises(IndexDefect):
+        s.delete(0)
+
+    block:
+      doAssert not compiles(@["foo"].delete(-1))
+
+    block: # bug #6710
+      var s = @["foo"]
+      s.delete(0)
+      doAssert s == @[]
+
+    when false: # bug #16544: deleting out of bounds index should raise
+      var s = @["foo"]
+      doAssertRaises(IndexDefect):
+        s.delete(1)
+
+static: main()
+main()
+
+# bug #19967
+block:
+  type
+    X = object
+      a: string
+      b: set[char]
+      c: int
+      d: float
+      e: int64
+
+
+  var x = X(b: {'a'}, e: 10)
+
+  var y = move x
+
+  doAssert x.a == ""
+  doAssert x.b == {}
+  doAssert x.c == 0
+  doAssert x.d == 0.0
+  doAssert x.e == 0
+
+  reset(y)
+
+  doAssert y.a == ""
+  doAssert y.b == {}
+  doAssert y.c == 0
+  doAssert y.d == 0.0
+  doAssert y.e == 0
+
+block:
+  var x = 2
+  var y = move x
+  doAssert y == 2
+  doAssert x == 0
+  reset y
+  doAssert y == 0
+
+block:
+  type
+    X = object
+      a: string
+      b: float
+
+  var y = X(b: 1314.521)
+
+  reset(y)
+
+  doAssert y.b == 0.0
+
+block:
+  type
+    X = object
+      a: string
+      b: string
+
+  var y = X(b: "1314")
+
+  reset(y)
+
+  doAssert y.b == ""
+
+block:
+  type
+    X = object
+      a: string
+      b: seq[int]
+
+  var y = X(b: @[1, 3])
+
+  reset(y)
+
+  doAssert y.b == @[]
+
+block:
+  type
+    X = object
+      a: string
+      b: tuple[a: int, b: string]
+
+  var y = X(b: (1, "cc"))
+
+  reset(y)
+
+  doAssert y.b == (0, "")
+
+block:
+  type
+    Color = enum
+      Red, Blue, Yellow
+    X = object
+      a: string
+      b: set[Color]
+
+  var y = X(b: {Red, Blue})
+
+  reset(y)
+  doAssert y.b == {}
+
+block: # bug #20516
+  type Foo = object
+    x {.bitsize:4.}: uint
+    y {.bitsize:4.}: uint
+
+  when not defined(js):
+    let a = create(Foo)
+
+block: # bug #6549
+  when not defined(js):
+    block:
+      const v = 18446744073709551615'u64
+
+      doAssert $v == "18446744073709551615"
+      doAssert $float32(v) == "1.8446744e+19", $float32(v)
+      doAssert $float64(v) == "1.8446744073709552e+19", $float64(v)
+
+    block:
+      let v = 18446744073709551615'u64
+
+      doAssert $v == "18446744073709551615"
+      doAssert $float32(v) == "1.8446744e+19"
+      doAssert $float64(v) == "1.8446744073709552e+19"
#aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
//: It's often convenient to express recipes in a textual fashion.
:(scenarios add_recipes)
:(scenario first_recipe)
recipe main [
  1:integer <- copy 23:literal
]
+parse: instruction: 1
+parse:   ingredient: {name: "23", value: 0, type: 0, properties: ["23": "literal"]}
+parse:   product: {name: "1", value: 0, type: 1, properties: ["1": "integer"]}

:(code)
vector<recipe_number> add_recipes(string form) {
  istringstream in(form);
  in >> std::noskipws;
  return add_recipes(in);
}

vector<recipe_number> add_recipes(istream& in) {
  vector<recipe_number> result;
  while (!in.eof()) {
    skip_comments_and_newlines(in);
    if (in.eof()) break;
    string command = next_word(in);
    // Command Handlers
    if (command == "recipe") {
      result.push_back(add_recipe(in));
    }
    // End Command Handlers
    else {
      raise << "unknown top-level command: " << command;
    }
  }
  return result;
}

recipe_number add_recipe(istream& in) {
  skip_comments_and_newlines(in);
  string recipe_name = next_word(in);
//?   cout << "recipe name: ^" << recipe_name << "$\n"; //? 3
  if (recipe_name.empty())
    raise << "empty recipe name\n";
//?     raise << "empty recipe name in " << in.str() << '\n';
  if (Recipe_number.find(recipe_name) == Recipe_number.end()) {
    Recipe_number[recipe_name] = Next_recipe_number++;
//?     cout << "AAA: " << recipe_name << " is now " << Recipe_number[recipe_name] << '\n'; //? 1
  }
  recipe_number r = Recipe_number[recipe_name];
  if (Recipe.find(r) != Recipe.end()) {
    raise << "redefining recipe " << Recipe[r].name << "\n";
    Recipe.erase(r);
  }
//?   cout << recipe_name << ": adding recipe " << r << '\n'; //? 3

  skip_whitespace(in);
  if (in.get() != '[')
    raise << "recipe body must begin with '['\n";

  skip_comments_and_newlines(in);

  instruction curr;
  while (next_instruction(in, &curr)) {
//?     if (!curr.products.empty()) cout << curr.products[0].to_string() << '\n'; //? 1
    Recipe[r].steps.push_back(curr);
  }
  Recipe[r].name = recipe_name;
//?   cout << "recipe " << recipe_name << " has " << Recipe[r].steps.size() << " steps.\n"; //? 1
  // track added recipes because we may need to undo them in tests; see below
  recently_added_recipes.push_back(r);
  return r;
}

bool next_instruction(istream& in, instruction* curr) {
  curr->clear();
  if (in.eof()) return false;
  skip_whitespace(in);  if (in.eof()) return false;
  skip_comments_and_newlines(in);  if (in.eof()) return false;

  vector<string> words;
  while (in.peek() != '\n') {
    skip_whitespace(in);  if (in.eof()) return false;
    string word = next_word(in);  if (in.eof()) return false;
    words.push_back(word);
    skip_whitespace(in);  if (in.eof()) return false;
  }
  skip_comments_and_newlines(in);  if (in.eof()) return false;

//?   if (words.size() == 1) cout << words[0] << ' ' << words[0].size() << '\n'; //? 1
  if (words.size() == 1 && words[0] == "]") {
//?     cout << "AAA\n"; //? 1
    return false;  // end of recipe
  }

  if (words.size() == 1 && !isalnum(words[0][0])) {
    curr->is_label = true;
    curr->label = words[0];
    trace("parse") << "label: " << curr->label;
    return !in.eof();
  }

  vector<string>::iterator p = words.begin();
  if (find(words.begin(), words.end(), "<-") != words.end()) {
    for (; *p != "<-"; ++p) {
      if (*p == ",") continue;
      curr->products.push_back(reagent(*p));
    }
    ++p;  // skip <-
  }

  curr->name = *p;
  if (Recipe_number.find(*p) == Recipe_number.end()) {
    Recipe_number[*p] = Next_recipe_number++;
//?     cout << "AAA: " << *p << " is now " << Recipe_number[*p] << '\n'; //? 1
  }
  if (Recipe_number[*p] == 0) {
    raise << "Recipe " << *p << " has number 0, which is reserved for IDLE.\n" << die();
  }
  curr->operation = Recipe_number[*p];  ++p;

  for (; p != words.end(); ++p) {
    if (*p == ",") continue;
    curr->ingredients.push_back(reagent(*p));
  }

  trace("parse") << "instruction: " << curr->operation;
  for (vector<reagent>::iterator p = curr->ingredients.begin(); p != curr->ingredients.end(); ++p) {
    trace("parse") << "  ingredient: " << p->to_string();
  }
  for (vector<reagent>::iterator p = curr->products.begin(); p != curr->products.end(); ++p) {
    trace("parse") << "  product: " << p->to_string();
  }
  return !in.eof();
}

string next_word(istream& in) {
//?   cout << "AAA next_word\n"; //? 1
  ostringstream out;
  skip_whitespace(in);
  slurp_word(in, out);
  skip_whitespace(in);
  skip_comment(in);
  return out.str();
}

void slurp_word(istream& in, ostream& out) {
//?   cout << "AAA slurp_word\n"; //? 1
  char c;
  if (in.peek() == ',') {
    in >> c;
    out << c;
    return;
  }
  while (in >> c) {
//?     cout << c << '\n'; //? 1
    if (isspace(c) || c == ',') {
      in.putback(c);
      break;
    }
    out << c;
  }
}

void skip_whitespace(istream& in) {
  while (isspace(in.peek()) && in.peek() != '\n') {
    in.get();
  }
}

void skip_comments_and_newlines(istream& in) {
  while (in.peek() == '\n' || in.peek() == '#') {
    if (in.peek() == '#') {
      in.get();
      while (in.peek() != '\n') in.get();
    }
    in.get();
  }
}

void skip_comment(istream& in) {
  if (in.peek() == '#') {
    in.get();
    while (in.peek() != '\n') in.get();
  }
}

void skip_comma(istream& in) {
  skip_whitespace(in);
  if (in.peek() == ',') in.get();
  skip_whitespace(in);
}

//: Have tests clean up any recipes they added.
:(before "End Globals")
vector<recipe_number> recently_added_recipes;
:(before "End Setup")
for (size_t i = 0; i < recently_added_recipes.size(); ++i) {
//?   cout << "AAA clearing " << Recipe[recently_added_recipes[i]].name << '\n'; //? 2
  Recipe_number.erase(Recipe[recently_added_recipes[i]].name);
  Recipe.erase(recently_added_recipes[i]);
}
// Clear Other State For recently_added_recipes
recently_added_recipes.clear();

:(scenario parse_comment_outside_recipe)
# comment
recipe main [
  1:integer <- copy 23:literal
]
+parse: instruction: 1
+parse:   ingredient: {name: "23", value: 0, type: 0, properties: ["23": "literal"]}
+parse:   product: {name: "1", value: 0, type: 1, properties: ["1": "integer"]}

:(scenario parse_comment_amongst_instruction)
recipe main [
  # comment
  1:integer <- copy 23:literal
]
+parse: instruction: 1
+parse:   ingredient: {name: "23", value: 0, type: 0, properties: ["23": "literal"]}
+parse:   product: {name: "1", value: 0, type: 1, properties: ["1": "integer"]}

:(scenario parse_comment_amongst_instruction2)
recipe main [
  # comment
  1:integer <- copy 23:literal
  # comment
]
+parse: instruction: 1
+parse:   ingredient: {name: "23", value: 0, type: 0, properties: ["23": "literal"]}
+parse:   product: {name: "1", value: 0, type: 1, properties: ["1": "integer"]}

:(scenario parse_comment_amongst_instruction3)
recipe main [
  1:integer <- copy 23:literal
  # comment
  2:integer <- copy 23:literal
]
+parse: instruction: 1
+parse:   ingredient: {name: "23", value: 0, type: 0, properties: ["23": "literal"]}
+parse:   product: {name: "1", value: 0, type: 1, properties: ["1": "integer"]}
+parse: instruction: 1
+parse:   ingredient: {name: "23", value: 0, type: 0, properties: ["23": "literal"]}
+parse:   product: {name: "2", value: 0, type: 1, properties: ["2": "integer"]}

:(scenario parse_comment_after_instruction)
recipe main [
  1:integer <- copy 23:literal  # comment
]
+parse: instruction: 1
+parse:   ingredient: {name: "23", value: 0, type: 0, properties: ["23": "literal"]}
+parse:   product: {name: "1", value: 0, type: 1, properties: ["1": "integer"]}

:(scenario parse_label)
recipe main [
  +foo
]
+parse: label: +foo
-parse: instruction: 1

:(scenario parse_multiple_properties)
recipe main [
  1:integer <- copy 23:literal/foo:bar:baz
]
+parse: instruction: 1
+parse:   ingredient: {name: "23", value: 0, type: 0, properties: ["23": "literal", "foo": "bar":"baz"]}
+parse:   product: {name: "1", value: 0, type: 1, properties: ["1": "integer"]}

:(scenario parse_multiple_products)
recipe main [
  1:integer, 2:integer <- copy 23:literal
]
+parse: instruction: 1
+parse:   ingredient: {name: "23", value: 0, type: 0, properties: ["23": "literal"]}
+parse:   product: {name: "1", value: 0, type: 1, properties: ["1": "integer"]}
+parse:   product: {name: "2", value: 0, type: 1, properties: ["2": "integer"]}

:(scenario parse_multiple_ingredients)
recipe main [
  1:integer, 2:integer <- copy 23:literal, 4:integer
]
+parse: instruction: 1
+parse:   ingredient: {name: "23", value: 0, type: 0, properties: ["23": "literal"]}
+parse:   ingredient: {name: "4", value: 0, type: 1, properties: ["4": "integer"]}
+parse:   product: {name: "1", value: 0, type: 1, properties: ["1": "integer"]}
+parse:   product: {name: "2", value: 0, type: 1, properties: ["2": "integer"]}

:(scenario parse_multiple_types)
recipe main [
  1:integer, 2:address:integer <- copy 23:literal, 4:integer
]
+parse: instruction: 1
+parse:   ingredient: {name: "23", value: 0, type: 0, properties: ["23": "literal"]}
+parse:   ingredient: {name: "4", value: 0, type: 1, properties: ["4": "integer"]}
+parse:   product: {name: "1", value: 0, type: 1, properties: ["1": "integer"]}
+parse:   product: {name: "2", value: 0, type: 2-1, properties: ["2": "address":"integer"]}

:(scenario parse_properties)
recipe main [
  1:integer:address/deref <- copy 23:literal
]
+parse:   product: {name: "1", value: 0, type: 1-2, properties: ["1": "integer":"address", "deref": ]}