text:
import { dateTime, TimeRange } from '@grafana/data';
import { setDataSourceSrv } from '@grafana/runtime';

import { silenceConsoleOutput } from '../../../test/core/utils/silenceConsoleOutput';
import { initTemplateSrv } from '../../../test/helpers/initTemplateSrv';
import { mockDataSource, MockDataSourceSrv } from '../alerting/unified/mocks';
import { VariableAdapter, variableAdapters } from '../variables/adapters';
import { createAdHocVariableAdapter } from '../variables/adhoc/adapter';
import { createQueryVariableAdapter } from '../variables/query/adapter';
import { VariableModel } from '../variables/types';

import { FormatRegistryID } from './formatRegistry';

const key = 'key';

variableAdapters.setInit(() => [
  createQueryVariableAdapter() as unknown as VariableAdapter<VariableModel>,
  createAdHocVariableAdapter() as unknown as VariableAdapter<VariableModel>,
]);

describe('templateSrv', () => {
  silenceConsoleOutput();
  let _templateSrv: any;

  describe('init', () => {
    beforeEach(() => {
      _templateSrv = initTemplateSrv(key, [{ type: 'query', name: 'test', current: { value: 'oogle' } }]);
    });

    it('should initialize template data', () => {
      const target = _templateSrv.replace('this.[[test]].filters');
      expect(target).toBe('this.oogle.filters');
    });
  });

  describe('replace can pass scoped vars', () => {
    beforeEach(() => {
      _templateSrv = initTemplateSrv(key, [{ type: 'query', name: 'test', current: { value: 'oogle' } }]);
    });

    it('scoped vars should support objects', () => {
      const target = _templateSrv.replace('${series.name} ${series.nested.field}', {
        series: { value: { name: 'Server1', nested: { field: 'nested' } } },
      });
      expect(target).toBe('Server1 nested');
    });

    it('built in vars should support objects', () => {
      _templateSrv.setGlobalVariable('__dashboard', {
        value: { name: 'hello' },
      });
      const target = _templateSrv.replace('${__dashboard.name}');
      expect(target).toBe('hello');
    });

    it('scoped vars should support objects with propert names with dot', () => {
      const target = _templateSrv.replace('${series.name} ${series.nested["field.with.dot"]}', {
        series: { value: { name: 'Server1', nested: { 'field.with.dot': 'nested' } } },
      });
      expect(target).toBe('Server1 nested');
    });

    it('scoped vars should support arrays of objects', () => {
      const target = _templateSrv.replace('${series.rows[0].name} ${series.rows[1].name}', {
        series: { value: { rows: [{ name: 'first' }, { name: 'second' }] } },
      });
      expect(target).toBe('first second');
    });

    it('should replace $test with scoped value', () => {
      const target = _templateSrv.replace('this.$test.filters', {
        test: { value: 'mupp', text: 'asd' },
      });
      expect(target).toBe('this.mupp.filters');
    });

    it('should replace ${test} with scoped value', () => {
      const target = _templateSrv.replace('this.${test}.filters', {
        test: { value: 'mupp', text: 'asd' },
      });
      expect(target).toBe('this.mupp.filters');
    });

    it('should replace ${test:glob} with scoped value', () => {
      const target = _templateSrv.replace('this.${test:glob}.filters', {
        test: { value: 'mupp', text: 'asd' },
      });
      expect(target).toBe('this.mupp.filters');
    });

    it('should replace $test with scoped text', () => {
      const target = _templateSrv.replaceWithText('this.$test.filters', {
        test: { value: 'mupp', text: 'asd' },
      });
      expect(target).toBe('this.asd.filters');
    });

    it('should replace ${test} with scoped text', () => {
      const target = _templateSrv.replaceWithText('this.${test}.filters', {
        test: { value: 'mupp', text: 'asd' },
      });
      expect(target).toBe('this.asd.filters');
    });

    it('should replace ${test.name} with scoped text', () => {
      const target = _templateSrv.replaceWithText('this.${test.name}.filters', {
        test: { value: { name: 'mupp' }, text: 'asd' },
      });
      expect(target).toBe('this.mupp.filters');
    });

    it('should not replace ${test:glob} with scoped text', () => {
      const target = _templateSrv.replaceWithText('this.${test:glob}.filters', {
        test: { value: 'mupp', text: 'asd' },
      });
      expect(target).toBe('this.mupp.filters');
    });
  });

  describe('getAdhocFilters', () => {
    beforeEach(() => {
      _templateSrv = initTemplateSrv(key, [
        {
          type: 'datasource',
          name: 'ds',
          current: { value: 'logstash', text: 'logstash' },
        },
        { type: 'adhoc', name: 'test', datasource: { uid: 'oogle' }, filters: [1] },
        { type: 'adhoc', name: 'test2', datasource: { uid: '$ds' }, filters: [2] },
      ]);
      setDataSourceSrv(
        new MockDataSourceSrv({
          oogle: mockDataSource({
            name: 'oogle',
            uid: 'oogle',
          }),
        })
      );
    });

    it('should return filters if datasourceName match', () => {
      const filters = _templateSrv.getAdhocFilters('oogle');
      expect(filters).toMatchObject([1]);
    });

    it('should return empty array if datasourceName does not match', () => {
      const filters = _templateSrv.getAdhocFilters('oogleasdasd');
      expect(filters).toMatchObject([]);
    });

    it('should return filters when datasourceName match via data source variable', () => {
      const filters = _templateSrv.getAdhocFilters('logstash');
      expect(filters).toMatchObject([2]);
    });
  });

  describe('replace can pass multi / all format', () => {
    beforeEach(() => {
      _templateSrv = initTemplateSrv(key, [
        {
          type: 'query',
          name: 'test',
          current: { value: ['value1', 'value2'] },
        },
      ]);
    });

    it('should replace $test with globbed value', () => {
      const target = _templateSrv.replace('this.$test.filters', {}, 'glob');
      expect(target).toBe('this.{value1,value2}.filters');
    });

    describe('when the globbed variable only has one value', () => {
      beforeEach(() => {
        _templateSrv = initTemplateSrv(key, [
          {
            type: 'query',
            name: 'test',
            current: { value: ['value1'] },
          },
        ]);
      });

      it('should not glob the value', () => {
        const target = _templateSrv.replace('this.$test.filters', {}, 'glob');
        expect(target).toBe('this.value1.filters');
      });
    });

    it('should replace ${test} with globbed value', () => {
      const target = _templateSrv.replace('this.${test}.filters', {}, 'glob');
      expect(target).toBe('this.{value1,value2}.filters');
    });

    it('should replace ${test:glob} with globbed value', () => {
      const target = _templateSrv.replace('this.${test:glob}.filters', {});
      expect(target).toBe('this.{value1,value2}.filters');
    });

    it('should replace $test with piped value', () => {
      const target = _templateSrv.replace('this=$test', {}, 'pipe');
      expect(target).toBe('this=value1|value2');
    });

    it('should replace ${test} with piped value', () => {
      const target = _templateSrv.replace('this=${test}', {}, 'pipe');
      expect(target).toBe('this=value1|value2');
    });

    it('should replace ${test:pipe} with piped value', () => {
      const target = _templateSrv.replace('this=${test:pipe}', {});
      expect(target).toBe('this=value1|value2');
    });

    it('should replace ${test:pipe} with piped value and $test with globbed value', () => {
      const target = _templateSrv.replace('${test:pipe},$test', {}, 'glob');
      expect(target).toBe('value1|value2,{value1,value2}');
    });
  });

  describe('variable with all option', () => {
    beforeEach(() => {
      _templateSrv = initTemplateSrv(key, [
        {
          type: 'query',
          name: 'test',
          current: { value: '$__all' },
          options: [{ value: '$__all' }, { value: 'value1' }, { value: 'value2' }],
        },
      ]);
    });

    it('should replace $test with formatted all value', () => {
      const target = _templateSrv.replace('this.$test.filters', {}, 'glob');
      expect(target).toBe('this.{value1,value2}.filters');
    });

    it('should replace ${test} with formatted all value', () => {
      const target = _templateSrv.replace('this.${test}.filters', {}, 'glob');
      expect(target).toBe('this.{value1,value2}.filters');
    });

    it('should replace ${test:glob} with formatted all value', () => {
      const target = _templateSrv.replace('this.${test:glob}.filters', {});
      expect(target).toBe('this.{value1,value2}.filters');
    });

    it('should replace ${test:pipe} with piped value and $test with globbed value', () => {
      const target = _templateSrv.replace('${test:pipe},$test', {}, 'glob');
      expect(target).toBe('value1|value2,{value1,value2}');
    });

    it('should replace ${test:queryparam} with correct query parameter', () => {
      const target = _templateSrv.replace('${test:queryparam}', {});
      expect(target).toBe('var-test=All');
    });
  });

  describe('variable with all option and custom value', () => {
    beforeEach(() => {
      _templateSrv = initTemplateSrv(key, [
        {
          type: 'query',
          name: 'test',
          current: { value: '$__all' },
          allValue: '*',
          options: [{ value: 'value1' }, { value: 'value2' }],
        },
      ]);
    });

    it('should replace $test with formatted all value', () => {
      const target = _templateSrv.replace('this.$test.filters', {}, 'glob');
      expect(target).toBe('this.*.filters');
    });

    it('should replace ${test} with formatted all value', () => {
      const target = _templateSrv.replace('this.${test}.filters', {}, 'glob');
      expect(target).toBe('this.*.filters');
    });

    it('should replace ${test:glob} with formatted all value', () => {
      const target = _templateSrv.replace('this.${test:glob}.filters', {});
      expect(target).toBe('this.*.filters');
    });

    it('should replace ${test:text} with "all" value', () => {
      const target = _templateSrv.replace('this.${test:text}.filters', {});
      expect(target).toBe('this.All.filters');
    });

    it('should not escape custom all value', () => {
      const target = _templateSrv.replace('this.$test', {}, 'regex');
      expect(target).toBe('this.*');
    });

    it('should replace ${test:queryparam} with correct query parameter', () => {
      const target = _templateSrv.replace('${test:queryparam}', {});
      expect(target).toBe('var-test=All');
    });
  });

  describe('lucene format', () => {
    it('should properly escape $test with lucene escape sequences', () => {
      _templateSrv = initTemplateSrv(key, [{ type: 'query', name: 'test', current: { value: 'value/4' } }]);
      const target = _templateSrv.replace('this:$test', {}, 'lucene');
      expect(target).toBe('this:value\\/4');
    });

    it('should properly escape ${test} with lucene escape sequences', () => {
      _templateSrv = initTemplateSrv(key, [{ type: 'query', name: 'test', current: { value: 'value/4' } }]);
      const target = _templateSrv.replace('this:${test}', {}, 'lucene');
      expect(target).toBe('this:value\\/4');
    });

    it('should properly escape ${test:lucene} with lucene escape sequences', () => {
      _templateSrv = initTemplateSrv(key, [{ type: 'query', name: 'test', current: { value: 'value/4' } }]);
      const target = _templateSrv.replace('this:${test:lucene}', {});
      expect(target).toBe('this:value\\/4');
    });
  });

  describe('html format', () => {
    it('should encode values html escape sequences', () => {
      _templateSrv = initTemplateSrv(key, [
        { type: 'query', name: 'test', current: { value: '' } },
      ]);
      const target = _templateSrv.replace('$test', {}, 'html');
      expect(target).toBe('<script>alert(asd)</script>');
    });
  });

  describe('format variable to string values', () => {
    it('single value should return value', () => {
      const result = _templateSrv.formatValue('test');
      expect(result).toBe('test');
    });

    it('should use glob format when unknown format provided', () => {
      let result = _templateSrv.formatValue('test', 'nonexistentformat');
      expect(result).toBe('test');
      result = _templateSrv.formatValue(['test', 'test1'], 'nonexistentformat');
      expect(result).toBe('{test,test1}');
    });

    it('multi value and glob format should render glob string', () => {
      const result = _templateSrv.formatValue(['test', 'test2'], 'glob');
      expect(result).toBe('{test,test2}');
    });

    it('multi value and lucene should render as lucene expr', () => {
      const result = _templateSrv.formatValue(['test', 'test2'], 'lucene');
      expect(result).toBe('("test" OR "test2")');
    });

    it('multi value and regex format should render regex string', () => {
      const result = _templateSrv.formatValue(['test.', 'test2'], 'regex');
      expect(result).toBe('(test\\.|test2)');
    });

    it('multi value and pipe should render pipe string', () => {
      const result = _templateSrv.formatValue(['test', 'test2'], 'pipe');
      expect(result).toBe('test|test2');
    });

    it('multi value and distributed should render distributed string', () => {
      const result = _templateSrv.formatValue(['test', 'test2'], 'distributed', {
        name: 'build',
      });
      expect(result).toBe('test,build=test2');
    });

    it('multi value and distributed should render when not string', () => {
      const result = _templateSrv.formatValue(['test'], 'distributed', {
        name: 'build',
      });
      expect(result).toBe('test');
    });

    it('multi value and csv format should render csv string', () => {
      const result = _templateSrv.formatValue(['test', 'test2'], 'csv');
      expect(result).toBe('test,test2');
    });

    it('multi value and percentencode format should render percent-encoded string', () => {
      const result = _templateSrv.formatValue(['foo()bar BAZ', 'test2'], 'percentencode');
      expect(result).toBe('%7Bfoo%28%29bar%20BAZ%2Ctest2%7D');
    });

    it('slash should be properly escaped in regex format', () => {
      const result = _templateSrv.formatValue('Gi3/14', 'regex');
      expect(result).toBe('Gi3\\/14');
    });

    it('single value and singlequote format should render string with value enclosed in single quotes', () => {
      const result = _templateSrv.formatValue('test', 'singlequote');
      expect(result).toBe("'test'");
    });

    it('multi value and singlequote format should render string with values enclosed in single quotes', () => {
      const result = _templateSrv.formatValue(['test', "test'2"], 'singlequote');
      expect(result).toBe("'test','test\\'2'");
    });

    it('single value and doublequote format should render string with value enclosed in double quotes', () => {
      const result = _templateSrv.formatValue('test', 'doublequote');
      expect(result).toBe('"test"');
    });

    it('multi value and doublequote format should render string with values enclosed in double quotes', () => {
      const result = _templateSrv.formatValue(['test', 'test"2'], 'doublequote');
      expect(result).toBe('"test","test\\"2"');
    });

    it('single value and sqlstring format should render string with value enclosed in single quotes', () => {
      const result = _templateSrv.formatValue("test'value", 'sqlstring');
      expect(result).toBe(`'test''value'`);
    });

    it('multi value and sqlstring format should render string with values enclosed in single quotes', () => {
      const result = _templateSrv.formatValue(['test', "test'value2"], 'sqlstring');
      expect(result).toBe(`'test','test''value2'`);
    });

    it('raw format should leave value intact and do no escaping', () => {
      const result = _templateSrv.formatValue("'test\n", 'raw');
      expect(result).toBe("'test\n");
    });
  });

  describe('can check if variable exists', () => {
    beforeEach(() => {
      _templateSrv = initTemplateSrv(key, [{ type: 'query', name: 'test', current: { value: 'oogle' } }]);
    });

    it('should return true if $test exists', () => {
      const result = _templateSrv.containsTemplate('$test');
      expect(result).toBe(true);
    });

    it('should return true if $test exists in string', () => {
      const result = _templateSrv.containsTemplate('something $test something');
      expect(result).toBe(true);
    });

    it('should return true if [[test]] exists in string', () => {
      const result = _templateSrv.containsTemplate('something [[test]] something');
      expect(result).toBe(true);
    });

    it('should return true if [[test:csv]] exists in string', () => {
      const result = _templateSrv.containsTemplate('something [[test:csv]] something');
      expect(result).toBe(true);
    });

    it('should return true if ${test} exists in string', () => {
      const result = _templateSrv.containsTemplate('something ${test} something');
      expect(result).toBe(true);
    });

    it('should return true if ${test:raw} exists in string', () => {
      const result = _templateSrv.containsTemplate('something ${test:raw} something');
      expect(result).toBe(true);
    });

    it('should return null if there are no variables in string', () => {
      const result = _templateSrv.containsTemplate('string without variables');
      expect(result).toBe(false);
    });
  });

  describe('can highlight variables in string', () => {
    beforeEach(() => {
      _templateSrv = initTemplateSrv(key, [{ type: 'query', name: 'test', current: { value: 'oogle' } }]);
    });

    it('should insert html', () => {
      const result = _templateSrv.highlightVariablesAsHtml('$test');
      expect(result).toBe('$test');
    });

    it('should insert html anywhere in string', () => {
      const result = _templateSrv.highlightVariablesAsHtml('this $test ok');
      expect(result).toBe('this $test ok');
    });

    it('should ignore if variables does not exist', () => {
      const result = _templateSrv.highlightVariablesAsHtml('this $google ok');
      expect(result).toBe('this $google ok');
    });
  });

  describe('updateIndex with simple value', () => {
    beforeEach(() => {
      _templateSrv = initTemplateSrv(key, [{ type: 'query', name: 'test', current: { value: 'muuuu' } }]);
    });

    it('should set current value and update template data', () => {
      const target = _templateSrv.replace('this.[[test]].filters');
      expect(target).toBe('this.muuuu.filters');
    });
  });

  describe('replaceWithText', () => {
    beforeEach(() => {
      _templateSrv = initTemplateSrv(key, [
        {
          type: 'query',
          name: 'server',
          current: { value: '{asd,asd2}', text: 'All' },
        },
        {
          type: 'interval',
          name: 'period',
          current: { value: '$__auto_interval_interval', text: 'auto' },
        },
        {
          type: 'textbox',
          name: 'empty_on_init',
          current: { value: '', text: '' },
        },
        {
          type: 'custom',
          name: 'foo',
          current: { value: 'constructor', text: 'constructor' },
        },
      ]);
      _templateSrv.setGrafanaVariable('$__auto_interval_interval', '13m');
      _templateSrv.updateIndex();
    });

    it('should replace with text except for grafanaVariables', () => {
      const target = _templateSrv.replaceWithText('Server: $server, period: $period');
      expect(target).toBe('Server: All, period: 13m');
    });

    it('should replace empty string-values with an empty string', () => {
      const target = _templateSrv.replaceWithText('Hello $empty_on_init');
      expect(target).toBe('Hello ');
    });

    it('should not return a string representation of a constructor property', () => {
      const target = _templateSrv.replaceWithText('$foo');
      expect(target).not.toBe('function Object() { [native code] }');
      expect(target).toBe('constructor');
    });
  });

  describe('replaceWithText can pass all / multi value', () => {
    beforeEach(() => {
      _templateSrv = initTemplateSrv(key, [
        {
          type: 'query',
          name: 'server',
          current: { value: ['server1', 'server2'], text: ['Server 1', 'Server 2'] },
        },
        {
          type: 'textbox',
          name: 'empty_on_init',
          current: { value: '', text: '' },
        },
        {
          type: 'query',
          name: 'databases',
          current: { value: '$__all', text: '' },
          options: [{ value: '$__all' }, { value: 'db1', text: 'Database 1' }, { value: 'db2', text: 'Database 2' }],
        },
        {
          type: 'custom',
          name: 'custom_all_value',
          allValue: 'CUSTOM_ALL',
          current: { value: '$__all', text: '' },
          options: [{ value: '$__all' }, { value: 'A-Value', text: 'This A' }, { value: 'B-Value', text: 'This B' }],
        },
      ]);
      _templateSrv.updateIndex();
    });

    it('should replace with text with variable label', () => {
      const target = _templateSrv.replaceWithText('Server: $server');
      expect(target).toBe('Server: Server 1 + Server 2');
    });

    it('should replace empty string-values with an empty string', () => {
      const target = _templateSrv.replaceWithText('Hello $empty_on_init');
      expect(target).toBe('Hello ');
    });

    it('should replace $__all with All', () => {
      const target = _templateSrv.replaceWithText('Db: $databases');
      expect(target).toBe('Db: All');
    });

    it('should replace $__all with All for values with custom all', () => {
      const target = _templateSrv.replaceWithText('Custom: $custom_all_value');
      expect(target).toBe('Custom: All');
    });
  });

  describe('built in interval variables', () => {
    beforeEach(() => {
      _templateSrv = initTemplateSrv(key, []);
    });

    it('should replace $__interval_ms with interval milliseconds', () => {
      const target = _templateSrv.replace('10 * $__interval_ms', {
        __interval_ms: { text: '100', value: '100' },
      });
      expect(target).toBe('10 * 100');
    });
  });

  describe('date formating', () => {
    beforeEach(() => {
      _templateSrv = initTemplateSrv(key, [], {
        from: dateTime(1594671549254),
        to: dateTime(1595237229747),
      } as TimeRange);
    });

    it('should replace ${__from} with ms epoch value', () => {
      const target = _templateSrv.replace('${__from}');
      expect(target).toBe('1594671549254');
    });

    it('should replace ${__from:date:seconds} with epoch in seconds', () => {
      const target = _templateSrv.replace('${__from:date:seconds}');
      expect(target).toBe('1594671549');
    });

    it('should replace ${__from:date} with iso date', () => {
      const target = _templateSrv.replace('${__from:date}');
      expect(target).toBe('2020-07-13T20:19:09.254Z');
    });

    it('should replace ${__from:date:iso} with iso date', () => {
      const target = _templateSrv.replace('${__from:date:iso}');
      expect(target).toBe('2020-07-13T20:19:09.254Z');
    });

    it('should replace ${__from:date:YYYY-MM} using custom format', () => {
      const target = _templateSrv.replace('${__from:date:YYYY-MM}');
      expect(target).toBe('2020-07');
    });
  });

  describe('handle objects gracefully', () => {
    beforeEach(() => {
      _templateSrv = initTemplateSrv(key, [{ type: 'query', name: 'test', current: { value: { test: 'A' } } }]);
    });

    it('should not pass object to custom function', () => {
      let passedValue: any = null;
      _templateSrv.replace('this.${test}.filters', {}, (value: any) => {
        passedValue = value;
      });

      expect(passedValue).toBe('[object Object]');
    });
  });

  describe('handle objects gracefully and call toString if defined', () => {
    beforeEach(() => {
      const value = { test: 'A', toString: () => 'hello' };
      _templateSrv = initTemplateSrv(key, [{ type: 'query', name: 'test', current: { value } }]);
    });

    it('should not pass object to custom function', () => {
      let passedValue: any = null;
      _templateSrv.replace('this.${test}.filters', {}, (value: any) => {
        passedValue = value;
      });

      expect(passedValue).toBe('hello');
    });
  });

  describe('adhoc variables', () => {
    beforeEach(() => {
      _templateSrv = initTemplateSrv(key, [
        {
          type: 'adhoc',
          name: 'adhoc',
          filters: [
            {
              condition: '',
              key: 'alertstate',
              operator: '=',
              value: 'firing',
            },
            {
              condition: '',
              key: 'alertname',
              operator: '=',
              value: 'ExampleAlertAlwaysFiring',
            },
          ],
        },
      ]);
    });

    it(`should not be handled by any registry items except for queryparam`, () => {
      const registryItems = Object.values(FormatRegistryID);
      for (const registryItem of registryItems) {
        if (registryItem === FormatRegistryID.queryParam) {
          continue;
        }

        const firstTarget = _templateSrv.replace(`\${adhoc:${registryItem}}`, {});
        expect(firstTarget).toBe('');

        const secondTarget = _templateSrv.replace('${adhoc}', {}, registryItem);
        expect(secondTarget).toBe('');
      }
    });
  });

  describe('queryparam', () => {
    beforeEach(() => {
      _templateSrv = initTemplateSrv(key, [
        {
          type: 'query',
          name: 'single',
          current: { value: 'value1' },
          options: [{ value: 'value1' }, { value: 'value2' }],
        },
        {
          type: 'query',
          name: 'multi',
          current: { value: ['value1', 'value2'] },
          options: [{ value: 'value1' }, { value: 'value2' }],
        },
        {
          type: 'adhoc',
          name: 'adhoc',
          filters: [
            {
              condition: '',
              key: 'alertstate',
              operator: '=',
              value: 'firing',
            },
            {
              condition: '',
              key: 'alertname',
              operator: '=',
              value: 'ExampleAlertAlwaysFiring',
            },
          ],
        },
      ]);
    });

    it('query variable with single value with queryparam format should return correct queryparam', () => {
      const target = _templateSrv.replace(`\${single:queryparam}`, {});
      expect(target).toBe('var-single=value1');
    });

    it('query variable with single value with queryparam format and scoped vars should return correct queryparam', () => {
      const target = _templateSrv.replace(`\${single:queryparam}`, { single: { value: 'value1', text: 'value1' } });
      expect(target).toBe('var-single=value1');
    });

    it('query variable with single value and queryparam format should return correct queryparam', () => {
      const target = _templateSrv.replace('${single}', {}, 'queryparam');
      expect(target).toBe('var-single=value1');
    });

    it('query variable with single value and queryparam format and scoped vars should return correct queryparam', () => {
      const target = _templateSrv.replace('${single}', { single: { value: 'value1', text: 'value1' } }, 'queryparam');
      expect(target).toBe('var-single=value1');
    });

    it('query variable with multi value with queryparam format should return correct queryparam', () => {
      const target = _templateSrv.replace(`\${multi:queryparam}`, {});
      expect(target).toBe('var-multi=value1&var-multi=value2');
    });

    it('query variable with multi value with queryparam format and scoped vars should return correct queryparam', () => {
      const target = _templateSrv.replace(`\${multi:queryparam}`, { multi: { value: 'value2', text: 'value2' } });
      expect(target).toBe('var-multi=value2');
    });

    it('query variable with multi value and queryparam format should return correct queryparam', () => {
      const target = _templateSrv.replace('${multi}', {}, 'queryparam');
      expect(target).toBe('var-multi=value1&var-multi=value2');
    });

    it('query variable with multi value and queryparam format and scoped vars should return correct queryparam', () => {
      const target = _templateSrv.replace('${multi}', { multi: { value: 'value2', text: 'value2' } }, 'queryparam');
      expect(target).toBe('var-multi=value2');
    });

    it('query variable with adhoc value with queryparam format should return correct queryparam', () => {
      const target = _templateSrv.replace(`\${adhoc:queryparam}`, {});
      expect(target).toBe('var-adhoc=alertstate%7C%3D%7Cfiring&var-adhoc=alertname%7C%3D%7CExampleAlertAlwaysFiring');
    });

    it('query variable with adhoc value with queryparam format should return correct queryparam', () => {
      const target = _templateSrv.replace(`\${adhoc:queryparam}`, { adhoc: { value: 'value2', text: 'value2' } });
      expect(target).toBe('var-adhoc=value2');
    });

    it('query variable with adhoc value and queryparam format should return correct queryparam', () => {
      const target = _templateSrv.replace('${adhoc}', {}, 'queryparam');
      expect(target).toBe('var-adhoc=alertstate%7C%3D%7Cfiring&var-adhoc=alertname%7C%3D%7CExampleAlertAlwaysFiring');
    });

    it('query variable with adhoc value and queryparam format should return correct queryparam', () => {
      const target = _templateSrv.replace('${adhoc}', { adhoc: { value: 'value2', text: 'value2' } }, 'queryparam');
      expect(target).toBe('var-adhoc=value2');
    });
  });
});


