column.spec.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. import FixedColumn from '../common/text/column/fixed'
  8. import TokenColumn from '../common/text/column/token'
  9. import { Column, ColumnHelpers } from 'mol-data/db'
  10. const lines = [
  11. '1.123 abc',
  12. '1.00 a',
  13. '1.1 bcd ',
  14. '',
  15. ' 5'
  16. ]
  17. const linesData = lines.join('\n');
  18. const linesTokens = (function () {
  19. const tokens: number[] = [];
  20. let last = 0;
  21. for (const l of lines) {
  22. tokens.push(last, last + l.length);
  23. last += l.length + 1;
  24. }
  25. if (tokens[tokens.length - 1] > linesData.length) tokens[tokens.length - 1] = linesData.length;
  26. return tokens;
  27. }());
  28. describe('fixed text column', () => {
  29. const col = FixedColumn({ data: linesData, indices: linesTokens, count: lines.length });
  30. const col1 = col(0, 5, Column.Schema.float);
  31. const col2 = col(5, 4, Column.Schema.str);
  32. it('number', () => {
  33. expect(col1.value(0)).toBe(1.123);
  34. expect(col1.value(1)).toBe(1.0);
  35. expect(col1.value(2)).toBe(1.1);
  36. expect(col1.value(3)).toBe(0);
  37. expect(col1.value(4)).toBe(5);
  38. })
  39. it('str', () => {
  40. expect(col2.value(0)).toBe('abc');
  41. expect(col2.value(1)).toBe('a');
  42. expect(col2.value(2)).toBe('bc');
  43. expect(col2.value(3)).toBe('');
  44. expect(col2.value(4)).toBe('');
  45. })
  46. });
  47. describe('token text column', () => {
  48. const tokensData = '321';
  49. const col = TokenColumn({ data: tokensData, indices: [0, 1, 1, 2, 2, 3], count: 3 });
  50. const col1 = col(Column.Schema.int);
  51. it('number', () => {
  52. expect(col1.value(0)).toBe(3);
  53. expect(col1.value(1)).toBe(2);
  54. expect(col1.value(2)).toBe(1);
  55. })
  56. });
  57. describe('binary column', () => {
  58. it('window works', () => {
  59. const xs = new Float64Array([1, 2, 3, 4]);
  60. const w1 = ColumnHelpers.typedArrayWindow(xs, { start: 1 });
  61. const w2 = ColumnHelpers.typedArrayWindow(xs, { start: 2, end: 4 });
  62. expect(w1.length).toBe(3);
  63. for (let i = 0; i < w1.length; i++) expect(w1[i]).toBe(xs[i + 1]);
  64. expect(w2.length).toBe(2);
  65. for (let i = 0; i < w2.length; i++) expect(w2[i]).toBe(xs[i + 2]);
  66. });
  67. })