@@ -1021,113 +1021,115 @@ public void TestCases2() {
1021
1021
1022
1022
[ Fact ]
1023
1023
public void TestCustom ( ) {
1024
- var input = @"\lcm(a,b)" ;
1025
- var builder = new LaTeXParser ( input ) ;
1026
- var ( list , error ) = builder . Build ( ) ;
1027
- Assert . Null ( list ) ;
1028
- Assert . Equal ( @"Invalid command \lcm" , error ) ;
1029
-
1030
- LaTeXSettings . CommandSymbols . Add ( @"\lcm" , new LargeOperator ( "lcm" , false ) ) ;
1031
- list = ParseLaTeX ( input ) ;
1032
- Assert . Collection ( list ,
1033
- CheckAtom < LargeOperator > ( "lcm" ) ,
1034
- CheckAtom < Open > ( "(" ) ,
1035
- CheckAtom < Variable > ( "a" ) ,
1036
- CheckAtom < Punctuation > ( "," ) ,
1037
- CheckAtom < Variable > ( "b" ) ,
1038
- CheckAtom < Close > ( ")" )
1039
- ) ;
1040
- Assert . Equal ( @"\lcm (a,b)" , LaTeXParser . MathListToLaTeX ( list ) . ToString ( ) ) ;
1041
-
1042
- LaTeXSettings . CommandSymbols . Add ( @"lcm" , new LargeOperator ( "lcm" , false ) ) ;
1043
- LaTeXSettings . CommandSymbols . Add ( @"lcm12" , new LargeOperator ( "lcm12" , false ) ) ;
1044
- LaTeXSettings . CommandSymbols . Add ( @"lcm1234" , new LargeOperator ( "lcm1234" , false ) ) ;
1045
- LaTeXSettings . CommandSymbols . Add ( @"lcm1235" , new LargeOperator ( "lcm1235" , false ) ) ;
1046
-
1047
- // Does not match custom atoms added above
1048
- list = ParseLaTeX ( "lc(a,b)" ) ;
1049
- Assert . Collection ( list ,
1050
- CheckAtom < Variable > ( "l" ) ,
1051
- CheckAtom < Variable > ( "c" ) ,
1052
- CheckAtom < Open > ( "(" ) ,
1053
- CheckAtom < Variable > ( "a" ) ,
1054
- CheckAtom < Punctuation > ( "," ) ,
1055
- CheckAtom < Variable > ( "b" ) ,
1056
- CheckAtom < Close > ( ")" )
1057
- ) ;
1058
- Assert . Equal ( @"lc(a,b)" , LaTeXParser . MathListToLaTeX ( list ) . ToString ( ) ) ;
1059
-
1060
- // Baseline for lookup as a non-command (not starting with \)
1061
- list = ParseLaTeX ( "lcm(a,b)" ) ;
1062
- Assert . Collection ( list ,
1063
- CheckAtom < LargeOperator > ( "lcm" ) ,
1064
- CheckAtom < Open > ( "(" ) ,
1065
- CheckAtom < Variable > ( "a" ) ,
1066
- CheckAtom < Punctuation > ( "," ) ,
1067
- CheckAtom < Variable > ( "b" ) ,
1068
- CheckAtom < Close > ( ")" )
1069
- ) ;
1070
- Assert . Equal ( @"\lcm (a,b)" , LaTeXParser . MathListToLaTeX ( list ) . ToString ( ) ) ;
1071
-
1072
- // Originally in https://github.com/verybadcat/CSharpMath/pull/143,
1073
- // the non-command dictionary of LaTeXCommandDictionary were implemented with a trie.
1074
- // With the above LaTeXSettings.CommandSymbols.Add calls, it would have looked like:
1075
- // [l] -> l[cm] -> lcm[12] -> @lcm12[3] -> lcm123[4]
1076
- // ^--> lcm123[5]
1077
- // where [square brackets] denote added characters compared to previous node
1078
- // and the @at sign denotes the node without an atom to provide
1079
- // Here we ensure that all behaviours of the trie carry over to the new SortedSet implementation
1080
-
1081
- // Test lookup fallbacks when trie node key (lcm12) does not fully match input (lcm1)
1082
- list = ParseLaTeX ( "lcm1(a,b)" ) ;
1083
- Assert . Collection ( list ,
1084
- CheckAtom < LargeOperator > ( "lcm" ) ,
1085
- CheckAtom < Number > ( "1" ) ,
1086
- CheckAtom < Open > ( "(" ) ,
1087
- CheckAtom < Variable > ( "a" ) ,
1088
- CheckAtom < Punctuation > ( "," ) ,
1089
- CheckAtom < Variable > ( "b" ) ,
1090
- CheckAtom < Close > ( ")" )
1091
- ) ;
1092
- Assert . Equal ( @"\lcm 1(a,b)" , LaTeXParser . MathListToLaTeX ( list ) . ToString ( ) ) ;
1093
-
1094
- // Test lookup success for trie node between above case and below case
1095
- list = ParseLaTeX ( "lcm12(a,b)" ) ;
1096
- Assert . Collection ( list ,
1097
- CheckAtom < LargeOperator > ( "lcm12" ) ,
1098
- CheckAtom < Open > ( "(" ) ,
1099
- CheckAtom < Variable > ( "a" ) ,
1100
- CheckAtom < Punctuation > ( "," ) ,
1101
- CheckAtom < Variable > ( "b" ) ,
1102
- CheckAtom < Close > ( ")" )
1103
- ) ;
1104
- Assert . Equal ( @"lcm12(a,b)" , LaTeXParser . MathListToLaTeX ( list ) . ToString ( ) ) ;
1105
-
1106
- // Test lookup fallbacks when trie node key (lcm123) fully matches input (lcm123) but has no atoms to provide
1107
- list = ParseLaTeX ( "lcm123(a,b)" ) ;
1108
- Assert . Collection ( list ,
1109
- CheckAtom < LargeOperator > ( "lcm12" ) ,
1110
- CheckAtom < Number > ( "3" ) ,
1111
- CheckAtom < Open > ( "(" ) ,
1112
- CheckAtom < Variable > ( "a" ) ,
1113
- CheckAtom < Punctuation > ( "," ) ,
1114
- CheckAtom < Variable > ( "b" ) ,
1115
- CheckAtom < Close > ( ")" )
1116
- ) ;
1117
- Assert . Equal ( @"lcm123(a,b)" , LaTeXParser . MathListToLaTeX ( list ) . ToString ( ) ) ;
1118
-
1119
- // Add a new shorter entry to ensure that the longest key matches instead of the last one
1120
- LaTeXSettings . CommandSymbols . Add ( @"lcm123" , new LargeOperator ( "lcm123" , false ) ) ;
1121
- list = ParseLaTeX ( "lcm1234(a,b)" ) ;
1122
- Assert . Collection ( list ,
1123
- CheckAtom < LargeOperator > ( "lcm1234" ) ,
1124
- CheckAtom < Open > ( "(" ) ,
1125
- CheckAtom < Variable > ( "a" ) ,
1126
- CheckAtom < Punctuation > ( "," ) ,
1127
- CheckAtom < Variable > ( "b" ) ,
1128
- CheckAtom < Close > ( ")" )
1129
- ) ;
1130
- Assert . Equal ( @"lcm1234(a,b)" , LaTeXParser . MathListToLaTeX ( list ) . ToString ( ) ) ;
1024
+ lock ( LaTeXSettings . Commands ) {
1025
+ var input = @"\lcm(a,b)" ;
1026
+ var builder = new LaTeXParser ( input ) ;
1027
+ var ( list , error ) = builder . Build ( ) ;
1028
+ Assert . Null ( list ) ;
1029
+ Assert . Equal ( @"Invalid command \lcm" , error ) ;
1030
+
1031
+ LaTeXSettings . CommandSymbols . Add ( @"\lcm" , new LargeOperator ( "lcm" , false ) ) ;
1032
+ list = ParseLaTeX ( input ) ;
1033
+ Assert . Collection ( list ,
1034
+ CheckAtom < LargeOperator > ( "lcm" ) ,
1035
+ CheckAtom < Open > ( "(" ) ,
1036
+ CheckAtom < Variable > ( "a" ) ,
1037
+ CheckAtom < Punctuation > ( "," ) ,
1038
+ CheckAtom < Variable > ( "b" ) ,
1039
+ CheckAtom < Close > ( ")" )
1040
+ ) ;
1041
+ Assert . Equal ( @"\lcm (a,b)" , LaTeXParser . MathListToLaTeX ( list ) . ToString ( ) ) ;
1042
+
1043
+ LaTeXSettings . CommandSymbols . Add ( @"lcm" , new LargeOperator ( "lcm" , false ) ) ;
1044
+ LaTeXSettings . CommandSymbols . Add ( @"lcm12" , new LargeOperator ( "lcm12" , false ) ) ;
1045
+ LaTeXSettings . CommandSymbols . Add ( @"lcm1234" , new LargeOperator ( "lcm1234" , false ) ) ;
1046
+ LaTeXSettings . CommandSymbols . Add ( @"lcm1235" , new LargeOperator ( "lcm1235" , false ) ) ;
1047
+
1048
+ // Does not match custom atoms added above
1049
+ list = ParseLaTeX ( "lc(a,b)" ) ;
1050
+ Assert . Collection ( list ,
1051
+ CheckAtom < Variable > ( "l" ) ,
1052
+ CheckAtom < Variable > ( "c" ) ,
1053
+ CheckAtom < Open > ( "(" ) ,
1054
+ CheckAtom < Variable > ( "a" ) ,
1055
+ CheckAtom < Punctuation > ( "," ) ,
1056
+ CheckAtom < Variable > ( "b" ) ,
1057
+ CheckAtom < Close > ( ")" )
1058
+ ) ;
1059
+ Assert . Equal ( @"lc(a,b)" , LaTeXParser . MathListToLaTeX ( list ) . ToString ( ) ) ;
1060
+
1061
+ // Baseline for lookup as a non-command (not starting with \)
1062
+ list = ParseLaTeX ( "lcm(a,b)" ) ;
1063
+ Assert . Collection ( list ,
1064
+ CheckAtom < LargeOperator > ( "lcm" ) ,
1065
+ CheckAtom < Open > ( "(" ) ,
1066
+ CheckAtom < Variable > ( "a" ) ,
1067
+ CheckAtom < Punctuation > ( "," ) ,
1068
+ CheckAtom < Variable > ( "b" ) ,
1069
+ CheckAtom < Close > ( ")" )
1070
+ ) ;
1071
+ Assert . Equal ( @"\lcm (a,b)" , LaTeXParser . MathListToLaTeX ( list ) . ToString ( ) ) ;
1072
+
1073
+ // Originally in https://github.com/verybadcat/CSharpMath/pull/143,
1074
+ // the non-command dictionary of LaTeXCommandDictionary were implemented with a trie.
1075
+ // With the above LaTeXSettings.CommandSymbols.Add calls, it would have looked like:
1076
+ // [l] -> l[cm] -> lcm[12] -> @lcm12[3] -> lcm123[4]
1077
+ // ^--> lcm123[5]
1078
+ // where [square brackets] denote added characters compared to previous node
1079
+ // and the @at sign denotes the node without an atom to provide
1080
+ // Here we ensure that all behaviours of the trie carry over to the new SortedSet implementation
1081
+
1082
+ // Test lookup fallbacks when trie node key (lcm12) does not fully match input (lcm1)
1083
+ list = ParseLaTeX ( "lcm1(a,b)" ) ;
1084
+ Assert . Collection ( list ,
1085
+ CheckAtom < LargeOperator > ( "lcm" ) ,
1086
+ CheckAtom < Number > ( "1" ) ,
1087
+ CheckAtom < Open > ( "(" ) ,
1088
+ CheckAtom < Variable > ( "a" ) ,
1089
+ CheckAtom < Punctuation > ( "," ) ,
1090
+ CheckAtom < Variable > ( "b" ) ,
1091
+ CheckAtom < Close > ( ")" )
1092
+ ) ;
1093
+ Assert . Equal ( @"\lcm 1(a,b)" , LaTeXParser . MathListToLaTeX ( list ) . ToString ( ) ) ;
1094
+
1095
+ // Test lookup success for trie node between above case and below case
1096
+ list = ParseLaTeX ( "lcm12(a,b)" ) ;
1097
+ Assert . Collection ( list ,
1098
+ CheckAtom < LargeOperator > ( "lcm12" ) ,
1099
+ CheckAtom < Open > ( "(" ) ,
1100
+ CheckAtom < Variable > ( "a" ) ,
1101
+ CheckAtom < Punctuation > ( "," ) ,
1102
+ CheckAtom < Variable > ( "b" ) ,
1103
+ CheckAtom < Close > ( ")" )
1104
+ ) ;
1105
+ Assert . Equal ( @"lcm12(a,b)" , LaTeXParser . MathListToLaTeX ( list ) . ToString ( ) ) ;
1106
+
1107
+ // Test lookup fallbacks when trie node key (lcm123) fully matches input (lcm123) but has no atoms to provide
1108
+ list = ParseLaTeX ( "lcm123(a,b)" ) ;
1109
+ Assert . Collection ( list ,
1110
+ CheckAtom < LargeOperator > ( "lcm12" ) ,
1111
+ CheckAtom < Number > ( "3" ) ,
1112
+ CheckAtom < Open > ( "(" ) ,
1113
+ CheckAtom < Variable > ( "a" ) ,
1114
+ CheckAtom < Punctuation > ( "," ) ,
1115
+ CheckAtom < Variable > ( "b" ) ,
1116
+ CheckAtom < Close > ( ")" )
1117
+ ) ;
1118
+ Assert . Equal ( @"lcm123(a,b)" , LaTeXParser . MathListToLaTeX ( list ) . ToString ( ) ) ;
1119
+
1120
+ // Add a new shorter entry to ensure that the longest key matches instead of the last one
1121
+ LaTeXSettings . CommandSymbols . Add ( @"lcm123" , new LargeOperator ( "lcm123" , false ) ) ;
1122
+ list = ParseLaTeX ( "lcm1234(a,b)" ) ;
1123
+ Assert . Collection ( list ,
1124
+ CheckAtom < LargeOperator > ( "lcm1234" ) ,
1125
+ CheckAtom < Open > ( "(" ) ,
1126
+ CheckAtom < Variable > ( "a" ) ,
1127
+ CheckAtom < Punctuation > ( "," ) ,
1128
+ CheckAtom < Variable > ( "b" ) ,
1129
+ CheckAtom < Close > ( ")" )
1130
+ ) ;
1131
+ Assert . Equal ( @"lcm1234(a,b)" , LaTeXParser . MathListToLaTeX ( list ) . ToString ( ) ) ;
1132
+ }
1131
1133
}
1132
1134
1133
1135
[ Theory ]
0 commit comments