text: from lighthouse.search_indexes.lapack_le import *

redpajama_set_name: RedPajamaGithub


text:
<?php

namespace Opengento\Dontworry\Test\Unit\Model;

class ProductTest extends \PHPUnit_Framework_TestCase
{
    protected $amazing;

    protected function setUp()
    {
        $this->amazing = true;
    }

    public function testDoesProductModelPluginIsAmazing()
    {
        $this->assertTrue($this->amazing);
    }
}


text:
package eu.fiveminutes.network;

import android.os.Handler;
import android.os.Looper;

public final class MainThreadExecutorImpl implements MainThreadExecutor {

    private final Handler handler = new Handler(Looper.getMainLooper());

    @Override
    public void execute(final Runnable runnable) {
        handler.post(runnable);
    }
}


text:
/*! @license Firebase v4.4.0
Build: rev-380121f
Terms: https://firebase.google.com/terms/ */

'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.Reference = undefined;

var _args = require('./implementation/args');

var args = _interopRequireWildcard(_args);

var _blob = require('./implementation/blob');

var _error = require('./implementation/error');

var errorsExports = _interopRequireWildcard(_error);

var _location = require('./implementation/location');

var _metadata = require('./implementation/metadata');

var metadata = _interopRequireWildcard(_metadata);

var _object = require('./implementation/object');

