pattern_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package number
  5. import (
  6. "reflect"
  7. "testing"
  8. "unsafe"
  9. )
  10. var testCases = []struct {
  11. pat string
  12. want *Pattern
  13. }{{
  14. "#",
  15. &Pattern{
  16. FormatWidth: 1,
  17. // TODO: Should MinIntegerDigits be 1?
  18. },
  19. }, {
  20. "0",
  21. &Pattern{
  22. FormatWidth: 1,
  23. RoundingContext: RoundingContext{
  24. MinIntegerDigits: 1,
  25. },
  26. },
  27. }, {
  28. "+0",
  29. &Pattern{
  30. Affix: "\x01+\x00",
  31. FormatWidth: 2,
  32. RoundingContext: RoundingContext{
  33. MinIntegerDigits: 1,
  34. },
  35. },
  36. }, {
  37. "0+",
  38. &Pattern{
  39. Affix: "\x00\x01+",
  40. FormatWidth: 2,
  41. RoundingContext: RoundingContext{
  42. MinIntegerDigits: 1,
  43. },
  44. },
  45. }, {
  46. "0000",
  47. &Pattern{
  48. FormatWidth: 4,
  49. RoundingContext: RoundingContext{
  50. MinIntegerDigits: 4,
  51. },
  52. },
  53. }, {
  54. ".#",
  55. &Pattern{
  56. FormatWidth: 2,
  57. RoundingContext: RoundingContext{
  58. MaxFractionDigits: 1,
  59. },
  60. },
  61. }, {
  62. "#0.###",
  63. &Pattern{
  64. FormatWidth: 6,
  65. RoundingContext: RoundingContext{
  66. MinIntegerDigits: 1,
  67. MaxFractionDigits: 3,
  68. },
  69. },
  70. }, {
  71. "#0.######",
  72. &Pattern{
  73. FormatWidth: 9,
  74. RoundingContext: RoundingContext{
  75. MinIntegerDigits: 1,
  76. MaxFractionDigits: 6,
  77. },
  78. },
  79. }, {
  80. "#,0",
  81. &Pattern{
  82. FormatWidth: 3,
  83. GroupingSize: [2]uint8{1, 0},
  84. RoundingContext: RoundingContext{
  85. MinIntegerDigits: 1,
  86. },
  87. },
  88. }, {
  89. "#,0.00",
  90. &Pattern{
  91. FormatWidth: 6,
  92. GroupingSize: [2]uint8{1, 0},
  93. RoundingContext: RoundingContext{
  94. MinIntegerDigits: 1,
  95. MinFractionDigits: 2,
  96. MaxFractionDigits: 2,
  97. },
  98. },
  99. }, {
  100. "#,##0.###",
  101. &Pattern{
  102. FormatWidth: 9,
  103. GroupingSize: [2]uint8{3, 0},
  104. RoundingContext: RoundingContext{
  105. MinIntegerDigits: 1,
  106. MaxFractionDigits: 3,
  107. },
  108. },
  109. }, {
  110. "#,##,##0.###",
  111. &Pattern{
  112. FormatWidth: 12,
  113. GroupingSize: [2]uint8{3, 2},
  114. RoundingContext: RoundingContext{
  115. MinIntegerDigits: 1,
  116. MaxFractionDigits: 3,
  117. },
  118. },
  119. }, {
  120. // Ignore additional separators.
  121. "#,####,##,##0.###",
  122. &Pattern{
  123. FormatWidth: 17,
  124. GroupingSize: [2]uint8{3, 2},
  125. RoundingContext: RoundingContext{
  126. MinIntegerDigits: 1,
  127. MaxFractionDigits: 3,
  128. },
  129. },
  130. }, {
  131. "#E0",
  132. &Pattern{
  133. FormatWidth: 3,
  134. RoundingContext: RoundingContext{
  135. MaxIntegerDigits: 1,
  136. MinExponentDigits: 1,
  137. },
  138. },
  139. }, {
  140. // At least one exponent digit is required. As long as this is true, one can
  141. // determine that scientific rendering is needed if MinExponentDigits > 0.
  142. "#E#",
  143. nil,
  144. }, {
  145. "0E0",
  146. &Pattern{
  147. FormatWidth: 3,
  148. RoundingContext: RoundingContext{
  149. MinIntegerDigits: 1,
  150. MinExponentDigits: 1,
  151. },
  152. },
  153. }, {
  154. "##0.###E00",
  155. &Pattern{
  156. FormatWidth: 10,
  157. RoundingContext: RoundingContext{
  158. MinIntegerDigits: 1,
  159. MaxIntegerDigits: 3,
  160. MaxFractionDigits: 3,
  161. MinExponentDigits: 2,
  162. },
  163. },
  164. }, {
  165. "##00.0#E0",
  166. &Pattern{
  167. FormatWidth: 9,
  168. RoundingContext: RoundingContext{
  169. MinIntegerDigits: 2,
  170. MaxIntegerDigits: 4,
  171. MinFractionDigits: 1,
  172. MaxFractionDigits: 2,
  173. MinExponentDigits: 1,
  174. },
  175. },
  176. }, {
  177. "#00.0E+0",
  178. &Pattern{
  179. FormatWidth: 8,
  180. Flags: AlwaysExpSign,
  181. RoundingContext: RoundingContext{
  182. MinIntegerDigits: 2,
  183. MaxIntegerDigits: 3,
  184. MinFractionDigits: 1,
  185. MaxFractionDigits: 1,
  186. MinExponentDigits: 1,
  187. },
  188. },
  189. }, {
  190. "0.0E++0",
  191. nil,
  192. }, {
  193. "#0E+",
  194. nil,
  195. }, {
  196. // significant digits
  197. "@",
  198. &Pattern{
  199. FormatWidth: 1,
  200. RoundingContext: RoundingContext{
  201. MinSignificantDigits: 1,
  202. MaxSignificantDigits: 1,
  203. MaxFractionDigits: -1,
  204. },
  205. },
  206. }, {
  207. // significant digits
  208. "@@@@",
  209. &Pattern{
  210. FormatWidth: 4,
  211. RoundingContext: RoundingContext{
  212. MinSignificantDigits: 4,
  213. MaxSignificantDigits: 4,
  214. MaxFractionDigits: -1,
  215. },
  216. },
  217. }, {
  218. "@###",
  219. &Pattern{
  220. FormatWidth: 4,
  221. RoundingContext: RoundingContext{
  222. MinSignificantDigits: 1,
  223. MaxSignificantDigits: 4,
  224. MaxFractionDigits: -1,
  225. },
  226. },
  227. }, {
  228. // Exponents in significant digits mode gets normalized.
  229. "@@E0",
  230. &Pattern{
  231. FormatWidth: 4,
  232. RoundingContext: RoundingContext{
  233. MinIntegerDigits: 1,
  234. MaxIntegerDigits: 1,
  235. MinFractionDigits: 1,
  236. MaxFractionDigits: 1,
  237. MinExponentDigits: 1,
  238. },
  239. },
  240. }, {
  241. "@###E00",
  242. &Pattern{
  243. FormatWidth: 7,
  244. RoundingContext: RoundingContext{
  245. MinIntegerDigits: 1,
  246. MaxIntegerDigits: 1,
  247. MinFractionDigits: 0,
  248. MaxFractionDigits: 3,
  249. MinExponentDigits: 2,
  250. },
  251. },
  252. }, {
  253. // The significant digits mode does not allow fractions.
  254. "@###.#E0",
  255. nil,
  256. }, {
  257. //alternative negative pattern
  258. "#0.###;(#0.###)",
  259. &Pattern{
  260. Affix: "\x00\x00\x01(\x01)",
  261. NegOffset: 2,
  262. FormatWidth: 6,
  263. RoundingContext: RoundingContext{
  264. MinIntegerDigits: 1,
  265. MaxFractionDigits: 3,
  266. },
  267. },
  268. }, {
  269. // Rounding increment
  270. "1.05",
  271. &Pattern{
  272. FormatWidth: 4,
  273. RoundingContext: RoundingContext{
  274. Increment: 105,
  275. IncrementScale: 2,
  276. MinIntegerDigits: 1,
  277. MinFractionDigits: 2,
  278. MaxFractionDigits: 2,
  279. },
  280. },
  281. }, {
  282. // Rounding increment with grouping
  283. "1,05",
  284. &Pattern{
  285. FormatWidth: 4,
  286. GroupingSize: [2]uint8{2, 0},
  287. RoundingContext: RoundingContext{
  288. Increment: 105,
  289. IncrementScale: 0,
  290. MinIntegerDigits: 3,
  291. MinFractionDigits: 0,
  292. MaxFractionDigits: 0,
  293. },
  294. },
  295. }, {
  296. "0.0%",
  297. &Pattern{
  298. Affix: "\x00\x01%",
  299. FormatWidth: 4,
  300. RoundingContext: RoundingContext{
  301. DigitShift: 2,
  302. MinIntegerDigits: 1,
  303. MinFractionDigits: 1,
  304. MaxFractionDigits: 1,
  305. },
  306. },
  307. }, {
  308. "0.0‰",
  309. &Pattern{
  310. Affix: "\x00\x03‰",
  311. FormatWidth: 4,
  312. RoundingContext: RoundingContext{
  313. DigitShift: 3,
  314. MinIntegerDigits: 1,
  315. MinFractionDigits: 1,
  316. MaxFractionDigits: 1,
  317. },
  318. },
  319. }, {
  320. "#,##0.00¤",
  321. &Pattern{
  322. Affix: "\x00\x02¤",
  323. FormatWidth: 9,
  324. GroupingSize: [2]uint8{3, 0},
  325. RoundingContext: RoundingContext{
  326. MinIntegerDigits: 1,
  327. MinFractionDigits: 2,
  328. MaxFractionDigits: 2,
  329. },
  330. },
  331. }, {
  332. "#,##0.00 ¤;(#,##0.00 ¤)",
  333. &Pattern{Affix: "\x00\x04\u00a0¤\x01(\x05\u00a0¤)",
  334. NegOffset: 6,
  335. FormatWidth: 10,
  336. GroupingSize: [2]uint8{3, 0},
  337. RoundingContext: RoundingContext{
  338. DigitShift: 0,
  339. MinIntegerDigits: 1,
  340. MinFractionDigits: 2,
  341. MaxFractionDigits: 2,
  342. },
  343. },
  344. }, {
  345. // padding
  346. "*x#",
  347. &Pattern{
  348. PadRune: 'x',
  349. FormatWidth: 1,
  350. },
  351. }, {
  352. // padding
  353. "#*x",
  354. &Pattern{
  355. PadRune: 'x',
  356. FormatWidth: 1,
  357. Flags: PadBeforeSuffix,
  358. },
  359. }, {
  360. "*xpre#suf",
  361. &Pattern{
  362. Affix: "\x03pre\x03suf",
  363. PadRune: 'x',
  364. FormatWidth: 7,
  365. },
  366. }, {
  367. "pre*x#suf",
  368. &Pattern{
  369. Affix: "\x03pre\x03suf",
  370. PadRune: 'x',
  371. FormatWidth: 7,
  372. Flags: PadAfterPrefix,
  373. },
  374. }, {
  375. "pre#*xsuf",
  376. &Pattern{
  377. Affix: "\x03pre\x03suf",
  378. PadRune: 'x',
  379. FormatWidth: 7,
  380. Flags: PadBeforeSuffix,
  381. },
  382. }, {
  383. "pre#suf*x",
  384. &Pattern{
  385. Affix: "\x03pre\x03suf",
  386. PadRune: 'x',
  387. FormatWidth: 7,
  388. Flags: PadAfterSuffix,
  389. },
  390. }, {
  391. `* #0 o''clock`,
  392. &Pattern{Affix: "\x00\x09 o\\'clock",
  393. FormatWidth: 10,
  394. PadRune: 32,
  395. RoundingContext: RoundingContext{
  396. MinIntegerDigits: 0x1,
  397. },
  398. },
  399. }, {
  400. `'123'* #0'456'`,
  401. &Pattern{Affix: "\x05'123'\x05'456'",
  402. FormatWidth: 8,
  403. PadRune: 32,
  404. RoundingContext: RoundingContext{
  405. MinIntegerDigits: 0x1,
  406. },
  407. Flags: PadAfterPrefix},
  408. }, {
  409. // no duplicate padding
  410. "*xpre#suf*x", nil,
  411. }, {
  412. // no duplicate padding
  413. "*xpre#suf*x", nil,
  414. }}
  415. func TestParsePattern(t *testing.T) {
  416. for i, tc := range testCases {
  417. t.Run(tc.pat, func(t *testing.T) {
  418. f, err := ParsePattern(tc.pat)
  419. if !reflect.DeepEqual(f, tc.want) {
  420. t.Errorf("%d:%s:\ngot %#v;\nwant %#v", i, tc.pat, f, tc.want)
  421. }
  422. if got, want := err != nil, tc.want == nil; got != want {
  423. t.Errorf("%d:%s:error: got %v; want %v", i, tc.pat, err, want)
  424. }
  425. })
  426. }
  427. }
  428. func TestPatternSize(t *testing.T) {
  429. if sz := unsafe.Sizeof(Pattern{}); sz > 56 {
  430. t.Errorf("got %d; want <= 56", sz)
  431. }
  432. }