Dictionary.vala 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using Invercargill;
  2. using Invercargill.Convert;
  3. void dictionary_tests() {
  4. Test.add_func("/invercargill/dictionary/string", () => {
  5. var names = new string[] {"Billy Barrow", "William Robert", "Johnny Gala", "John Doe"};
  6. var dict = ate(names).to_dictionary<string>(n => n.split(" ")[0]);
  7. dict["James"] = "James Smith";
  8. var billy = dict.get_or_default("Billy");
  9. var john = dict.get_or_default("John");
  10. var james = dict.get_or_default("James");
  11. var bob = dict.get_or_default("Bob");
  12. assert_cmpstr("Billy Barrow", CompareOperator.EQ, billy);
  13. assert_cmpstr("John Doe", CompareOperator.EQ, john);
  14. assert_cmpstr("James Smith", CompareOperator.EQ, james);
  15. assert_null(bob);
  16. });
  17. Test.add_func("/invercargill/dictionary/int", () => {
  18. var dict = new Dictionary<int, string>();
  19. dict[1999] = "Billy";
  20. dict[1962] = "John";
  21. dict[2002] = "James";
  22. var billy = dict.get_or_default(1999);
  23. var john = dict.get_or_default(1962);
  24. var james = dict.get_or_default(2002);
  25. var bob = dict.get_or_default(1840);
  26. assert_cmpstr("Billy", CompareOperator.EQ, billy);
  27. assert_cmpstr("John", CompareOperator.EQ, john);
  28. assert_cmpstr("James", CompareOperator.EQ, james);
  29. assert_null(bob);
  30. });
  31. }