var object = _interopRequireWildcard(_object);

var _path = require('./implementation/path');

var path = _interopRequireWildcard(_path);

var _requests = require('./implementation/requests');

var requests = _interopRequireWildcard(_requests);

var _string = require('./implementation/string');

var fbsString = _interopRequireWildcard(_string);

var _type = require('./implementation/type');

var type = _interopRequireWildcard(_type);

var _task = require('./task');

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

/**
 * Provides methods to interact with a bucket in the Firebase Storage service.
 * @param location An fbs.location, or the URL at
 *     which to base this object, in one of the following forms:
 *         gs://<bucket>/<object-path>
 *         http[s]://firebasestorage.googleapis.com/
 *                     <api-version>/b/<bucket>/o/<object-path>
 *     Any query or fragment strings will be ignored in the http[s]
 *     format. If no value is passed, the storage object will use a URL based on
 *     the project ID of the base firebase.App instance.
 */
/**
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
 * @fileoverview Defines the Firebase Storage Reference class.
 */
var Reference = /** @class */function () {
    function Reference(authWrapper, location) {
        this.authWrapper = authWrapper;
        if (location instanceof _location.Location) {
            this.location = location;
        } else {
            this.location = _location.Location.makeFromUrl(location);
        }
    }
    /**
     * @return The URL for the bucket and path this object references,
     *     in the form gs://<bucket>/<object-path>
     * @override
     */
    Reference.prototype.toString = function () {
        args.validate('toString', [], arguments);
        return 'gs://' + this.location.bucket + '/' + this.location.path;
    };
    Reference.prototype.newRef = function (authWrapper, location) {
        return new Reference(authWrapper, location);
    };
    Reference.prototype.mappings = function () {
        return metadata.getMappings();
    };
    /**
     * @return A reference to the object obtained by
     *     appending childPath, removing any duplicate, beginning, or trailing
     *     slashes.
     */
    Reference.prototype.child = function (childPath) {
        args.validate('child', [args.stringSpec()], arguments);
        var newPath = path.child(this.location.path, childPath);
        var location = new _location.Location(this.location.bucket, newPath);
        return this.newRef(this.authWrapper, location);
    };
    Object.defineProperty(Reference.prototype, "parent", {
        /**
         * @return A reference to the parent of the
         *     current object, or null if the current object is the root.
         */
        get: function get() {
            var newPath = path.parent(this.location.path);
            if (newPath === null) {
                return null;
            }
            var location = new _location.Location(this.location.bucket, newPath);
            return this.newRef(this.authWrapper, location);
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Reference.prototype, "root", {
        /**
         * @return An reference to the root of this
         *     object's bucket.
         */
        get: function get() {
            var location = new _location.Location(this.location.bucket, '');
            return this.newRef(this.authWrapper, location);
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Reference.prototype, "bucket", {
        get: function get() {
            return this.location.bucket;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Reference.prototype, "fullPath", {
        get: function get() {
            return this.location.path;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Reference.prototype, "name", {
        get: function get() {
            return path.lastComponent(this.location.path);
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(Reference.prototype, "storage", {
        get: function get() {
            return this.authWrapper.service();
        },
        enumerable: true,
        configurable: true
    });
    /**
     * Uploads a blob to this object's location.
     * @param data The blob to upload.
     * @return An UploadTask that lets you control and
     *     observe the upload.
     */
    Reference.prototype.put = function (data, metadata) {
        if (metadata === void 0) {
            metadata = null;
        }
        args.validate('put', [args.uploadDataSpec(), args.metadataSpec(true)], arguments);
        this.throwIfRoot_('put');
        return new _task.UploadTask(this, this.authWrapper, this.location, this.mappings(), new _blob.FbsBlob(data), metadata);
    };
    /**
     * Uploads a string to this object's location.
     * @param string The string to upload.
     * @param opt_format The format of the string to upload.
     * @return An UploadTask that lets you control and
     *     observe the upload.
     */
    Reference.prototype.putString = function (string, format, opt_metadata) {
        if (format === void 0) {
            format = _string.StringFormat.RAW;
        }
        args.validate('putString', [args.stringSpec(), args.stringSpec(fbsString.formatValidator, true), args.metadataSpec(true)], arguments);
        this.throwIfRoot_('putString');
        var data = fbsString.dataFromString(format, string);
        var metadata = object.clone(opt_metadata);
        if (!type.isDef(metadata['contentType']) && type.isDef(data.contentType)) {
            metadata['contentType'] = data.contentType;
        }
        return new _task.UploadTask(this, this.authWrapper, this.location, this.mappings(), new _blob.FbsBlob(data.data, true), metadata);
    };
    /**
     * Deletes the object at this location.
     * @return A promise that resolves if the deletion succeeds.
     */
    Reference.prototype.delete = function () {
        args.validate('delete', [], arguments);
        this.throwIfRoot_('delete');
        var self = this;
        return this.authWrapper.getAuthToken().then(function (authToken) {
            var requestInfo = requests.deleteObject(self.authWrapper, self.location);
            return self.authWrapper.makeRequest(requestInfo, authToken).getPromise();
        });
    };
    /**
     *     A promise that resolves with the metadata for this object. If this
     *     object doesn't exist or metadata cannot be retreived, the promise is
     *     rejected.
     */
    Reference.prototype.getMetadata = function () {
        args.validate('getMetadata', [], arguments);
        this.throwIfRoot_('getMetadata');
        var self = this;
        return this.authWrapper.getAuthToken().then(function (authToken) {
            var requestInfo = requests.getMetadata(self.authWrapper, self.location, self.mappings());
            return self.authWrapper.makeRequest(requestInfo, authToken).getPromise();
        });
    };
    /**
     * Updates the metadata for this object.
     * @param metadata The new metadata for the object.
     *     Only values that have been explicitly set will be changed. Explicitly
     *     setting a value to null will remove the metadata.
     * @return A promise that resolves
     *     with the new metadata for this object.
     *     @see firebaseStorage.Reference.prototype.getMetadata
     */
    Reference.prototype.updateMetadata = function (metadata) {
        args.validate('updateMetadata', [args.metadataSpec()], arguments);
        this.throwIfRoot_('updateMetadata');
        var self = this;
        return this.authWrapper.getAuthToken().then(function (authToken) {
            var requestInfo = requests.updateMetadata(self.authWrapper, self.location, metadata, self.mappings());
            return self.authWrapper.makeRequest(requestInfo, authToken).getPromise();
        });
    };
    /**
     * @return A promise that resolves with the download
     *     URL for this object.
     */
    Reference.prototype.getDownloadURL = function () {
        args.validate('getDownloadURL', [], arguments);
        this.throwIfRoot_('getDownloadURL');
        return this.getMetadata().then(function (metadata) {
            var url = metadata['downloadURLs'][0];
            if (type.isDef(url)) {
                return url;
            } else {
                throw errorsExports.noDownloadURL();
            }
        });
    };
    Reference.prototype.throwIfRoot_ = function (name) {
        if (this.location.path === '') {
            throw errorsExports.invalidRootOperation(name);
        }
    };
    return Reference;
}();
exports.Reference = Reference;
//# sourceMappingURL=reference.js.map


text:
package com.olegych.scastie.client
package components

import com.olegych.scastie.client.components.editor.EditorKeymaps
import japgolly.scalajs.react._

import vdom.all._
import extra._

final case class NewButton(isNewSnippetModalClosed: Boolean,
                           openNewSnippetModal: Reusable[Callback],
                           closeNewSnippetModal: Reusable[Callback],
                           newSnippet: Reusable[Callback]) {
  @inline def render: VdomElement = NewButton.component(this)
}

object NewButton {
  implicit val reusability: Reusability[NewButton] =
    Reusability.derive[NewButton]

  def render(props: NewButton): VdomElement = {

    li(
      title := s"New code snippet (${EditorKeymaps.openNewSnippetModal.getName})",
      role := "button",
      onClick --> props.openNewSnippetModal,
      cls := "btn"
    )(
      i(cls := "fa fa-file-o"),
      span("New"),
      PromptModal(
        modalText = "New Snippet",
        modalId = "new-snippet-modal",
        isClosed = props.isNewSnippetModalClosed,
        close = props.closeNewSnippetModal,
        actionText = "Are you sure you want to create a new snippet ?",
        actionLabel = "New",
        action = props.newSnippet
      ).render
    )
  }

  private val component =
    ScalaComponent
      .builder[NewButton]("NewButton")
      .render_P(render)
      .configure(Reusability.shouldComponentUpdate)
      .build
}


text:
#include "stdafx.h"
#include "r_table.h"
#include "tools.h"
#include "misc.h"

using namespace rd;
const char* table::class_name = "++table++";

SEXP table::get_fields()
{
  //if (!m_dataset->is_table())
  //  return error_Ret("arc.table");
  auto v = m_dataset->get_fields();
  return forward_from_keyvalue_variant(v);
}
#include <unordered_set>

#if 0
void test_realloc()
{
  SEXP sx = nullptr;
  __try
  {
  for (size_t i = 0, n = 0; i < 90000000; i++)
  {
    if (i == n)
    {
      auto new_n = n == 0 ? 8 : n + (n/2);
      Rprintf("allocating : %u\n" , (unsigned int)n);
      auto s = Rf_protect(Rf_allocVector(INTSXP, new_n));
      if (sx != nullptr)
      {
        std::memcpy(INTEGER(s), INTEGER(sx), sizeof(n)*sizeof(int));
        ::R_ReleaseObject(sx);
        //R_gc();
      }
      sx = s;
      ::R_PreserveObject(sx);
      Rf_unprotect(1);
      n = new_n;
    }
    INTEGER(sx)[i] = (int)i;
  }
  }
  __finally
  {
    ::R_ReleaseObject(sx);
    R_gc();
    Rf_error("out of memory");
  }
}
#endif
#if 0
SEXP table::select(SEXP fields, SEXP args)
{
  if (!m_dataset->is_table())
    return error_Ret("arc.table");

  SEXP selected = CAR(args);args = CDR(args);
  SEXP where_str = CAR(args);args = CDR(args);
  SEXP sr = CAR(args);args = CDR(args);

  std::vector<std::wstring> fld;
  if (!tools::copy_to(fields, fld))
    return error_Ret("'fields' should be string(s)");
  bool use_selection = true;
  tools::copy_to(selected, use_selection);

  if (fld.empty())
    return error_Ret("nothing to select");

  std::wstring where_clause;
  tools::copy_to(where_str, where_clause);

  const auto sp_ref = from_sr(sr);

  std::vector<std::wstring> unique;
#if 0
  for (const auto& it : fld)
    unique.push_back(tools::tolower(it));

  auto end = std::unique(unique.begin(), unique.end());
  auto new_size = std::distance(unique.begin(), end);
  unique.resize(new_size);
#else
  std::unordered_set<std::wstring> set;
  for (const auto& it : fld)
  {
    const auto str = tools::tolower(it);
    if (set.find(str) == set.end())
    {
      set.insert(str);
      unique.push_back(it);
    }
  }

#endif
  auto cols = m_dataset->select(unique, where_clause, use_selection, sp_ref);
  if (cols.empty())
    return showError<true>("COM error");

  tools::protect pt;
  std::vector<std::pair<SEXP, std::wstring>> unique_cols;
  for (auto &it : cols)
  {
    switch (it->t)
    {
    case arcobject::column_t::eInt:
    {
      auto &data = arcobject::column_t::get_ints(*it);
      unique_cols.emplace_back(pt.add(tools::newVal(data)), it->name);
    }break;
    case arcobject::column_t::eDate:
    {
      auto &data = arcobject::column_t::get_doubles(*it);
      for (auto &d : data)
        d = ole2epoch(d);

      auto column = pt.add(tools::newVal(data));
      const static std::vector<std::string> names{ "POSIXt", "POSIXct" };
      Rf_setAttrib(column, R_ClassSymbol, pt.add(tools::newVal(names)));
      //use local time zone
      //Rf_setAttrib(column, Rf_install("tzone"), tools::newVal("UTC"));
      unique_cols.emplace_back(column, it->name);
    }
    break;
    case arcobject::column_t::eDouble:
    {
      auto &data = arcobject::column_t::get_doubles(*it);
      unique_cols.emplace_back(pt.add(tools::newVal(data)), it->name);
    }break;
    case arcobject::column_t::eString:
    {
      auto &data = arcobject::column_t::get_strings(*it);
      unique_cols.emplace_back(pt.add(tools::newVal(data)), it->name);
    }break;
    case arcobject::column_t::eGeometry_point:
    {
      bool add_wkt = (!it->sr.first.empty() || it->sr.second != 0);
      auto& pts = arcobject::column_t::get_points(*it);
      tools::listGeneric xyzm((add_wkt ? 2 : 0) + 2 + (pts[2].empty() ? 0 : 1) + (pts[3].empty() ? 0 : 1));
      xyzm.push_back(pts[0], L"x"); pts[0].clear();
      xyzm.push_back(pts[1], L"y"); pts[1].clear();
      if (!pts[2].empty())
      {
        xyzm.push_back(pts[2], L"z");
        pts[2].clear();
      }
      if (!pts[3].empty())
      {
        xyzm.push_back(pts[3], L"m");
        pts[3].clear();
      }
      if (add_wkt)
      {
        xyzm.push_back(it->sr.first, L"WKT");
        xyzm.push_back(it->sr.second, L"WKID");
      }
      unique_cols.emplace_back(pt.add(xyzm.get()), it->name);
    } break;
    case arcobject::column_t::eGeometry_polyline:
    case arcobject::column_t::eGeometry_polygon:
    case arcobject::column_t::eGeometry_multipoint:
    {
      auto& geoms = arcobject::column_t::get_shapes(*it);
      bool add_wkt = (!it->sr.first.empty() || it->sr.second != 0);
      tools::listGeneric shapes(geoms.size() + add_wkt ? 2 : 0);
      std::wstring name;
      if (it->t == arcobject::column_t::eGeometry_polyline)
        name = L"Polyline";
      else if (it->t == arcobject::column_t::eGeometry_polygon)
        name = L"Polygon";
      else if (it->t == arcobject::column_t::eGeometry_multipoint)
        name = L"Multipoint";

      for (auto &g : geoms)
      {
        if (g.empty())
          shapes.push_back(R_NilValue, name);
        else
          shapes.push_back(g, name);
        g.clear();
      }
      if (add_wkt)
      {
        shapes.push_back(it->sr.first, L"WKT");
        shapes.push_back(it->sr.second, L"WKID");
      }
      unique_cols.emplace_back(pt.add(shapes.get()), it->name);
    }break;
    default:
      ATLASSERT(0);
      break;
    }
    it.reset(nullptr);
  }
  //use same reference for duplicated columns
  tools::listGeneric column_list(fld.size());
  ATLASSERT(unique_cols.size() == unique.size());
  if (unique_cols.size() == unique.size())
  {
    for(auto it = fld.begin(), end = fld.end(); it != end; ++it)
    {
      //auto idx = std::distance(unique.begin(), std::find(unique.begin(), unique.end(), tools::tolower(*it)));
      auto idx = std::distance(unique.begin(), std::find(unique.begin(), unique.end(), *it));
      column_list.push_back(unique_cols[idx].first, it->c_str());
    }
  }
  else
  {
    for(const auto &it : unique_cols)
      column_list.push_back(it.first, it.second.c_str());
  }
  return column_list.get();
}
#endif

std::pair<std::vector<SEXP>, std::vector<std::wstring>> process_cols(tools::protect &pt, std::vector<std::unique_ptr<arcobject::column_t2>> &cols)
{
  //std::vector<std::pair<SEXP, std::wstring>> unique_cols;
  std::pair<std::vector<SEXP>, std::vector<std::wstring>> unique_cols;
  for (auto &it : cols)
  {
    auto s = pt.add(tools::newVal(it->data.first));
    if (it->data.second.vt != VT_EMPTY)
      tools::nameIt(s, tools::newVal(it->data.second));
    if (it->attr.first.vt == (VT_VARIANT|VT_VECTOR) && it->attr.second.vt == (VT_LPSTR|VT_VECTOR))
    {
      ATLASSERT(it->attr.first.capropvar.cElems == it->attr.second.calpstr.cElems);
      for (ULONG i = 0; i < it->attr.first.capropvar.cElems; i++)
      {
        Rf_setAttrib(s, Rf_install(it->attr.second.calpstr.pElems[i]), tools::newVal(it->attr.first.capropvar.pElems[i]));
      }
    }
    if (it->t == arcobject::column_t2::ct::eDate)
    {
      Rf_setAttrib(s, R_ClassSymbol, tools::newVal({ "POSIXt", "POSIXct" }));
    }
    //unique_cols.emplace_back(s, it->name);
    unique_cols.first.emplace_back(s);
    unique_cols.second.emplace_back(std::move(it->name));
    it.reset(nullptr);
  }
  return unique_cols;
}

SEXP table::select2(SEXP fields, SEXP sargs)
{
  //if (!m_dataset->is_table())
  //  return error_Ret("arc.table");

  std::vector<std::wstring> fld;
  if (!tools::copy_to(fields, fld))
    return error_Ret("'fields' should be string(s)");

  if (fld.empty())
    return error_Ret("nothing to select");

  bool use_selection = true;
  std::wstring where_clause;
  arcobject::sr_type sp_ref(std::wstring(), 0);
  bool asWKB = false;
  bool asJson = false;
  bool asJsonSR = false;
  bool dencify = true;
  std::wstring transformation;

  const static std::unordered_set<std::string_view> valid_args{
    "selected",
    "where_clause",
    "sr",
    "asWKB",
    "asJson",
    "asJsonSR",
    "dencify",
    "transformation"
  };
  const auto args = tools::pairlist2args_map(sargs);
  const auto is_args = tools::unpack_args<false>(args,
    std::tie("selected", use_selection),
    std::tie("where_clause", where_clause),
    std::tie("sr", sp_ref),
    std::tie("asWKB", asWKB),
    std::tie("asJson", asJson),
    std::tie("asJsonSR", asJsonSR),
    std::tie("dencify", dencify),
    std::tie("transformation", transformation));

  if (std::get<0>(is_args) == 2)
    return error_Ret("incorrect type, argument: selected");
  if (std::get<1>(is_args) == 2)
    return error_Ret("incorrect type, argument: where_clause");
  if (std::get<2>(is_args) == 2)
    return error_Ret("'asWKB' argument is not logical");
  if (std::get<3>(is_args) == 2)
    return error_Ret("'dencify' argument is not logical");
  if (std::get<7>(is_args) == 2)
    return error_Ret("incorrect type, argument: transformation");

  //validate unknown arguments
  for (const auto& kv : args)
  {
    if (valid_args.find(kv.first) == valid_args.end())
      Rf_warning("ignore unknown argument: '%s'", kv.first.c_str());
  }
  /*
  auto it = args.find("selected");
  if (it != args.end())
    tools::copy_to(it->second, use_selection);
  it = args.find("where_clause");
  if (it != args.end())
    tools::copy_to(it->second, where_clause);

  it = args.find("sr");
  if (it != args.end())
    sp_ref = from_sr(it->second);
  else
    sp_ref.second = 0;
  it = args.find("asWKB");
  if (it != args.end() && tools::copy_to(it->second, asWKB) == false)
    return error_Ret("'asWKB' argument is not logical");

  it = args.find("dencify");
  if (it != args.end() && tools::copy_to(it->second, dencify) == false)
    return error_Ret("'dencify' argument is not logical");
  */
  std::vector<std::wstring> unique;
  std::unordered_set<std::wstring> set;
  for (const auto &it : fld)
  {
    const auto str = tools::tolower(it);
    if (set.find(str) == set.end())
    {
      set.insert(str);
      unique.push_back(it);
    }
  }

  arcobject::dataset::geometry_kind kind = arcobject::dataset::geometry_kind::eShapeBuffer;
  if (asWKB) kind = arcobject::dataset::geometry_kind::eWkb;
  else if (asJson) kind = arcobject::dataset::geometry_kind::eJson;
  else if (asJsonSR) kind = arcobject::dataset::geometry_kind::eJsonSR;

  auto cols = m_dataset->select(unique, where_clause, use_selection, sp_ref, dencify, kind, transformation);

  if (cols.empty())
    return showError<true>("COM error");

  tools::protect pt;
  auto unique_cols = process_cols(pt, cols);
  //use same reference for duplicated columns
  //tools::listGeneric column_list(fld.size());
  ATLASSERT(unique_cols.first.size() == unique.size());
  /*if (unique_cols.size() == unique.size())
  {
    for(auto it = fld.begin(), end = fld.end(); it != end; ++it)
    {
      //auto idx = std::distance(unique.begin(), std::find(unique.begin(), unique.end(), tools::tolower(*it)));
      auto idx = std::distance(unique.begin(), std::find(unique.begin(), unique.end(), *it));
      column_list.push_back(unique_cols[idx].first, it->c_str());
    }
  }
  else
  {
    for(const auto &it : unique_cols)
      column_list.push_back(it.first, it.second.c_str());
  }
  return column_list.get();*/
  auto ret = pt.add(tools::newVal(unique_cols.first));
  return tools::nameIt(ret, unique_cols.second);
}


text:
from parlai.utils.testing import AutoTeacherTest

class TestDefaultTeacher(AutoTeacherTest):
    task = "personachat"

class TestNoneTeacher(AutoTeacherTest):
    task = "personachat:none"

class TestSelfOriginalTeacher(AutoTeacherTest):
    task = "personachat:self_original"

class TestSelfTeacher(AutoTeacherTest):
    task = "personachat:self"

class TestSelfRevisedTeacher(AutoTeacherTest):
    task = "personachat:self_revised"

class TestOtherOriginalTeacher(AutoTeacherTest):
    task = "personachat:other_original"

class TestOtherTeacher(AutoTeacherTest):
    task = "personachat:other"

class TestOtherRevisedTeacher(AutoTeacherTest):
    task = "personachat:other_revised"

class TestBothOriginalTeacher(AutoTeacherTest):
    task = "personachat:both_original"

class TestBothTeacher(AutoTeacherTest):
    task = "personachat:both"

class TestBothRevisedTeacher(AutoTeacherTest):
    task = "personachat:both_revised"


text:
package api

import (
	"io"
	"net/url"
	"path/filepath"
	"strconv"
	"testing"
	"time"

	"github.com/NebulousLabs/Sia/crypto"
	"github.com/NebulousLabs/Sia/modules"
	"github.com/NebulousLabs/Sia/modules/host/storagemanager"
)

var (
	// Various folder sizes for testing host storage folder resizing.
	// Must be provided as strings to the API call.
	minFolderSizeString    = strconv.FormatUint(storagemanager.MinimumStorageFolderSize(), 10)
	maxFolderSizeString    = strconv.FormatUint(storagemanager.MaximumStorageFolderSize(), 10)
	tooSmallFolderString   = strconv.FormatUint(storagemanager.MinimumStorageFolderSize()-1, 10)
	tooLargeFolderString   = strconv.FormatUint(storagemanager.MaximumStorageFolderSize()+1, 10)
	mediumSizeFolderString = strconv.FormatUint(3*storagemanager.MinimumStorageFolderSize(), 10)

	// Test cases for resizing a host's storage folder.
	// Running all the invalid cases before the valid ones simplifies some
	// logic in the tests that use resizeTests.
	resizeTests = []struct {
		sizeString string
		size       uint64
		err        error
	}{
		// invalid sizes
		{"", 0, io.EOF},
		{"0", 0, storagemanager.ErrSmallStorageFolder},
		{tooSmallFolderString, storagemanager.MinimumStorageFolderSize() - 1, storagemanager.ErrSmallStorageFolder},
		{tooLargeFolderString, storagemanager.MaximumStorageFolderSize() + 1, storagemanager.ErrLargeStorageFolder},

		// valid sizes
		{minFolderSizeString, storagemanager.MinimumStorageFolderSize(), nil},
		{maxFolderSizeString, storagemanager.MaximumStorageFolderSize(), nil},
		{mediumSizeFolderString, 3 * storagemanager.MinimumStorageFolderSize(), nil},
	}
)

// TestStorageHandler tests that host storage is being reported correctly.
func TestStorageHandler(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	st, err := createServerTester("TestStorageHandler")
	if err != nil {
		t.Fatal(err)
	}
	defer st.server.Close()

	// Announce the host and start accepting contracts.
	if err := st.announceHost(); err != nil {
		t.Fatal(err)
	}
	if err := st.acceptContracts(); err != nil {
		t.Fatal(err)
	}
	if err := st.setHostStorage(); err != nil {
		t.Fatal(err)
	}

	// Set an allowance for the renter, allowing a contract to be formed.
	allowanceValues := url.Values{}
	allowanceValues.Set("funds", testFunds)
	allowanceValues.Set("period", testPeriod)
	if err = st.stdPostAPI("/renter", allowanceValues); err != nil {
		t.Fatal(err)
	}

	// Create a file.
	path := filepath.Join(st.dir, "test.dat")
	fileBytes := 1024
	if err := createRandFile(path, fileBytes); err != nil {
		t.Fatal(err)
	}

	// Upload to host.
	uploadValues := url.Values{}
	uploadValues.Set("source", path)
	if err := st.stdPostAPI("/renter/upload/test", uploadValues); err != nil {
		t.Fatal(err)
	}

	// Only one piece will be uploaded (10% at current redundancy)
	var rf RenterFiles
	for i := 0; i < 200 && (len(rf.Files) != 1 || rf.Files[0].UploadProgress < 10); i++ {
		st.getAPI("/renter/files", &rf)
		time.Sleep(50 * time.Millisecond)
	}
	if len(rf.Files) != 1 || rf.Files[0].UploadProgress < 10 {
		t.Error(rf.Files[0].UploadProgress)
		t.Fatal("uploading has failed")
	}

	var sg StorageGET
	if err := st.getAPI("/host/storage", &sg); err != nil {
		t.Fatal(err)
	}

	// Uploading succeeded, so /host/storage should be reporting a successful
	// write.
	if sg.Folders[0].SuccessfulWrites != 1 {
		t.Fatalf("expected 1 successful write, got %v", sg.Folders[0].SuccessfulWrites)
	}
	if used := sg.Folders[0].Capacity - sg.Folders[0].CapacityRemaining; used != modules.SectorSize {
		t.Fatalf("expected used capacity to be the size of one sector (%v bytes), got %v bytes", modules.SectorSize, used)
	}
}

// TestAddFolderNoPath tests that an API call to add a storage folder fails if
// no path was provided.
func TestAddFolderNoPath(t *testing.T) {
	if testing.Short() {
		t.Skip()
	}
	t.Parallel()
	st, err := createServerTester("TestAddFolderNoPath")
	if err != nil {
		t.Fatal(err)
	}
	defer st.server.Close()

	// Try adding a storage folder without setting "path" in the API call.
	addValues := url.Values{}
	addValues.Set("size", mediumSizeFolderString)
	err = st.stdPostAPI("/host/storage/folders/add", addValues)
	if err == nil || err.Error() != storagemanager.ErrEmptyPath.Error() {
		t.Fatalf("expected error to be %v; got %v", storagemanager.ErrEmptyPath, err)
	}

	// Setting the path to an empty string should trigger the same error.
	addValues.Set("path", "")
	err = st.stdPostAPI("/host/storage/folders/add", addValues)
	if err == nil || err.Error() != storagemanager.ErrEmptyPath.Error() {
		t.Fatalf("expected error to be %v; got %v", storagemanager.ErrEmptyPath, err)
	}
}

// TestAddFolderNoSize tests that an API call to add a storage folder fails if
// no path was provided.
func TestAddFolderNoSize(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	st, err := createServerTester("TestAddFolderNoSize")
	if err != nil {
		t.Fatal(err)
	}
	defer st.server.Close()

	// Try adding a storage folder without setting "size" in the API call.
	addValues := url.Values{}
	addValues.Set("path", st.dir)
	err = st.stdPostAPI("/host/storage/folders/add", addValues)
	if err == nil || err.Error() != io.EOF.Error() {
		t.Fatalf("expected error to be %v, got %v", io.EOF, err)
	}
}

// TestAddSameFolderTwice tests that an API call that attempts to add a
// host storage folder that's already been added is handled gracefully.
func TestAddSameFolderTwice(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	st, err := createServerTester("TestAddSameFolderTwice")
	if err != nil {
		t.Fatal(err)
	}
	defer st.server.Close()

	// Make the call to add a storage folder twice.
	addValues := url.Values{}
	addValues.Set("path", st.dir)
	addValues.Set("size", mediumSizeFolderString)
	err = st.stdPostAPI("/host/storage/folders/add", addValues)
	if err != nil {
		t.Fatal(err)
	}
	err = st.stdPostAPI("/host/storage/folders/add", addValues)
	if err == nil || err.Error() != storagemanager.ErrRepeatFolder.Error() {
		t.Fatalf("expected err to be %v, got %v", err, storagemanager.ErrRepeatFolder)
	}
}

// TestResizeEmptyStorageFolder tests that invalid and valid calls to resize
// an empty storage folder are properly handled.
func TestResizeEmptyStorageFolder(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	st, err := createServerTester("TestResizeEmptyStorageFolder")
	if err != nil {
		t.Fatal(err)
	}
	defer st.server.Close()

	// Announce the host and start accepting contracts.
	if err := st.announceHost(); err != nil {
		t.Fatal(err)
	}
	if err := st.acceptContracts(); err != nil {
		t.Fatal(err)
	}
	if err := st.setHostStorage(); err != nil {
		t.Fatal(err)
	}

	// Find out how large the host's initial storage folder is.
	var sg StorageGET
	if err := st.getAPI("/host/storage", &sg); err != nil {
		t.Fatal(err)
	}
	defaultSize := sg.Folders[0].Capacity
	// Convert defaultSize (uint64) to a string for the API call.
	defaultSizeString := strconv.FormatUint(defaultSize, 10)

	resizeValues := url.Values{}
	resizeValues.Set("path", st.dir)
	resizeValues.Set("newsize", defaultSizeString)

	// Attempting to resize to the same size should return an error.
	err = st.stdPostAPI("/host/storage/folders/resize", resizeValues)
	if err == nil || err.Error() != storagemanager.ErrNoResize.Error() {
		t.Fatalf("expected error %v, got %v", storagemanager.ErrNoResize, err)
	}

	// Try resizing to a bunch of sizes (invalid ones first, valid ones second).
	// This ordering simplifies logic within the for loop.
	for _, test := range resizeTests {
		// Attempt to resize the host's storage folder.
		resizeValues.Set("newsize", test.sizeString)
		err = st.stdPostAPI("/host/storage/folders/resize", resizeValues)
		if (err == nil && test.err != nil) || (err != nil && err.Error() != test.err.Error()) {
			t.Fatalf("expected error to be %v, got %v", test.err, err)
		}

		// Find out if the resize call worked as expected.
		if err := st.getAPI("/host/storage", &sg); err != nil {
			t.Fatal(err)
		}
		// If the test size is valid, check that the folder has been resized
		// properly.
		if test.err == nil {
			// Check that the folder's total capacity has been updated.
			if got := sg.Folders[0].Capacity; got != test.size {
				t.Fatalf("expected folder to be resized to %v; got %v instead", test.size, got)
			}
			// Check that the folder's remaining capacity has been updated.
			if got := sg.Folders[0].CapacityRemaining; got != test.size {
				t.Fatalf("folder should be empty, but capacity remaining (%v) != total capacity (%v)", got, test.size)
			}
		} else {
			// If the test size is invalid, the folder should not have been
			// resized. The invalid test cases are all run before the valid ones,
			// so the folder size should still be defaultSize.
			if got := sg.Folders[0].Capacity; got != defaultSize {
				t.Fatalf("folder was resized to an invalid size (%v) in a test case that should have failed: %v", got, test)
			}
		}
	}
}

// TestResizeNonemptyStorageFolder tests that invalid and valid calls to resize
// a storage folder with one sector filled are properly handled.
// Ideally, we would also test a very full storage folder (including the case
// where the host tries to resize to a size smaller than the amount of data
// in the folder), but that would be a very expensive test.
func TestResizeNonemptyStorageFolder(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	st, err := createServerTester("TestResizeNonemptyStorageFolder")
	if err != nil {
		t.Fatal(err)
	}
	defer st.server.Close()

	// Announce the host and start accepting contracts.
	if err := st.announceHost(); err != nil {
		t.Fatal(err)
	}
	if err := st.acceptContracts(); err != nil {
		t.Fatal(err)
	}
	if err := st.setHostStorage(); err != nil {
		t.Fatal(err)
	}

	// Set an allowance for the renter, allowing a contract to be formed.
	allowanceValues := url.Values{}
	allowanceValues.Set("funds", testFunds)
	allowanceValues.Set("period", testPeriod)
	if err = st.stdPostAPI("/renter", allowanceValues); err != nil {
		t.Fatal(err)
	}

	// Create a file.
	path := filepath.Join(st.dir, "test.dat")
	fileBytes := 1024
	if err := createRandFile(path, fileBytes); err != nil {
		t.Fatal(err)
	}

	// Upload to host.
	uploadValues := url.Values{}
	uploadValues.Set("source", path)
	if err := st.stdPostAPI("/renter/upload/test", uploadValues); err != nil {
		t.Fatal(err)
	}

	// Only one piece will be uploaded (10% at current redundancy)
	var rf RenterFiles
	for i := 0; i < 200 && (len(rf.Files) != 1 || rf.Files[0].UploadProgress < 10); i++ {
		st.getAPI("/renter/files", &rf)
		time.Sleep(50 * time.Millisecond)
	}
	if len(rf.Files) != 1 || rf.Files[0].UploadProgress < 10 {
		t.Error(rf.Files[0].UploadProgress)
		t.Fatal("uploading has failed")
	}

	// Find out how large the host's initial storage folder is.
	var sg StorageGET
	if err := st.getAPI("/host/storage", &sg); err != nil {
		t.Fatal(err)
	}
	defaultSize := sg.Folders[0].Capacity
	// Convert defaultSize (uint64) to a string for the API call.
	defaultSizeString := strconv.FormatUint(defaultSize, 10)

	resizeValues := url.Values{}
	resizeValues.Set("path", st.dir)
	resizeValues.Set("newsize", defaultSizeString)

	// Attempting to resize to the same size should return an error.
	err = st.stdPostAPI("/host/storage/folders/resize", resizeValues)
	if err == nil || err.Error() != storagemanager.ErrNoResize.Error() {
		t.Fatalf("expected error %v, got %v", storagemanager.ErrNoResize, err)
	}

	// Try resizing to a bunch of sizes (invalid ones first, valid ones second).
	// This ordering simplifies logic within the for loop.
	for _, test := range resizeTests {
		// Attempt to resize the host's storage folder.
		resizeValues.Set("newsize", test.sizeString)
		err = st.stdPostAPI("/host/storage/folders/resize", resizeValues)
		if (err == nil && test.err != nil) || (err != nil && err.Error() != test.err.Error()) {
			t.Fatalf("expected error to be %v, got %v", test.err, err)
		}

		// Find out if the resize call worked as expected.
		if err := st.getAPI("/host/storage", &sg); err != nil {
			t.Fatal(err)
		}
		// If the test size is valid, check that the folder has been resized
		// properly.
		if test.err == nil {
			// Check that the folder's total capacity has been updated.
			if sg.Folders[0].Capacity != test.size {
				t.Fatalf("expected folder to be resized to %v; got %v instead", test.size, sg.Folders[0].Capacity)
			}
			// Since one sector has been uploaded, the available capacity
			// should be one sector size smaller than the total capacity.
			if used := test.size - sg.Folders[0].CapacityRemaining; used != modules.SectorSize {
				t.Fatalf("used capacity (%v) != the size of 1 sector (%v)", used, modules.SectorSize)
			}
		} else {
			// If the test size is invalid, the folder should not have been
			// resized. The invalid test cases are all run before the valid
			// ones, so the folder size should still be defaultSize.
			if got := sg.Folders[0].Capacity; got != defaultSize {
				t.Fatalf("folder was resized to an invalid size (%v) in a test case that should have failed: %v", got, test)
			}
		}
	}
}

// TestResizeNonexistentFolder checks that an API call to resize a nonexistent
// folder triggers the appropriate error.
func TestResizeNonexistentFolder(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	st, err := createServerTester("TestResizeNonexistentFolder")
	if err != nil {
		t.Fatal(err)
	}
	defer st.server.Close()

	// No folder has been created yet at st.dir, so using it as the path for
	// the resize call should trigger an error.
	resizeValues := url.Values{}
	resizeValues.Set("path", st.dir)
	resizeValues.Set("newsize", mediumSizeFolderString)
	err = st.stdPostAPI("/host/storage/folders/resize", resizeValues)
	if err == nil || err.Error() != errStorageFolderNotFound.Error() {
		t.Fatalf("expected error to be %v, got %v", errStorageFolderNotFound, err)
	}
}

// TestResizeFolderNoPath checks that an API call to resize a storage folder fails
// if no path was provided.
func TestResizeFolderNoPath(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	st, err := createServerTester("TestResizeFolderNoPath")
	if err != nil {
		t.Fatal(err)
	}
	defer st.server.Close()

	// The call to resize should fail if no path has been provided.
	resizeValues := url.Values{}
	resizeValues.Set("newsize", mediumSizeFolderString)
	err = st.stdPostAPI("/host/storage/folders/resize", resizeValues)
	if err == nil || err.Error() != errNoPath.Error() {
		t.Fatalf("expected error to be %v; got %v", errNoPath, err)
	}
}

// TestRemoveEmptyStorageFolder checks that removing an empty storage folder
// succeeds -- even if the host is left with zero storage folders.
func TestRemoveEmptyStorageFolder(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	st, err := createServerTester("TestRemoveEmptyStorageFolder")
	if err != nil {
		t.Fatal(err)
	}
	defer st.server.Close()

	// Set up a storage folder for the host.
	if err := st.setHostStorage(); err != nil {
		t.Fatal(err)
	}

	// Try to delete the host's empty storage folder.
	removeValues := url.Values{}
	removeValues.Set("path", st.dir)
	if err = st.stdPostAPI("/host/storage/folders/remove", removeValues); err != nil {
		t.Fatal(err)
	}
}

// TestRemoveStorageFolderError checks that invalid calls to
// /host/storage/folders/remove fail with the appropriate error.
func TestRemoveStorageFolderError(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	st, err := createServerTester("TestRemoveStorageFolderErr")
	if err != nil {
		t.Fatal(err)
	}
	defer st.server.Close()

	// Set up a storage folder for the host.
	if err := st.setHostStorage(); err != nil {
		t.Fatal(err)
	}

	// Try removing a nonexistent folder.
	removeValues := url.Values{}
	removeValues.Set("path", "/foo/bar")
	err = st.stdPostAPI("/host/storage/folders/remove", removeValues)
	if err == nil || err.Error() != errStorageFolderNotFound.Error() {
		t.Fatalf("expected error %v, got %v", errStorageFolderNotFound, err)
	}

	// The folder path can't be an empty string.
	removeValues.Set("path", "")
	err = st.stdPostAPI("/host/storage/folders/remove", removeValues)
	if err == nil || err.Error() != errNoPath.Error() {
		t.Fatalf("expected error to be %v; got %v", errNoPath, err)
	}
}

// TestRemoveStorageFolderForced checks that if a call to remove a storage
// folder will result in data loss, that call succeeds if and only if "force"
// has been set to "true".
func TestRemoveStorageFolderForced(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	st, err := createServerTester("TestRemoveStorageFolderForced")
	if err != nil {
		t.Fatal(err)
	}
	defer st.server.Close()

	// Announce the host.
	if err := st.announceHost(); err != nil {
		t.Fatal(err)
	}
	if err := st.acceptContracts(); err != nil {
		t.Fatal(err)
	}
	if err := st.setHostStorage(); err != nil {
		t.Fatal(err)
	}

	// Set an allowance for the renter, allowing a contract to be formed.
	allowanceValues := url.Values{}
	allowanceValues.Set("funds", testFunds)
	allowanceValues.Set("period", testPeriod)
	if err = st.stdPostAPI("/renter", allowanceValues); err != nil {
		t.Fatal(err)
	}

	// Create a file for upload.
	path := filepath.Join(st.dir, "test.dat")
	if err := createRandFile(path, 512); err != nil {
		t.Fatal(err)
	}
	// Upload to host.
	uploadValues := url.Values{}
	uploadValues.Set("source", path)
	if err := st.stdPostAPI("/renter/upload/test", uploadValues); err != nil {
		t.Fatal(err)
	}

	// Only one piece will be uploaded (10%  at current redundancy)
	var rf RenterFiles
	for i := 0; i < 200 && (len(rf.Files) != 1 || rf.Files[0].UploadProgress < 10); i++ {
		st.getAPI("/renter/files", &rf)
		time.Sleep(50 * time.Millisecond)
	}
	if len(rf.Files) != 1 || rf.Files[0].UploadProgress < 10 {
		t.Error(rf.Files[0].UploadProgress)
		t.Fatal("uploading has failed")
	}

	// The host should not be able to remove its only folder without setting
	// "force" to "true", since this will result in data loss (there are no
	// other folders for the data to be redistributed to).
	removeValues := url.Values{}
	removeValues.Set("path", st.dir)
	err = st.stdPostAPI("/host/storage/folders/remove", removeValues)
	if err == nil || err.Error() != storagemanager.ErrIncompleteOffload.Error() {
		t.Fatalf("expected err to be %v; got %v", storagemanager.ErrIncompleteOffload, err)
	}
	// Forced removal of the folder should succeed, though.
	removeValues.Set("force", "true")
	err = st.stdPostAPI("/host/storage/folders/remove", removeValues)
	if err != nil {
		t.Fatal(err)
	}
}

// TestDeleteSector tests the call to delete a storage sector from the host.
func TestDeleteSector(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	st, err := createServerTester("TestDeleteSector")
	if err != nil {
		t.Fatal(err)
	}
	defer st.server.Close()

	// Set up the host for forming contracts.
	if err := st.announceHost(); err != nil {
		t.Fatal(err)
	}
	if err := st.acceptContracts(); err != nil {
		t.Fatal(err)
	}
	if err := st.setHostStorage(); err != nil {
		t.Fatal(err)
	}

	// Set an allowance for the renter, allowing contracts to formed.
	allowanceValues := url.Values{}
	allowanceValues.Set("funds", testFunds)
	allowanceValues.Set("period", testPeriod)
	if err = st.stdPostAPI("/renter", allowanceValues); err != nil {
		t.Fatal(err)
	}

	// Create a file.
	path := filepath.Join(st.dir, "test.dat")
	if err := createRandFile(path, 1024); err != nil {
		t.Fatal(err)
	}

	// Upload to host.
	uploadValues := url.Values{}
	uploadValues.Set("source", path)
	if err = st.stdPostAPI("/renter/upload/test", uploadValues); err != nil {
		t.Fatal(err)
	}

	// Only one piece will be uploaded (10%  at current redundancy)
	var rf RenterFiles
	for i := 0; i < 200 && (len(rf.Files) != 1 || rf.Files[0].UploadProgress < 10); i++ {
		st.getAPI("/renter/files", &rf)
		time.Sleep(50 * time.Millisecond)
	}
	if len(rf.Files) != 1 || rf.Files[0].UploadProgress < 10 {
		t.Error(rf.Files[0].UploadProgress)
		t.Fatal("uploading has failed")
	}

	// Get the Merkle root of the piece that was uploaded.
	contracts := st.renter.Contracts()
	if len(contracts) != 1 {
		t.Fatalf("expected exactly 1 contract to have been formed; got %v instead", len(contracts))
	}
	sectorRoot := contracts[0].MerkleRoots[0].String()

	if err = st.stdPostAPI("/host/storage/sectors/delete/"+sectorRoot, url.Values{}); err != nil {
		t.Fatal(err)
	}
}

// TestDeleteNonexistentSector checks that attempting to delete a storage
// sector that doesn't exist will fail with the appropriate error.
func TestDeleteNonexistentSector(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	t.Parallel()
	st, err := createServerTester("TestDeleteNonexistentSector")
	if err != nil {
		t.Fatal(err)
	}
	defer st.server.Close()

	// These calls to delete imaginary sectors should fail for a few reasons:
	// - the given sector root strings are invalid
	// - the renter hasn't uploaded anything
	// - the host has no storage folders yet
	// Right now, the calls fail for the first reason. This test will report if that behavior changes.
	badHash := crypto.HashObject("fake object").String()
	err = st.stdPostAPI("/host/storage/sectors/delete/"+badHash, url.Values{})
	if err == nil || err.Error() != storagemanager.ErrSectorNotFound.Error() {
		t.Fatalf("expected error to be %v; got %v", storagemanager.ErrSectorNotFound, err)
	}
	wrongSize := "wrong size string"
	err = st.stdPostAPI("/host/storage/sectors/delete/"+wrongSize, url.Values{})
	if err == nil || err.Error() != crypto.ErrHashWrongLen.Error() {
		t.Fatalf("expected error to be %v; got %v", crypto.ErrHashWrongLen, err)
	}
}


text:
import copy
import uuid

__flatnode_items = lambda: (
    Field('f_uuid', comment='Unique identifer', default=lambda: str(uuid.uuid4()), length=64, notnull=True, unique=True),
)

__treenode_items = lambda: (
    Field('f_parent_uuid', comment='Identifer of parent', length=64, notnull=False, unique=False),
    Field('f_uuid', comment='Unique identifer', default=lambda: str(uuid.uuid4()), length=64, notnull=True, unique=True)
)

db.define_table(
    't_mtuser',
    Field('f_username', comment='Username'),
    Field('f_auth_user', 'reference auth_user', comment='Mapping to web2py user', requires=IS_EMPTY_OR(IS_IN_DB(db, 'auth_user.id', '%(username)s'))),
    *__flatnode_items()
)

db.define_table(
    't_mtalbum',
    Field('f_name', comment='Name of album'),
    Field('f_owner', 'reference t_mtuser', comment='Owning user', requires=IS_IN_DB(db, 't_mtuser.id', '%(f_username)s', zero=None)),
    *__treenode_items()
)

db.define_table(
    't_mtitemtype',
    Field('f_name', comment='Name of type'),
    Field('f_key', comment='Type key, e.g. yt', unique=True)
)

db.define_table(
    't_mtitem',
    Field('f_name', comment='Name of item'),
    Field('f_album', 'reference t_mtalbum', comment='Album containing this item', requires=IS_IN_DB(db, 't_mtalbum.id', '%(f_name)s', zero=None)),
    Field('f_alien_etag', 'string', comment='Etag to manage refetch of data', default=None, length=64),
    Field('f_alien_key', 'string', comment='Identifier within the source, e.g. bwgCRdwWGzE for YouTube', length=256),
    Field('f_alien_last_fetch_timestamp', 'datetime', comment='Time that parameteres were last fetched from the alien', default=None),
    Field('f_alien_params', 'json', comment='Parameters from the alien service, e.g. YouTube', default='{}', notnull=True),
    Field('f_itemtype', 'reference t_mtitemtype', comment='Item type', requires=IS_IN_DB(db, 't_mtitemtype.id', '%(f_name)s (%(f_key)s)', zero=None)),
    *__treenode_items()
)

db.define_table(
    't_mtdataset',
    Field('f_creator', 'reference auth_user', comment='User that created this item, if any', default=lambda: auth.user and auth.user.id),
    Field('f_data', 'json', comment='Data in JSON format', default={}, notnull=True, requires=IS_JSON()),
    Field('f_item', 'reference t_mtitem', comment='Item this dataset relates to', notnull=True, requires=IS_IN_DB(db, 't_mtitem.id', '%(f_name)s (%(id)d)', zero=None)),
    Field('f_owner', 'reference auth_group', comment='Group that owns this item', default=lambda: auth.user and auth.user_group()),
    Field('f_param', 'json', comment='Parameters in JSON format', default={}, notnull=True, requires=IS_JSON()),
    Field('f_session', 'json', comment='Session parameters in JSON format', default={}, notnull=True, requires=IS_JSON()),
    Field('f_session_id', comment='Session that owns this item, if no group is present', default=lambda: response.session_id)
)

db.define_table(
    't_mtsearch',
    Field('f_query', comment='Query', label='YouTube URL'),
    Field('f_user', 'reference t_mtuser', comment='Owning user', requires=IS_IN_DB(db, 't_mtuser.id', '%(f_username)s', zero=None), writable=False)
)


text:
namespace CornerFlag.Data
{
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    using CornerFlag.Data.Models;
    using CornerFlag.Data.Models.Entities;
    using CornerFlag.Data.Models.People;

    public interface ICornerFlagDbContext
    {
        IDbSet<ApplicationUser> Users { get; set; }

        IDbSet<Country> Countries { get; }

        IDbSet<Stadium> Stadiums { get; }

        IDbSet<Match> Games { get; }

        IDbSet<Player> Players { get; }

        IDbSet<Team> Teams { get; }

        IDbSet<Club> Clubs { get; }

        IDbSet<Competition> Competitions { get; }

        IDbSet<Round> Rounds { get; }

        IDbSet<Season> Seasons { get; }

        DbContext DbContext { get; }

        int SaveChanges();

        void Dispose();

        DbEntityEntry<TEntity> Entry<TEntity>(TEntity entity) where TEntity : class;

        IDbSet<T> Set<T>() where T : class;
    }
}


text:
<map>
  <entry>
    <string>plugin_status</string>
    <string>deprecated</string>
  </entry>
  <entry>
    <string>plugin_identifier</string>
    <string>edu.nyu.plugin.entelequia.EntelequiaPlugin</string>
  </entry>
  <entry>
    <string>plugin_version</string>
    <string>2</string>
  </entry>
  <entry>
    <string>au_def_pause_time</string>
    <long>6000</long>
  </entry>
  <entry>
    <string>au_name</string>
    <string>"Entelequia Plugin, Base URL %s, Volume %d", base_url, volume</string>
  </entry>
  <entry>
    <string>au_def_new_content_crawl</string>
    <long>1209600000</long>
  </entry>
  <entry>
    <string>plugin_name</string>
    <string>Entelequia Plugin</string>
  </entry>
  <entry>
    <string>au_crawlrules</string>
    <list>
      <string>4,"^%s", base_url</string>
      <string>1,"%slockss/manifest%.03d\.html", base_url, volume</string>
      <string>1,"%se(n|s)\.ant\.php\?a=%d", base_url, volume</string>
      <string>1,"%spdf/e.*\.pdf", base_url</string>
      <string>1,"%se(n|s)\.art.php\?a=%.02d", base_url, volume</string>
      <string>1,"%se(n|s)\.(boards|contact|editorial|call|faq)\.php", base_url</string>
      <string>1,"%sxml/dc/%.02d", base_url, volume</string>
      <string>1,".*\.([cC][sS][sS]|[gG][iI][fF]|[jJ][pP][eE][gG]|[jJ][pP][gG]|[jJ][sS]|[pP][nN][gG]|[mM][pP]3)$"</string>
    </list>
  </entry>
  <entry>
    <string>au_start_url</string>
    <string>"%slockss/manifest%.03d.html", base_url, volume</string>
  </entry>
  <entry>
    <string>plugin_config_props</string>
    <list>
      <org.lockss.daemon.ConfigParamDescr>
        <key>volume</key>
        <displayName>Volume No.</displayName>
        <type>6</type>
        <size>8</size>
        <definitional>true</definitional>
        <defaultOnly>false</defaultOnly>
      </org.lockss.daemon.ConfigParamDescr>
      <org.lockss.daemon.ConfigParamDescr>
        <key>base_url</key>
        <displayName>Base URL</displayName>
        <description>Usually of the form http://<journal-name>.com/</description>
        <type>3</type>
        <size>40</size>
        <definitional>true</definitional>
        <defaultOnly>false</defaultOnly>
      </org.lockss.daemon.ConfigParamDescr>
    </list>
  </entry>
</map>


text:
package com.cloudlbs.core.utils.system;

import java.util.Properties;

/**
 * Provides access to the centrally managed system properties.
 *
 * @author Dan Mascenik
 *
 */
public interface SystemPropertyRemoteService {

	public Properties findByCategory(String category);

	public String getSystemProperty(String key);

}


text:
import helper

helper.ask("what's up?")


text:
//
// finite impulse response (FIR) filter using fast Fourier transforms (FFTs)
//

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// fftfilt object structure
struct FFTFILT(_s) {
    TC *         h;     // filter coefficients array [size; h_len x 1]
    unsigned int h_len; // filter length
    unsigned int n;     // input/output block size

    // internal memory arrays
    // TODO: make TI/TO type, but ensuring complex
    // TODO: use special format for fftfilt_rrrf type
    float complex * time_buf;   // time buffer [size: 2*n x 1]
    float complex * freq_buf;   // freq buffer [size: 2*n x 1]
    float complex * H;          // FFT of filter coefficients [size: 2*n x 1]
    float complex * w;          // overlap array [size: n x 1]

    // FFT objects
    FFT_PLAN fft;       // FFT object (forward)
    FFT_PLAN ifft;      // FFT object (inverse)
    TC scale;           // output scaling factor
};

// create FFT-based FIR filter using external coefficients
//  _h      : filter coefficients [size: _h_len x 1]
//  _h_len  : filter length, _h_len > 0
//  _n      : block size = nfft/2, at least _h_len-1
FFTFILT() FFTFILT(_create)(TC *         _h,
                           unsigned int _h_len,
                           unsigned int _n)
{
    // validate input
    if (_h_len == 0)
        return liquid_error_config("fftfilt_%s_create(), filter length must be greater than zero",EXTENSION_FULL);
    if (_n < _h_len-1)
        return liquid_error_config("fftfilt_%s_create(), block length must be greater than _h_len-1 (%u)",EXTENSION_FULL,_h_len-1);

    // create filter object and initialize
    FFTFILT() q = (FFTFILT()) malloc(sizeof(struct FFTFILT(_s)));
    q->h_len    = _h_len;
    q->n        = _n;

    // copy filter coefficients
    q->h = (TC *) malloc((q->h_len)*sizeof(TC));
    memmove(q->h, _h, _h_len*sizeof(TC));

    // allocate internal memory arrays
    q->time_buf = (float complex *) FFT_MALLOC((2*q->n)* sizeof(float complex)); // time buffer
    q->freq_buf = (float complex *) FFT_MALLOC((2*q->n)* sizeof(float complex)); // frequency buffer
    q->H        = (float complex *) malloc((2*q->n)* sizeof(float complex));     // FFT{ h }
    q->w        = (float complex *) malloc((  q->n)* sizeof(float complex));     // delay buffer

    // create internal FFT objects
    q->fft  = FFT_CREATE_PLAN(2*q->n, q->time_buf, q->freq_buf, FFT_DIR_FORWARD,  FFT_METHOD);
    q->ifft = FFT_CREATE_PLAN(2*q->n, q->freq_buf, q->time_buf, FFT_DIR_BACKWARD, FFT_METHOD);

    // compute FFT of filter coefficients and copy to internal H array
    unsigned int i;
    for (i=0; i<2*q->n; i++)
        q->time_buf[i] = (i < q->h_len) ? q->h[i] : 0;
    // time_buf > {FFT} > freq_buf
    FFT_EXECUTE(q->fft);
    memmove(q->H, q->freq_buf, 2*q->n*sizeof(float complex));

    // set default scaling
    FFTFILT(_set_scale)(q, 1);

    // reset filter state (clear buffer)
    FFTFILT(_reset)(q);

    // return object
    return q;
}

// copy object
FFTFILT() FFTFILT(_copy)(FFTFILT() q_orig)
{
    // validate input
    if (q_orig == NULL)
        return liquid_error_config("firfilt_%s_copy(), object cannot be NULL", EXTENSION_FULL);

    // create filter object and copy base parameters
    FFTFILT() q_copy = (FFTFILT()) malloc(sizeof(struct FFTFILT(_s)));
    memmove(q_copy, q_orig, sizeof(struct FFTFILT(_s)));

    // copy filter coefficients
    q_copy->h = (TC *) liquid_malloc_copy(q_orig->h, q_orig->h_len, sizeof(TC));

    // allocate FFT buffers
    q_copy->time_buf = (float complex*) FFT_MALLOC((2*q_orig->n) * sizeof(float complex));
    q_copy->freq_buf = (float complex*) FFT_MALLOC((2*q_orig->n) * sizeof(float complex));

    // copy buffers
    memmove(q_copy->time_buf, q_orig->time_buf, (2*q_orig->n) * sizeof(float complex));
    memmove(q_copy->freq_buf, q_orig->freq_buf, (2*q_orig->n) * sizeof(float complex));
    q_copy->H = (float complex*) liquid_malloc_copy(q_orig->H, 2*q_orig->n, sizeof(float complex));
    q_copy->w = (float complex*) liquid_malloc_copy(q_orig->w,   q_orig->n, sizeof(float complex));

    // create internal FFT objects and return
    q_copy->fft  = FFT_CREATE_PLAN(2*q_copy->n, q_copy->time_buf, q_copy->freq_buf, FFT_DIR_FORWARD,  FFT_METHOD);
    q_copy->ifft = FFT_CREATE_PLAN(2*q_copy->n, q_copy->freq_buf, q_copy->time_buf, FFT_DIR_BACKWARD, FFT_METHOD);
    return q_copy;
}

// destroy object, freeing all internally-allocated memory
int FFTFILT(_destroy)(FFTFILT() _q)
{
    // free internal arrays
    free(_q->h);                // filter coefficients
    FFT_FREE(_q->time_buf);     // buffer (time domain)
    FFT_FREE(_q->freq_buf);     // buffer (frequency domain)
    free(_q->H);                // frequency response of filter coefficients
    free(_q->w);                // output window buffer

    // destroy FFT objects
    FFT_DESTROY_PLAN(_q->fft);  // forward transform
    FFT_DESTROY_PLAN(_q->ifft); // reverse transform

    // free main object
    free(_q);
    return LIQUID_OK;
}

// reset internal state of filter object
int FFTFILT(_reset)(FFTFILT() _q)
{
    // reset overlap window
    unsigned int i;
    for (i=0; i<_q->n; i++) {
        _q->w[i] = 0;
    }
    return LIQUID_OK;
}

// print filter object internals (taps, buffer)
int FFTFILT(_print)(FFTFILT() _q)
{
    printf("fftfilt_%s: [h_len=%u, n=%u]\n", EXTENSION_FULL, _q->h_len, _q->n);
    unsigned int i;
    unsigned int n = _q->h_len;
    for (i=0; i<n; i++) {
        printf("  h(%3u) = ", i+1);
        PRINTVAL_TC(_q->h[n-i-1],%12.8f);
        printf("\n");
    }

    // print scaling
    printf("  scale = ");
    PRINTVAL_TC(_q->scale,%12.8f);
    printf("\n");
    return LIQUID_OK;
}

// set output scaling for filter
int FFTFILT(_set_scale)(FFTFILT() _q,
                         TC        _scale)
{
    // set scale, normalized by fft size
    _q->scale = _scale / (float)(2*_q->n);
    return LIQUID_OK;
}

// get output scaling for filter
int FFTFILT(_get_scale)(FFTFILT() _q,
                         TC *      _scale)
{
    // get scale, normalized by fft size
    *_scale = _q->scale * (float)(2*_q->n);
    return LIQUID_OK;
}

// execute the filter on internal buffer and coefficients
//  _q      : filter object
//  _x      : pointer to input data array  [size: _n x 1]
//  _y      : pointer to output data array [size: _n x 1]
int FFTFILT(_execute)(FFTFILT() _q,
                      TI *      _x,
                      TO *      _y)
{
    unsigned int i;

    // copy input
#if 0 //TI_COMPLEX
    memmove(_q->time_buf, _x, _q->n*sizeof(TI));
#else
    // manual copy for type conversion
    // TODO: use DCT or equivalent
    for (i=0; i<_q->n; i++)
        _q->time_buf[i] = _x[i];
#endif

    // pad end of time-domain buffer with zeros
    // TODO: not necessary to do this every time
#if 0
    memset(&_q->time_buf[_q->n], 0, _q->n*sizeof(TI));
#else
    for (i=0; i<_q->n; i++)
        _q->time_buf[_q->n + i] = 0;
#endif

    // run forward transform
    FFT_EXECUTE(_q->fft);

    // compute inner product between FFT{ _x } and FFT{ H }
#if 1
    for (i=0; i<2*_q->n; i++)
        _q->freq_buf[i] *= _q->H[i];
#else
    // use SIMD vector extensions
# if TI_COMPLEX
    // complex floating-point inner product
    liquid_vectorcf_mul(_q->freq_buf, _q->H, 2*_q->n, _q->freq_buf);
# else
    // real floating-point inner product
    liquid_vectorf_mul(_q->freq_buf, _q->H, 2*_q->n, _q->freq_buf);
# endif
#endif

    // compute inverse transform
    FFT_EXECUTE(_q->ifft);

    // copy output summed with buffer and scaled
#if TI_COMPLEX
    for (i=0; i<_q->n; i++)
        _y[i] = (_q->time_buf[i] + _q->w[i]) * _q->scale;
#else
    // manual copy for type conversion
    // TODO: use DCT or equivalent
    for (i=0; i<_q->n; i++)
        _y[i] = (T) crealf(_q->time_buf[i] + _q->w[i]) * _q->scale;
#endif

    // copy buffer
    memmove(_q->w, &_q->time_buf[_q->n], _q->n*sizeof(float complex));
    return LIQUID_OK;
}

// return length of filter object's internal coefficients
unsigned int FFTFILT(_get_length)(FFTFILT() _q)
{
    return _q->h_len;
}


text:
using System.Reflection;
using System.Runtime.InteropServices;

[assembly: AssemblyCompany("Vintage Software")]
[assembly: AssemblyTitle("Vstack.Common")]
[assembly: AssemblyDescription("Vstack.Common")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("029ecbe3-6e0e-498e-80a7-bad385891c91")]
[assembly: AssemblyVersion("2.1.0.0")]


text:
package com.kubakhov.xml.parser;

import com.kubakhov.xml.component.XMLComponent;
import com.kubakhov.xml.parameters.XMLParameters;

public interface XMLParser {

	XMLComponent parse(String pathToXML, XMLParameters parameters) throws Exception;
}


text:
.class final Landroid/webkit/WebViewFactory$1;
.super Ljava/lang/Object;
.source "WebViewFactory.java"

# interfaces
.implements Ljava/lang/Runnable;

# annotations
.annotation system Ldalvik/annotation/EnclosingMethod;
    value = Landroid/webkit/WebViewFactory;->createRelroFile(Z[Ljava/lang/String;)V
.end annotation

.annotation system Ldalvik/annotation/InnerClass;
    accessFlags = 0x8
    name = null
.end annotation

# instance fields
.field final synthetic val$abi:Ljava/lang/String;

# direct methods
.method constructor <init>(Ljava/lang/String;)V
    .locals 0

    iput-object p1, p0, Landroid/webkit/WebViewFactory$1;->val$abi:Ljava/lang/String;

    invoke-direct {p0}, Ljava/lang/Object;-><init>()V

    return-void
.end method

# virtual methods
.method public run()V
    .locals 4

    :try_start_0
    const-string/jumbo v1, "WebViewFactory"

    new-instance v2, Ljava/lang/StringBuilder;

    invoke-direct {v2}, Ljava/lang/StringBuilder;-><init>()V

    const-string/jumbo v3, "relro file creator for "

    invoke-virtual {v2, v3}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v2

    iget-object v3, p0, Landroid/webkit/WebViewFactory$1;->val$abi:Ljava/lang/String;

    invoke-virtual {v2, v3}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v2

    const-string/jumbo v3, " crashed. Proceeding without"

    invoke-virtual {v2, v3}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v2

    invoke-virtual {v2}, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;

    move-result-object v2

    invoke-static {v1, v2}, Landroid/util/Log;->e(Ljava/lang/String;Ljava/lang/String;)I

    invoke-static {}, Landroid/webkit/WebViewFactory;->getUpdateService()Landroid/webkit/IWebViewUpdateService;

    move-result-object v1

    invoke-interface {v1}, Landroid/webkit/IWebViewUpdateService;->notifyRelroCreationCompleted()V
    :try_end_0
    .catch Landroid/os/RemoteException; {:try_start_0 .. :try_end_0} :catch_0

    :goto_0
    return-void

    :catch_0
    move-exception v0

    const-string/jumbo v1, "WebViewFactory"

    new-instance v2, Ljava/lang/StringBuilder;

    invoke-direct {v2}, Ljava/lang/StringBuilder;-><init>()V

    const-string/jumbo v3, "Cannot reach WebViewUpdateService. "

    invoke-virtual {v2, v3}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v2

    invoke-virtual {v0}, Landroid/os/RemoteException;->getMessage()Ljava/lang/String;

    move-result-object v3

    invoke-virtual {v2, v3}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v2

    invoke-virtual {v2}, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;

    move-result-object v2

    invoke-static {v1, v2}, Landroid/util/Log;->e(Ljava/lang/String;Ljava/lang/String;)I

    goto :goto_0
.end method


text:
package com.wookler.server.common.model;

/**
 * Exception type to be used to escalate Server exceptions
 *
 * @author Subho Ghosh (subho dot ghosh at outlook.com)
 * @created 01/09/14
 */
@SuppressWarnings("serial")
public class ServerException extends Exception {
    private static final String _PREFIX_ = "Server Exception : ";

    /**
     * Server exception capturing the exception server type and the
     * corresponding exception message
     *
     * @param type
     *            Server Exception type
     * @param mesg
     *            Exception message
     */
    public ServerException(Class<?> type, String mesg) {
        super(_PREFIX_ + " [" + type.getSimpleName() + "] " + mesg);
    }

    /**
     * Server exception capturing the exception server type, the
     * corresponding exception message and the exception cause
     *
     * @param type
     *            Server Exception type
     * @param mesg
     *            Exception message
     * @param inner
     *            Exception cause
     */
    public ServerException(Class<?> type, String mesg, Throwable inner) {
        super(_PREFIX_ + " [" + type.getSimpleName() + "] " + mesg, inner);
    }
}