test_namespace.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "volksdata/namespace.h"
  2. #include "test.h"
  3. static int
  4. test_namespace()
  5. {
  6. // Use nonconventional prefix names to avoid clashes with default
  7. // namespace prefixes already registered.
  8. ASSERT (VOLK_default_nsm != NULL, "Error creating namespace map on init!");
  9. EXPECT_PASS (VOLK_nsmap_add (
  10. "rdfxx", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"));
  11. EXPECT_PASS (VOLK_nsmap_add (
  12. "rdfsxxx", "http://www.w3.org/2000/01/rdf-schema#"));
  13. EXPECT_PASS (VOLK_nsmap_add ("ns1", "urn:ns1:"));
  14. EXPECT_PASS (VOLK_nsmap_add ("ns2", "urn:ns2:"));
  15. // Look up prefix.
  16. EXPECT_STR_EQ (
  17. VOLK_nsmap_get_ns ("rdfxx"),
  18. "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
  19. EXPECT_STR_EQ (
  20. VOLK_nsmap_get_ns ("rdfsxxx"),
  21. "http://www.w3.org/2000/01/rdf-schema#");
  22. // Prefixes longer than 7 chars are truncated.
  23. EXPECT_STR_EQ (
  24. VOLK_nsmap_get_ns ("rdfsxxxyyy"),
  25. "http://www.w3.org/2000/01/rdf-schema#");
  26. ASSERT (VOLK_nsmap_get_ns ("none") == NULL, "Non-existent NS found!");
  27. // Lookup NS.
  28. EXPECT_STR_EQ (VOLK_nsmap_get_pfx ("urn:ns1:"), "ns1");
  29. EXPECT_STR_EQ (VOLK_nsmap_get_pfx ("urn:ns2:"), "ns2");
  30. // Cannot test rdfxx and rdfsxxx reverse lookup because dc and dcterms are
  31. // also mapped to the same ns, and either one can come up.
  32. // Normalize and denormalize URIs.
  33. char *fq_uri, *pfx_uri;
  34. fq_uri = "urn:ns1:res1";
  35. EXPECT_PASS (VOLK_nsmap_denormalize_uri (fq_uri, &pfx_uri));
  36. EXPECT_STR_EQ (pfx_uri, "ns1:res1");
  37. fq_uri = NULL;
  38. EXPECT_PASS (VOLK_nsmap_normalize_uri (pfx_uri, &fq_uri));
  39. EXPECT_STR_EQ (fq_uri, "urn:ns1:res1");
  40. free (fq_uri);
  41. free (pfx_uri);
  42. ASSERT (
  43. VOLK_nsmap_normalize_uri ("dctermsxxtitle", &fq_uri)
  44. == VOLK_NORESULT, "Wrong RC for normalizing long ns!");
  45. ASSERT (!fq_uri, "Faulty normalization output should be NULL!");
  46. free (fq_uri);
  47. ASSERT (
  48. VOLK_nsmap_normalize_uri ("bogus:ns:123", &fq_uri)
  49. == VOLK_NORESULT, "Wrong RC for normalizing non-existent ns!");
  50. ASSERT (!fq_uri, "Faulty normalization output should be NULL!");
  51. free (fq_uri);
  52. ASSERT (
  53. VOLK_nsmap_normalize_uri ("bogus", &fq_uri)
  54. == VOLK_NORESULT, "Wrong RC for normalizing non-prefixed URI!");
  55. ASSERT (!fq_uri, "Faulty normalization output should be NULL!");
  56. free (fq_uri);
  57. EXPECT_PASS (VOLK_nsmap_remove ("rdfxx"));
  58. ASSERT (VOLK_nsmap_get_ns ("rdfxx") == NULL, "Deleted NS found!");
  59. ASSERT (
  60. VOLK_nsmap_remove ("none") == VOLK_NOACTION,
  61. "Wrong result for removal of non-existent prefix!");
  62. // Test normalization after removal.
  63. ASSERT (
  64. VOLK_nsmap_normalize_uri ("rdfxx:title", &fq_uri)
  65. == VOLK_NORESULT, "Wrong RC for normalizing non-prefixed URI!");
  66. ASSERT (!fq_uri, "Faulty normalization output should be NULL!");
  67. free (fq_uri);
  68. return 0;
  69. }
  70. int namespace_tests()
  71. {
  72. RUN (test_namespace);
  73. return 0;
  74. }