summaryrefslogtreecommitdiff
path: root/sorsyl/test/test_sonorityold.ml
blob: 70845a68cc774ce5a25f11ad6a449eb61412a8e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
(** Tests for the Sonority module *)

open Sorsyl

(** Test fixture - initialize the module once *)
let () =
  (* Initialize with the data directory *)
  (* When run with dune test, the working directory is _build/default/test *)
  let data_dir =
    if Sys.file_exists "./data" then "./data"
    else if Sys.file_exists "../data" then "../data"
    else if Sys.file_exists "../../../data" then "../../../data"
    else (
      Printf.eprintf "Current directory: %s\n" (Sys.getcwd ());
      failwith "Cannot find data directory")
  in
  Sonority.init data_dir

(** Test sonority value 9 - Low vowels *)
let test_sonority_nine () =
  let segments = [ "a"; "ɑ"; "æ"; "ɒ"; "e"; "o̥" ] in
  let expected = [ 9; 9; 9; 9; 9; 9 ] in
  let results = List.map Sonority.sonority segments in
  assert (results = expected);
  Printf.printf "test_sonority_nine: PASSED\n"

(** Test sonority value 8 - High vowels *)
let test_sonority_eight () =
  let segments = [ "i"; "y"; "ɨ"; "ʉ"; "ɯ"; "u" ] in
  let expected = [ 8; 8; 8; 8; 8; 8 ] in
  let results = List.map Sonority.sonority segments in
  assert (results = expected);
  Printf.printf "test_sonority_eight: PASSED\n"

(** Test sonority value 7 - Glides/approximants *)
let test_sonority_seven () =
  let segments = [ "j"; "w"; "ʋ"; "ɰ"; "ɹ"; "e̯" ] in
  let expected = [ 7; 7; 7; 7; 7; 7 ] in
  let results = List.map Sonority.sonority segments in
  assert (results = expected);
  Printf.printf "test_sonority_seven: PASSED\n"

(** Test sonority value 6 - Liquids *)
let test_sonority_six () =
  let segments = [ "l"; "ɭ"; "r"; "ɾ" ] in
  let expected = [ 6; 6; 6; 6 ] in
  let results = List.map Sonority.sonority segments in
  assert (results = expected);
  Printf.printf "test_sonority_six: PASSED\n"

(** Test sonority value 5 - Nasals *)
let test_sonority_five () =
  let segments = [ "n"; "m"; "ŋ"; "ɴ" ] in
  let expected = [ 5; 5; 5; 5 ] in
  let results = List.map Sonority.sonority segments in
  assert (results = expected);
  Printf.printf "test_sonority_five: PASSED\n"

(** Test sonority value 4 - Voiced fricatives *)
let test_sonority_four () =
  let segments = [ "v"; "z"; "ʒ"; "ɣ" ] in
  let expected = [ 4; 4; 4; 4 ] in
  let results = List.map Sonority.sonority segments in
  assert (results = expected);
  Printf.printf "test_sonority_four: PASSED\n"

(** Test sonority value 3 - Voiceless fricatives *)
let test_sonority_three () =
  let segments = [ "f"; "s"; "x"; "ħ"; "ʃ" ] in
  let expected = [ 3; 3; 3; 3; 3 ] in
  let results = List.map Sonority.sonority segments in
  assert (results = expected);
  Printf.printf "test_sonority_three: PASSED\n"

(** Test sonority value 2 - Voiced stops *)
let test_sonority_two () =
  let segments = [ "b"; "ɡ"; "d"; "ɢ" ] in
  let expected = [ 2; 2; 2; 2 ] in
  let results = List.map Sonority.sonority segments in
  assert (results = expected);
  Printf.printf "test_sonority_two: PASSED\n"

(** Test sonority value 1 - Voiceless stops *)
let test_sonority_one () =
  let segments = [ "p"; "k"; "c"; "q" ] in
  let expected = [ 1; 1; 1; 1 ] in
  let results = List.map Sonority.sonority segments in
  assert (results = expected);
  Printf.printf "test_sonority_one: PASSED\n"

(** Test unknown segment handling *)
let test_unknown_segment () =
  try
    let _ = Sonority.sonority "🦆" in
    assert false (* Should not reach here *)
  with
  | Failure msg when String.sub msg 0 20 = "Unknown IPA segment:" ->
      Printf.printf "test_unknown_segment: PASSED\n"
  | _ -> assert false

(** Test feature-based sonority calculation *)
let test_sonority_from_features () =
  (* Test a simple voiceless stop: -syl, +cons, -son, -cont, -voi *)
  let segment =
    [
      (Feature.Syllabic, Feature.Minus);
      (Feature.Consonantal, Feature.Plus);
      (Feature.Sonorant, Feature.Minus);
      (Feature.Continuant, Feature.Minus);
      (Feature.Voiced, Feature.Minus);
    ]
  in
  let result = Sonority.sonority_from_features segment in
  assert (result = 1);
  Printf.printf "test_sonority_from_features: PASSED\n"

(** Run all tests *)
let () =
  Printf.printf "Running Sonority module tests...\n";
  Printf.printf "================================\n";
  test_sonority_nine ();
  test_sonority_eight ();
  test_sonority_seven ();
  test_sonority_six ();
  test_sonority_five ();
  test_sonority_four ();
  test_sonority_three ();
  test_sonority_two ();
  test_sonority_one ();
  test_unknown_segment ();
  test_sonority_from_features ();
  Printf.printf "================================\n";
  Printf.printf "All tests passed!\n"