text:
id: 587d78a9367417b2b2512ae8
title: Learn How Bezier Curves Work
challengeType: 0
videoUrl: 'https://scrimba.com/c/c9bDrs8'
forumTopicId: 301058
dashedName: learn-how-bezier-curves-work
---

# --description--

The last challenge introduced the `animation-timing-function` property and a few keywords that change the speed of an animation over its duration. CSS offers an option other than keywords that provides even finer control over how the animation plays out, through the use of Bezier curves.

In CSS animations, Bezier curves are used with the `cubic-bezier` function. The shape of the curve represents how the animation plays out. The curve lives on a 1 by 1 coordinate system. The X-axis of this coordinate system is the duration of the animation (think of it as a time scale), and the Y-axis is the change in the animation.

The `cubic-bezier` function consists of four main points that sit on this 1 by 1 grid: `p0`, `p1`, `p2`, and `p3`. `p0` and `p3` are set for you - they are the beginning and end points which are always located respectively at the origin (0, 0) and (1, 1). You set the x and y values for the other two points, and where you place them in the grid dictates the shape of the curve for the animation to follow. This is done in CSS by declaring the x and y values of the `p1` and `p2` "anchor" points in the form: `(x1, y1, x2, y2)`. Pulling it all together, here's an example of a Bezier curve in CSS code:

```css
animation-timing-function: cubic-bezier(0.25, 0.25, 0.75, 0.75);
```

In the example above, the x and y values are equivalent for each point (x1 = 0.25 = y1 and x2 = 0.75 = y2), which if you remember from geometry class, results in a line that extends from the origin to point (1, 1). This animation is a linear change of an element during the length of an animation, and is the same as using the `linear` keyword. In other words, it changes at a constant speed.

# --instructions--

For the element with the id of `ball1`, change the value of the `animation-timing-function` property from `linear` to its equivalent `cubic-bezier` function value. Use the point values given in the example above.

# --hints--

The value of the `animation-timing-function` property for the element with the id `ball1` should be the linear-equivalent `cubic-bezier` function.

```js
assert(
  $('#ball1').css('animation-timing-function') ==
    'cubic-bezier(0.25, 0.25, 0.75, 0.75)'
);
```

The value of the `animation-timing-function` property for the element with the id `ball2` should not change.

```js
const ball2Animation = __helpers.removeWhiteSpace(
  $('#ball2').css('animation-timing-function')
);
assert(
  ball2Animation == 'ease-out' || ball2Animation == 'cubic-bezier(0,0,0.58,1)'
);
```

# --seed--

## --seed-contents--

```html
<style>

  .balls{
    border-radius: 50%;
    background: linear-gradient(
      35deg,
      #ccffff,
      #ffcccc
    );
    position: fixed;
    width: 50px;
    height: 50px;
    margin-top: 50px;
    animation-name: bounce;
    animation-duration: 2s;
    animation-iteration-count: infinite;
  }
  #ball1 {
    left: 27%;
    animation-timing-function: linear;
  }
  #ball2 {
    left: 56%;
    animation-timing-function: ease-out;
  }

  @keyframes bounce {
    0% {
      top: 0px;
    }
    100% {
      top: 249px;
    }
  }

</style>

<div class="balls" id="ball1"></div>
<div class="balls" id="ball2"></div>
```

# --solutions--

```html
<style>

  .balls{
    border-radius: 50%;
    background: linear-gradient(
      35deg,
      #ccffff,
      #ffcccc
    );
    position: fixed;
    width: 50px;
    height: 50px;
    margin-top: 50px;
    animation-name: bounce;
    animation-duration: 2s;
    animation-iteration-count: infinite;
  }
  #ball1 {
    left: 27%;
    animation-timing-function: cubic-bezier(0.25, 0.25, 0.75, 0.75);
  }
  #ball2 {
    left: 56%;
    animation-timing-function: ease-out;
  }

  @keyframes bounce {
    0% {
      top: 0px;
    }
    100% {
      top: 249px;
    }
  }
</style>
<div class="balls" id="ball1"></div>
<div class="balls" id="ball2"></div>
```


text:
package org.jbb.e2e.serenity.web.lockout;

import net.serenitybdd.core.pages.PageObject;
import net.thucydides.core.annotations.DefaultUrl;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.FindBys;

@DefaultUrl(AcpMemberLockoutSettingsPage.URL)
public class AcpMemberLockoutSettingsPage extends PageObject {
    public static final String URL = "/acp/general/lockout";

    @FindBy(id = "lockoutEnabled")
    WebElement lockoutEnabledRadioButton;

    @FindBy(id = "lockoutDisabled")
    WebElement lockoutDisabledRadioButton;

    @FindBy(id = "failedAttemptsThreshold")
    WebElement failedAttemptsThresholdField;

    @FindBy(id = "failedSignInAttemptsExpirationMinutes")
    WebElement failedAttemptsExpirationField;

    @FindBy(id = "lockoutDurationMinutes")
    WebElement lockoutDurationField;

    @FindBys({@FindBy(xpath = "//button[contains(text(),'Save')]")})
    WebElement saveButton;

    public void clickSaveButton() {
        saveButton.click();
    }

    public void clickEnableLockoutRadioButton() {
        lockoutEnabledRadioButton.click();
    }

    public void clickDisableLockoutRadioButton() {
        lockoutDisabledRadioButton.click();
    }

    public void typeFailedAttemptsThreshold(String failedAttemptsThreshold) {
        failedAttemptsThresholdField.clear();
        failedAttemptsThresholdField.sendKeys(failedAttemptsThreshold);
    }

    public void typeFailedAttemptsExpiration(String failedAttemptsExpiration) {
        failedAttemptsExpirationField.clear();
        failedAttemptsExpirationField.sendKeys(failedAttemptsExpiration);
    }

    public void typeLockoutDuration(String lockoutDuration) {
        lockoutDurationField.clear();
        lockoutDurationField.sendKeys(lockoutDuration);
    }

    public void shouldBeVisibleInfoAboutPositiveValue() {
        shouldContainText("must be greater than or equal to 1");
    }

    public void shouldBeVisibleInfoAboutInvalidValue() {
        shouldContainText("Invalid value");
    }
}

redpajama_set_name: RedPajamaGithub


text:
=head1 LICENSE

Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Copyright [2016-2018] EMBL-European Bioinformatics Institute

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.

=cut

package EnsEMBL::Web::Job::AlleleFrequency;

use strict;
use warnings;

use JSON qw(to_json);

use EnsEMBL::Web::Utils::FileHandler qw(file_put_contents);
use EnsEMBL::Web::File::Utils::URL;

use parent qw(EnsEMBL::Web::Job);

sub prepare_to_dispatch {
  ## @override
  my $self        = shift;

  my $rose_object = $self->rose_object;
  my $job_data    = $rose_object->job_data;
  my $job_dir     = $rose_object->job_dir;
  my $region      = $job_data->{'region'};
  my $population  = $job_data->{'population'};
  my $file_url    = $job_data->{'file_url'};
  my $proxy       = $self->hub->web_proxy;
  my $tabix       = $self->hub->species_defs->TABIX;
  my $bgzip       = $self->hub->species_defs->BGZIP;
  my $tools_dir   = $self->hub->species_defs->SHARED_SOFTWARE_PATH;

  # output file name, storing it in db for checking content
  my $output_file = "afc.".($region =~ s/:/./r).".proc$$.tsv";

  # download sample file  to work dir
  my $args        = {'no_exception' => 1 };
  $args->{proxy}  = $proxy ? $proxy : "";
  $args->{nice}   = 1;
  $args->{destination_path} = "$job_dir/";

  my $sample_file = EnsEMBL::Web::File::Utils::URL::fetch_file($job_data->{'sample_panel'}, $args);

  if(ref($sample_file) eq 'HASH' && $sample_file->{error}) {
    throw exception('HiveException', "Job failed, Sample panel file cannot be found: ".$sample_file->{error});
  }

  return {
    'work_dir'      => $job_dir,
    'output_file'   => $output_file,
    'input_file'    => $file_url,
    'tools_dir'     => $tools_dir,
    'tabix'         => $tabix,
    'bgzip'         => $bgzip,
    'region'        => $job_data->{'region'},
    'population'    => $job_data->{'population'},
    'sample_panel'  => $sample_file,
    'proxy'         => $proxy,
    'code_root'     => $self->hub->species_defs->ENSEMBL_HIVE_HOSTS_CODE_LOCATION
  };
}

1;


text:
[![Build Status](https://travis-ci.org/javiercejudo/betterer.svg)](https://travis-ci.org/javiercejudo/betterer)
[![Coverage Status](https://coveralls.io/repos/javiercejudo/betterer/badge.svg?branch=master)](https://coveralls.io/r/javiercejudo/betterer?branch=master)
[![Code Climate](https://codeclimate.com/github/javiercejudo/betterer/badges/gpa.svg)](https://codeclimate.com/github/javiercejudo/betterer)

Proof of concept for tests that read better(er)

## Install

    npm i betterer

## Basic usage

```js
var better = require('./betterer.js').better;

// must export a doTheRightThing function, typically with at least one `it`
var yourTests = require('./yourTests.js');

var o_o = describe;

o_o('using your thing', function() { var o_O = yourTests;
  o_o('the right way', better('do the right thing', o_O));
});
```

The string 'do the right thing' is mapped to the `doTheRightThing`
function exported by `yourTests.js`.

## See more

- [spec](test/spec.js)


text:
package com.hazelcast.jet.pipeline;

import com.hazelcast.function.BiFunctionEx;
import com.hazelcast.function.FunctionEx;
import com.hazelcast.jet.core.Processor;
import com.hazelcast.jet.core.processor.SourceProcessors;

import javax.annotation.Nonnull;
import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;

import static com.hazelcast.jet.pipeline.Sources.batchFromProcessor;
import static java.nio.charset.StandardCharsets.UTF_8;

/**
 * Builder for a file source which reads lines from files in a directory (but not
 * its subdirectories) and emits output object created by {@code mapOutputFn}
 *
 * @since 3.0
 */
public final class FileSourceBuilder {

    private static final String GLOB_WILDCARD = "*";

    private final String directory;

    private String glob = GLOB_WILDCARD;
    private boolean sharedFileSystem;
    private Charset charset = UTF_8;

    /**
     * Use {@link Sources#filesBuilder}.
     */
    FileSourceBuilder(@Nonnull String directory) {
        this.directory = directory;
    }

    /**
     * Sets the globbing mask, see {@link
     * java.nio.file.FileSystem#getPathMatcher(String) getPathMatcher()}.
     * Default value is {@code "*"} which means all files.
     */
    @Nonnull
    public FileSourceBuilder glob(@Nonnull String glob) {
        this.glob = glob;
        return this;
    }

    /**
     * Sets if files are in a shared storage visible to all members. Default
     * value is {@code false}.
     *

     * If {@code sharedFileSystem} is {@code true}, Jet will assume all members
     * see the same files. They will split the work so that each member will
     * read a part of the files. If {@code sharedFileSystem} is {@code false},
     * each member will read all files in the directory, assuming the are
     * local.
     *

     * If you start all the members on a single machine (such as for
     * development), set this property to true. If you have multiple machines
     * with multiple members each and the directory is not a shared storage,
     * it's not possible to configure the file reader correctly - use only one
     * member per machine.
     */
    @Nonnull
    public FileSourceBuilder sharedFileSystem(boolean sharedFileSystem) {
        this.sharedFileSystem = sharedFileSystem;
        return this;
    }

    /**
     * Sets the character set used to encode the files. Default value is {@link
     * java.nio.charset.StandardCharsets#UTF_8}.
     *

     * Setting this component has no effect if the user provides a custom
     * {@code readFileFn} to the {@link #build(FunctionEx) build()} method.
     */
    @Nonnull
    public FileSourceBuilder charset(@Nonnull Charset charset) {
        this.charset = charset;
        return this;
    }

    /**
     * Convenience for {@link FileSourceBuilder#build(BiFunctionEx)}.
     * Source emits lines to downstream without any transformation.
     */
    @Nonnull
    public BatchSource<String> build() {
        return build((filename, line) -> line);
    }

    /**
     * Builds a custom file {@link BatchSource} with supplied components and the
     * output function {@code mapOutputFn}.
     *

     * The source does not save any state to snapshot. If the job is restarted,
     * it will re-emit all entries.
     *

     * Any {@code IOException} will cause the job to fail. The files must not
     * change while being read; if they do, the behavior is unspecified.
     *

     * The default local parallelism for this processor is 4 (or available CPU
     * count if it is less than 4).
     *
     * @param mapOutputFn the function which creates output object from each
     *                    line. Gets the filename and line as parameters. It must be stateless and
     *                    {@linkplain Processor#isCooperative() cooperative}.
     * @param <T>         the type of the items the source emits
     */
    @Nonnull
    public <T> BatchSource<T> build(@Nonnull BiFunctionEx<String, String, ? extends T> mapOutputFn) {
        String charsetName = charset.name();
        return batchFromProcessor("filesSource(" + new File(directory, glob) + ')',
                SourceProcessors.readFilesP(directory, glob, sharedFileSystem,
                        path -> {
                            String fileName = path.getFileName().toString();
                            return Files.lines(path, Charset.forName(charsetName))
                                        .map(l -> mapOutputFn.apply(fileName, l));
                        }));
    }

    /**
     * Builds a custom file {@link BatchSource} with supplied components. Will
     * use the supplied {@code readFileFn} to read the files. The configured
     * {@linkplain #charset(Charset) is ignored}.
     *

     * The source does not save any state to snapshot. If the job is restarted,
     * it will re-emit all entries.
     *

     * Any {@code IOException} will cause the job to fail. The files must not
     * change while being read; if they do, the behavior is unspecified.
     *

     * The default local parallelism for this processor is 4 (or available CPU
     * count if it is less than 4).
     *
     * @param readFileFn the function to read objects from a file. Gets file
     *                   {@code Path} as parameter and returns a {@code Stream}
     *                   of items. The function must be stateless.
     * @param <T>        the type of items returned from file reading
     */
    @Nonnull
    public <T> BatchSource<T> build(@Nonnull FunctionEx<? super Path, ? extends Stream<T>> readFileFn) {
        return batchFromProcessor("filesSource(" + new File(directory, glob) + ')',
                SourceProcessors.readFilesP(directory, glob, sharedFileSystem, readFileFn));
    }

    /**
     * Convenience for {@link FileSourceBuilder#buildWatcher(BiFunctionEx)}.
     */
    @Nonnull
    public StreamSource<String> buildWatcher() {
        return buildWatcher((filename, line) -> line);
    }

    /**
     * Builds a source that emits a stream of lines of text coming from files in
     * the watched directory (but not its subdirectories). It will emit only
     * new contents added after startup: both new files and new content
     * appended to existing ones.
     *

     * If, during the scanning phase, the source observes a file that doesn't
     * end with a newline, it will assume that there is a line just being
     * written. This line won't appear in its output.
     *

     * The source completes when the directory is deleted. However, in order
     * to delete the directory, all files in it must be deleted and if you
     * delete a file that is currently being read from, the job may encounter
     * an {@code IOException}. The directory must be deleted on all nodes if
     * {@code sharedFileSystem} is {@code false}.
     *

     * Any {@code IOException} will cause the job to fail.
     *

     * The source does not save any state to snapshot. If the job is restarted,
     * lines added after the restart will be emitted, which gives at-most-once
     * behavior.
     *

     * The default local parallelism for this processor is 2 (or 1 if just 1
     * CPU is available).
     *
     *
Limitation on Windows

     * On Windows the {@code WatchService} is not notified of appended lines
     * until the file is closed. If the file-writing process keeps the file
     * open while appending, the processor may fail to observe the changes.
     * It will be notified if any process tries to open that file, such as
     * looking at the file in Explorer. This holds for Windows 10 with the NTFS
     * file system and might change in future. You are advised to do your own
     * testing on your target Windows platform.
     *
     *
Use the latest JRE

     * The underlying JDK API ({@link java.nio.file.WatchService}) has a
     * history of unreliability and this source may experience infinite
     * blocking, missed, or duplicate events as a result. Such problems may be
     * resolved by upgrading the JRE to the latest version.
     *
     *
Appending lines using an text editor

     * If you're testing this source, you might think of using a text editor to
     * append the lines. However, it might not work as expected because some
     * editors write to a temp file and then rename it or append extra newline
     * character at the end which gets overwritten if more text is added in the
     * editor. The best way to append is to use {@code echo text >> yourFile}.
     *
     * @param mapOutputFn the function which creates output object from each
     *                    line. Gets the filename and line as parameters. It must be stateless and
     *                    {@linkplain Processor#isCooperative() cooperative}.
     * @param <T>         the type of the items the source emits
     */
    @Nonnull
    public <T> StreamSource<T> buildWatcher(@Nonnull BiFunctionEx<String, String, ? extends T> mapOutputFn) {
        return Sources.streamFromProcessor("fileWatcherSource(" + directory + '/' + glob + ')',
                SourceProcessors.streamFilesP(directory, charset, glob, sharedFileSystem, mapOutputFn));
    }
}


text:
#include "data/hdf/HDFCmpFile.h"
#include "datastructures/alignment/CmpFile.h"
#include "CommandLineParser.h"
#include "datastructures/alignment/ByteAlignment.h"
#include "algorithms/alignment/printers/StickAlignmentPrinter.h"
#include "data/hdf/HDFBasReader.h"
#include "data/hdf/HDFBasWriter.h"
#include "utils/RegionUtils.h"
#include "files/ReaderAgglomerate.h"
#include "utils/FileOfFileNames.h"
#include "datastructures/alignment/ByteAlignment.h"
#include "Types.h"
#include "datastructures/matrix/Matrix.h"
#include <string>
#include <ext/hash_map>
#include <map>

#include "InsertedString.h"
#include "Profile.h"
#include "MSAUtils.h"

using namespace std;

typedef vector<unsigned char> ByteAlignment;

int PrintSNVS(FASTASequence &genome, DNASequence &sample, vector<int> &coverage) {
  int i;
  for (i = 0; i < genome.size(); i++) {
    //#ref000001	.	SNV	9454	9454	.	.	.	coverage=40;confidence=10.0;genotype=A;reference=C
    if (genome.seq[i] != sample.seq[i] and sample.seq[i] != ' ') {
      cout << genome.GetName() << "\t.\tSNV\t" << i + 1 << "\t" << i + 1
           << "\t.\t.\t.\tcoverage=" << coverage[i] << ";confidence=10.0;"
           << "variantseq="<<sample.seq[i] <<";"
           << "reference="<<genome.seq[i] << endl;
    }
  }
}

void PrintDeletions(FASTASequence &reference, DNASequence &sample, vector<int> &coverage) {
  //ref000001	.	deletion	50141	50141	.	.	.	coverage=28;confidence=1.0;length=1;reference=C
  int ndel = 0;
  int i;
  for (i = 0; i < reference.length; i++) {
    if (sample.seq[i] == ' ') {
      cout << reference.GetName() << "\t.\tdeletion\t" << i +1 << "\t" << i+1 << "\t.\t.\t.\t"
           << "coverage="<<coverage[i+1]<<";"
           << "confidence=10.0;length=1;reference=" << reference.seq[i] << endl;
    }
  }
}

int CountDeletions(DNASequence &seq) {
  int ndel = 0;
  int i;
  for (i = 0; i < seq.length; i++) {
    if (seq.seq[i] == ' ') {
      ndel++;
    }
  }
  return ndel;
}

void StoreCalledBasesInGenome(Profile &profile, DNASequence &genome) {
  int i;
  for (i = 0; i < profile.profileMatrix.GetNCols(); i++) {
    int c;
    int maxC = 0;
    int maxCCount = profile.profileMatrix[0][i];
    for (c = 1; c < 5; c++) {
      if (profile.profileMatrix[c][i] > maxCCount) {
        maxCCount = profile.profileMatrix[c][i];
        maxC = c;
      }
    }
    if (maxC == 4) {
      //      cout << " max del at " << i << " with " << maxCCount << " " << profile.profileMatrix[0][i] << " " << profile.profileMatrix[1][i] << " " << profile.profileMatrix[2][i] << " " << profile.profileMatrix[3][i] << " " << endl;
      genome.seq[i] = ' ';
    }
    else {
      assert(maxC < 4);
      genome.seq[i] = TwoBitToAscii[maxC];
    }
  }
}

int PrintInsertions(Profile &profile, FASTASequence &sampleGenome, vector<int> &coverage, int window=5) {
  //
  // Look to see if any insertion columns appear about average in
  // relation to the average coverage in the region.
  //
  int i;
  int numInsertions=0;
  for (i = 0; i < profile.profileMatrix.GetNCols(); i++) {
    //
    // First find the character inserted the most.
    //
    int maxInsChar = 5;
    int maxInsCount = profile.profileMatrix[5][i];
    int c;
    for (c = 6; c < 9; c++) {
      if (profile.profileMatrix[c][i] > maxInsCount) {
        maxInsCount = profile.profileMatrix[c][i];
        maxInsChar  = c;
      }
    }
    //
    // Now count the coverage in the region.
    //
    int start, end;
    if (i < window) {
      start = 0;
    }
    else {
      start = i - window;
    }
    if (i + window > profile.profileMatrix.GetNCols()) {
      end = profile.profileMatrix.GetNCols();
    }
    else {
      end = i + window;
    }
    int totalCov = 0;
    int w;
    for (w = start; w < end; w++) {
      if (sampleGenome[w] != ' ') {
        totalCov += profile.profileMatrix[TwoBit[sampleGenome[w]]][w];
      }
      else {
        totalCov += profile.profileMatrix[4][w];
      }
    }
    if (end > start) {
      float averageCoverage = totalCov / ((float)(end - start));
      if (maxInsCount * 2 > averageCoverage) {
        //ref000001	.	insertion	50223	50223	.	.	.	coverage=34;confidence=10.0;length=1;variantseq=A;reference=G
        cout << sampleGenome.GetName() << "\t.\tinsertion\t" << i + 1 << "\t" << i + 1 << "\t.\t.\t.\t"
             << "coverage=" << coverage[i] << ";confidence=10.0;length=1;"
             << "variantseq=" << TwoBitToAscii[maxInsChar-5] << ";"
             << "reference=" << sampleGenome.seq[i] << endl;
        ++numInsertions;
      }
    }
  }
  return numInsertions;
}

void ProfileToSequence(Profile &profile, DNASequence &seq) {

}

typedef vector<InsertedString> InsertedStringList;
void StoreInsertionQVs(InsertedStringList &insertions, vector<char> &valueStr) {
  int i;
  for (i = 0; i < insertions.size(); i++) {
    int substrLen = insertions[i].insSeq.size();
    int substrPos = insertions[i].alnPos;
    // Modify the string to be from the valuestr.
    insertions[i].insQVSeq = "";
    insertions[i].insQVSeq.insert(0, &valueStr[substrPos], substrLen);
  }
}

int ComputeOffsetToRefPosition(ByteAlignment &aln, DNALength alnStartPos, DNALength refPos, DNALength &alnIndex, DNALength &refOffset) {
  alnIndex = 0;
  refOffset = 0;
  while(alnIndex < aln.size() and refOffset + alnStartPos != refPos) {
    // move past the current position.
    if (RefChar[aln[alnIndex]] != ' ') {
      refOffset++;
    }
    alnIndex++;
  }
  if (alnIndex < aln.size()) {
    return 1;
  }
  else {
    return 0;
  }
}

int main(int argc, char* argv[]) {
  CommandLineParser clp;
  string cmpH5FileName, genomeFileName;
  int  scale = 5;
  int  marginSize = 5;
  stringstream verboseHelpStream, helpStream;
  vector<int> coverage;
  verboseHelpStream << "printmsa is a utility to either view the msa centered at a position" << endl
                    << "in a base.h5 file, or print all reads overlapping a position." << endl;
  clp.SetVerboseHelp(verboseHelpStream.str());
  clp.RegisterStringOption("cmpH5FileName", &cmpH5FileName, "The input hdf file");
  clp.RegisterStringOption("genome", &genomeFileName, "Name of the reference reads were aligned to (1 contig for now)");
  clp.RegisterPreviousFlagsAsHidden();
  clp.RegisterIntOption("scale",           &scale, "Scale quality values by 'scale'", CommandLineParser::PositiveInteger);

  clp.ParseCommandLine(argc, argv);

  FASTASequence genome;
  ReaderAgglomerate reader;
  reader.Initialize(genomeFileName);

  CmpFile cmpFile;
	HDFCmpFile<CmpAlignment> cmpReader;

	if (cmpReader.Initialize(cmpH5FileName, H5F_ACC_RDONLY) == 0) {
		cout << "ERROR, could not open the cmp file." << endl;
		exit(1);
	}
  //	cout << "Reading cmp file." << endl;
	cmpReader.Read(cmpFile);

  int p;
  while (reader.GetNext(genome)) {
    genome.ToUpper();
    FASTASequence sampleGenome;
    sampleGenome.Copy(genome);
    Profile profile(genome.length);
    coverage.resize(genome.length);
    fill(coverage.begin(), coverage.end(), 0);
    int alignmentIndex;

    for (alignmentIndex = 0; alignmentIndex < cmpFile.alnInfo.alignments.size(); alignmentIndex++) {
      int readStart, readEnd, refStart, refEnd;

      readStart = cmpFile.alnInfo.alignments[alignmentIndex].GetQueryStart();
      readEnd   = cmpFile.alnInfo.alignments[alignmentIndex].GetQueryEnd();
      refStart  = cmpFile.alnInfo.alignments[alignmentIndex].GetRefStart();
      refEnd    = cmpFile.alnInfo.alignments[alignmentIndex].GetRefEnd();
      int holeNumber = cmpFile.alnInfo.alignments[alignmentIndex].GetHoleNumber();
      int refGroupId = cmpFile.alnInfo.alignments[alignmentIndex].GetRefGroupId();
      int refInfoIndex;
      int refGroupIndex = cmpReader.refGroupIdToArrayIndex[refGroupId];
      int refInfoId = cmpFile.refGroup.refInfoId[refGroupIndex];
      if (cmpFile.refInfo.RefIdToIndex(refInfoId, refInfoIndex) == false) {
        cout << "ERROR, could not find refGroupId " << refGroupId << " in ref info structure." << endl;
        exit(1);
      }

      string refFullName = cmpFile.refInfo.fullName[refInfoIndex];
      //      cout <<" The alignment is aligned to " << refFullName << " vs " << genome.title << endl;

      int refStrand      = cmpFile.alnInfo.alignments[alignmentIndex].GetRCRefStrand();

      //
      // Read the alignment string.  All alignments
      //
      int offsetBegin = cmpFile.alnInfo.alignments[alignmentIndex].GetOffsetBegin();
      int offsetEnd   = cmpFile.alnInfo.alignments[alignmentIndex].GetOffsetEnd();

      int alignedSequenceLength = offsetEnd - offsetBegin;
      string   alignedSequence;
      ByteAlignment byteAlignment, byteAlignmentRC;
      vector<char> insQVChars, delQVChars, subQVChars;

      if (refFullName != genome.title) {
        //  cout << "ref full name is not " << genome.title << endl;
        continue;
      }
      if (alignedSequenceLength >= 0) {
        alignedSequence.resize(alignedSequenceLength);
        byteAlignment.resize(alignedSequenceLength);
      }
      byteAlignment = cmpFile.alnInfo.alignments[alignmentIndex].alignmentArray;

      if (refStrand == 1) {
        byteAlignmentRC.resize(byteAlignment.size());
        MakeReverseComplementByteAlignment(&byteAlignment[0], byteAlignment.size(), &byteAlignmentRC[0]);
        byteAlignment = byteAlignmentRC;
      }

      vector<int> refPositions, queryPositions;
      ComputeQueryPositions(byteAlignment, queryPositions);
      ComputeRefPositions(byteAlignment,   refPositions);

      string alnStr;
      InsertedStringList insertions;
      StoreInsertedStrings(byteAlignment, refPositions, queryPositions,
                           insertions, 0, byteAlignment.size());

      //
      // Project the alignment onto the reference
      int p = 0, bp = 0;
      while (bp < byteAlignment.size()) {
        if (RefChar[byteAlignment[bp]] != ' ') {
          alnStr.push_back(QueryChar[byteAlignment[bp]]);
          p++;
        }
        bp++;
      }

      for (p = 0; p < refPositions.size(); p++) {
        if (refPositions[p] != -1) {
          coverage[refStart + refPositions[p]]++;
        }
      }

      profile.StoreProfile(alnStr, 0, 0, insertions, refStart);
      /*    if (alignmentIndex % 10000 == 0) {
            cout << alignmentIndex << endl;
            }*/
    }
    StoreCalledBasesInGenome(profile, sampleGenome);
    PrintDeletions(genome, sampleGenome, coverage);
    PrintSNVS(genome, sampleGenome, coverage);
    PrintInsertions(profile, sampleGenome, coverage);
  }
  //  cout << "The consensus has " << ndel << " deletions. " << endl;
//  profile.Print(6);
  return 0;
}


text:
#include "header.h"
#include "drink_counter.h"
#include "main_display.h"

/**********************************************************
*
* DRINK COUNTER
*
*_________________________________________________________
* This Pebble watchapp allows a user to monitor drink
* consumption. The total number of drinks, time of the
* first drink, time of the last drink and drinks per hour
* are recorded. Drink count is always displayed on the top
* half of the screen, while first, last and drinks per
* hour can be cycled through on the bottom half.
*_________________________________________________________
* Input is given through button clicks.
* UP single click     - increment drink count
* UP double click     - decrement drink count
* SELECT double click - reset drink counter
* DOWN single click   - change lower display
***********************************************************/

static MainDisplay *mainDisplay;

static void init(void);
static void deinit(void);

int main(void)
{
  init();
  app_event_loop();
  deinit();
}

static void init(void)
{
    drink_counter_create();
    drink_counter_persist_load();
    mainDisplay = main_display_create();
    window_set_user_data(mainDisplay->window, mainDisplay);
    main_display_window_init(mainDisplay);
}

static void deinit(void)
{
    drink_counter_persist_write();
    drink_counter_destroy();
    main_display_destroy(mainDisplay);
}


text:
module.exports = function (gulp, plugins) {
    return function () {
        return gulp.src('index.ts')
            .pipe(plugins.tslint())
            .pipe(plugins.tslint.report('verbose'));
    };
};


text:
export function routerConfig ($routeProvider) {
  'ngInject';
  $routeProvider
    .when('/stats', {
      templateUrl: 'app/stats/stats.html',
      controller: 'StatsController',
      controllerAs: 'stats'
    })
    .when('/tasks', {
      templateUrl: 'app/main/main.html',
      controller: 'MainController',
      controllerAs: 'main'
    })
    .otherwise({
      redirectTo: '/tasks'
    });
}


text:
package org.apache.asterix.external.library;

import org.apache.asterix.external.api.IExternalScalarFunction;
import org.apache.asterix.external.api.IFunctionFactory;

public class KeywordsDetectorFactory implements IFunctionFactory {

    @Override
    public IExternalScalarFunction getExternalFunction() {
        return new KeywordsDetectorFunction();
    }
}


text:
package net.loomchild.segment;

import java.util.Iterator;
import java.util.List;

import net.loomchild.segment.srx.LanguageRule;

/**
 * Represents abstract text iterator. Responsible for implementing remove
 * operation.
 *
 * @author loomchild
 *
 */
public abstract class AbstractTextIterator implements TextIterator {

	/**
	 * {@inheritDoc}
	 */
	public void remove() {
		throw new UnsupportedOperationException(
				"Remove is not supported by TextIterator.");
	}

	public String toString(List<LanguageRule> languageRuleList) {
		StringBuilder builder = new StringBuilder();
		if (languageRuleList.size() > 0) {
			Iterator<LanguageRule> iterator = languageRuleList.iterator();
			builder.append(iterator.next().getName());
			while (iterator.hasNext()) {
				builder.append(iterator.next().getName());
			}
		}
		return builder.toString();
	}

}


text:
package com.bukhmastov.cdoitmo.activity.presenter.impl;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;

import com.bukhmastov.cdoitmo.R;
import com.bukhmastov.cdoitmo.activity.PikaActivity;
import com.bukhmastov.cdoitmo.activity.presenter.PikaActivityPresenter;
import com.bukhmastov.cdoitmo.factory.AppComponentProvider;
import com.bukhmastov.cdoitmo.firebase.FirebaseAnalyticsProvider;
import com.bukhmastov.cdoitmo.util.DateUtils;
import com.bukhmastov.cdoitmo.util.Log;
import com.bukhmastov.cdoitmo.util.StoragePref;
import com.bukhmastov.cdoitmo.util.Thread;

import java.util.Random;

import javax.inject.Inject;

public class PikaActivityPresenterImpl implements PikaActivityPresenter {

    private static final String TAG = "PikaActivity";
    private PikaActivity activity = null;
    private final Random random = new Random();
    private boolean dimas = false;

    @Inject
    Log log;
    @Inject
    Thread thread;
    @Inject
    StoragePref storagePref;
    @Inject
    DateUtils textUtils;
    @Inject
    FirebaseAnalyticsProvider firebaseAnalyticsProvider;

    public PikaActivityPresenterImpl() {
        AppComponentProvider.getComponent().inject(this);
    }

    @Override
    public void setActivity(@NonNull PikaActivity activity) {
        this.activity = activity;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        thread.runOnUI(() -> {
            log.v(TAG, "PIKA is no longer hiding");
            firebaseAnalyticsProvider.logCurrentScreen(activity);
            activity.overridePendingTransition(R.anim.zoom_bottom_in, R.anim.zoom_bottom_out);
            if (random.nextInt(6) % 6 == 0) {
                log.v(TAG, "LEGENDARY D1MA$ APPEARS, so pika went away");
                dimas = true;
                ((ImageView) activity.findViewById(R.id.image)).setImageDrawable(activity.getDrawable(R.drawable.wuwari));
            }
            View pikaContainer = activity.findViewById(R.id.pika_container);
            if (pikaContainer != null) {
                pikaContainer.setOnClickListener(v -> {
                    activity.finish();
                    activity.overridePendingTransition(R.anim.zoom_bottom_in, R.anim.zoom_bottom_out);
                });
            }
        });
    }

    @Override
    public void onDestroy() {
        thread.runOnUI(() -> {
            if (dimas) {
                log.v(TAG, "D1MA$ left us to not to be late for some movies");
            } else {
                log.v(TAG, "PIKA left us to stream some games");
            }
        });
    }
}


text:
package codec

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"hash"
	"io"
	"time"
	"unsafe"

	"github.com/pingcap/errors"
	"github.com/pingcap/tidb/parser/mysql"
	"github.com/pingcap/tidb/parser/terror"
	"github.com/pingcap/tidb/sessionctx/stmtctx"
	"github.com/pingcap/tidb/types"
	"github.com/pingcap/tidb/util/chunk"
	"github.com/pingcap/tidb/util/collate"
	"github.com/pingcap/tidb/util/hack"
	"github.com/pingcap/tidb/util/logutil"
	"go.uber.org/zap"
)

// First byte in the encoded value which specifies the encoding type.
const (
	NilFlag          byte = 0
	bytesFlag        byte = 1
	compactBytesFlag byte = 2
	intFlag          byte = 3
	uintFlag         byte = 4
	floatFlag        byte = 5
	decimalFlag      byte = 6
	durationFlag     byte = 7
	varintFlag       byte = 8
	uvarintFlag      byte = 9
	jsonFlag         byte = 10
	maxFlag          byte = 250
)

const (
	sizeUint64  = unsafe.Sizeof(uint64(0))
	sizeFloat64 = unsafe.Sizeof(float64(0))
)

func preRealloc(b []byte, vals []types.Datum, comparable1 bool) []byte {
	var size int
	for i := range vals {
		switch vals[i].Kind() {
		case types.KindInt64, types.KindUint64, types.KindMysqlEnum, types.KindMysqlSet, types.KindMysqlBit, types.KindBinaryLiteral:
			size += sizeInt(comparable1)
		case types.KindString, types.KindBytes:
			size += sizeBytes(vals[i].GetBytes(), comparable1)
		case types.KindMysqlTime, types.KindMysqlDuration, types.KindFloat32, types.KindFloat64:
			size += 9
		case types.KindNull, types.KindMinNotNull, types.KindMaxValue:
			size++
		case types.KindMysqlJSON:
			size += 2 + len(vals[i].GetBytes())
		case types.KindMysqlDecimal:
			size += 1 + types.MyDecimalStructSize
		default:
			return b
		}
	}
	return reallocBytes(b, size)
}

// encode will encode a datum and append it to a byte slice. If comparable1 is true, the encoded bytes can be sorted as it's original order.
// If hash is true, the encoded bytes can be checked equal as it's original value.
func encode(sc *stmtctx.StatementContext, b []byte, vals []types.Datum, comparable1 bool) (_ []byte, err error) {
	b = preRealloc(b, vals, comparable1)
	for i, length := 0, len(vals); i < length; i++ {
		switch vals[i].Kind() {
		case types.KindInt64:
			b = encodeSignedInt(b, vals[i].GetInt64(), comparable1)
		case types.KindUint64:
			b = encodeUnsignedInt(b, vals[i].GetUint64(), comparable1)
		case types.KindFloat32, types.KindFloat64:
			b = append(b, floatFlag)
			b = EncodeFloat(b, vals[i].GetFloat64())
		case types.KindString:
			b = encodeString(b, vals[i], comparable1)
		case types.KindBytes:
			b = encodeBytes(b, vals[i].GetBytes(), comparable1)
		case types.KindMysqlTime:
			b = append(b, uintFlag)
			b, err = EncodeMySQLTime(sc, vals[i].GetMysqlTime(), mysql.TypeUnspecified, b)
			if err != nil {
				return b, err
			}
		case types.KindMysqlDuration:
			// duration may have negative value, so we cannot use String to encode directly.
			b = append(b, durationFlag)
			b = EncodeInt(b, int64(vals[i].GetMysqlDuration().Duration))
		case types.KindMysqlDecimal:
			b = append(b, decimalFlag)
			b, err = EncodeDecimal(b, vals[i].GetMysqlDecimal(), vals[i].Length(), vals[i].Frac())
			if terror.ErrorEqual(err, types.ErrTruncated) {
				err = sc.HandleTruncate(err)
			} else if terror.ErrorEqual(err, types.ErrOverflow) {
				err = sc.HandleOverflow(err, err)
			}
		case types.KindMysqlEnum:
			b = encodeUnsignedInt(b, vals[i].GetMysqlEnum().Value, comparable1)
		case types.KindMysqlSet:
			b = encodeUnsignedInt(b, vals[i].GetMysqlSet().Value, comparable1)
		case types.KindMysqlBit, types.KindBinaryLiteral:
			// We don't need to handle errors here since the literal is ensured to be able to store in uint64 in convertToMysqlBit.
			var val uint64
			val, err = vals[i].GetBinaryLiteral().ToInt(sc)
			terror.Log(errors.Trace(err))
			b = encodeUnsignedInt(b, val, comparable1)
		case types.KindMysqlJSON:
			b = append(b, jsonFlag)
			j := vals[i].GetMysqlJSON()
			b = append(b, j.TypeCode)
			b = append(b, j.Value...)
		case types.KindNull:
			b = append(b, NilFlag)
		case types.KindMinNotNull:
			b = append(b, bytesFlag)
		case types.KindMaxValue:
			b = append(b, maxFlag)
		default:
			return b, errors.Errorf("unsupport encode type %d", vals[i].Kind())
		}
	}

	return b, errors.Trace(err)
}

// EstimateValueSize uses to estimate the value  size of the encoded values.
func EstimateValueSize(sc *stmtctx.StatementContext, val types.Datum) (int, error) {
	l := 0
	switch val.Kind() {
	case types.KindInt64:
		l = valueSizeOfSignedInt(val.GetInt64())
	case types.KindUint64:
		l = valueSizeOfUnsignedInt(val.GetUint64())
	case types.KindFloat32, types.KindFloat64, types.KindMysqlTime, types.KindMysqlDuration:
		l = 9
	case types.KindString, types.KindBytes:
		l = valueSizeOfBytes(val.GetBytes())
	case types.KindMysqlDecimal:
		var err error
		l, err = valueSizeOfDecimal(val.GetMysqlDecimal(), val.Length(), val.Frac())
		if err != nil {
			return 0, err
		}
		l = l + 1
	case types.KindMysqlEnum:
		l = valueSizeOfUnsignedInt(val.GetMysqlEnum().Value)
	case types.KindMysqlSet:
		l = valueSizeOfUnsignedInt(val.GetMysqlSet().Value)
	case types.KindMysqlBit, types.KindBinaryLiteral:
		val, err := val.GetBinaryLiteral().ToInt(sc)
		terror.Log(errors.Trace(err))
		l = valueSizeOfUnsignedInt(val)
	case types.KindMysqlJSON:
		l = 2 + len(val.GetMysqlJSON().Value)
	case types.KindNull, types.KindMinNotNull, types.KindMaxValue:
		l = 1
	default:
		return l, errors.Errorf("unsupported encode type %d", val.Kind())
	}
	return l, nil
}

// EncodeMySQLTime encodes datum of `KindMysqlTime` to []byte.
func EncodeMySQLTime(sc *stmtctx.StatementContext, t types.Time, tp byte, b []byte) (_ []byte, err error) {
	// Encoding timestamp need to consider timezone. If it's not in UTC, transform to UTC first.
	// This is compatible with `PBToExpr > convertTime`, and coprocessor assumes the passed timestamp is in UTC as well.
	if tp == mysql.TypeUnspecified {
		tp = t.Type()
	}
	if tp == mysql.TypeTimestamp && sc.TimeZone != time.UTC {
		err = t.ConvertTimeZone(sc.TimeZone, time.UTC)
		if err != nil {
			return nil, err
		}
	}
	var v uint64
	v, err = t.ToPackedUint()
	if err != nil {
		return nil, err
	}
	b = EncodeUint(b, v)
	return b, nil
}

func encodeString(b []byte, val types.Datum, comparable1 bool) []byte {
	if collate.NewCollationEnabled() && comparable1 {
		return encodeBytes(b, collate.GetCollator(val.Collation()).Key(val.GetString()), true)
	}
	return encodeBytes(b, val.GetBytes(), comparable1)
}

func encodeBytes(b []byte, v []byte, comparable1 bool) []byte {
	if comparable1 {
		b = append(b, bytesFlag)
		b = EncodeBytes(b, v)
	} else {
		b = append(b, compactBytesFlag)
		b = EncodeCompactBytes(b, v)
	}
	return b
}

func valueSizeOfBytes(v []byte) int {
	return valueSizeOfSignedInt(int64(len(v))) + len(v)
}

func sizeBytes(v []byte, comparable1 bool) int {
	if comparable1 {
		reallocSize := (len(v)/encGroupSize + 1) * (encGroupSize + 1)
		return 1 + reallocSize
	}
	reallocSize := binary.MaxVarintLen64 + len(v)
	return 1 + reallocSize
}

func encodeSignedInt(b []byte, v int64, comparable1 bool) []byte {
	if comparable1 {
		b = append(b, intFlag)
		b = EncodeInt(b, v)
	} else {
		b = append(b, varintFlag)
		b = EncodeVarint(b, v)
	}
	return b
}

func valueSizeOfSignedInt(v int64) int {
	if v < 0 {
		v = 0 - v - 1
	}
	// flag occupy 1 bit and at lease 1 bit.
	size := 2
	v = v >> 6
	for v > 0 {
		size++
		v = v >> 7
	}
	return size
}

func encodeUnsignedInt(b []byte, v uint64, comparable1 bool) []byte {
	if comparable1 {
		b = append(b, uintFlag)
		b = EncodeUint(b, v)
	} else {
		b = append(b, uvarintFlag)
		b = EncodeUvarint(b, v)
	}
	return b
}

func valueSizeOfUnsignedInt(v uint64) int {
	// flag occupy 1 bit and at lease 1 bit.
	size := 2
	v = v >> 7
	for v > 0 {
		size++
		v = v >> 7
	}
	return size
}

func sizeInt(comparable1 bool) int {
	if comparable1 {
		return 9
	}
	return 1 + binary.MaxVarintLen64
}

// EncodeKey appends the encoded values to byte slice b, returns the appended
// slice. It guarantees the encoded value is in ascending order for comparison.
// For decimal type, datum must set datum's length and frac.
func EncodeKey(sc *stmtctx.StatementContext, b []byte, v ...types.Datum) ([]byte, error) {
	return encode(sc, b, v, true)
}

// EncodeValue appends the encoded values to byte slice b, returning the appended
// slice. It does not guarantee the order for comparison.
func EncodeValue(sc *stmtctx.StatementContext, b []byte, v ...types.Datum) ([]byte, error) {
	return encode(sc, b, v, false)
}

func encodeHashChunkRowIdx(sc *stmtctx.StatementContext, row chunk.Row, tp *types.FieldType, idx int) (flag byte, b []byte, err error) {
	if row.IsNull(idx) {
		flag = NilFlag
		return
	}
	switch tp.GetType() {
	case mysql.TypeTiny, mysql.TypeShort, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong, mysql.TypeYear:
		flag = uvarintFlag
		if !mysql.HasUnsignedFlag(tp.GetFlag()) && row.GetInt64(idx) < 0 {
			flag = varintFlag
		}
		b = row.GetRaw(idx)
	case mysql.TypeFloat:
		flag = floatFlag
		f := float64(row.GetFloat32(idx))
		// For negative zero. In memory, 0 is [0, 0, 0, 0, 0, 0, 0, 0] and -0 is [0, 0, 0, 0, 0, 0, 0, 128].
		// It makes -0's hash val different from 0's.
		if f == 0 {
			f = 0
		}
		b = unsafe.Slice((*byte)(unsafe.Pointer(&f)), unsafe.Sizeof(f))
	case mysql.TypeDouble:
		flag = floatFlag
		f := row.GetFloat64(idx)
		// For negative zero. In memory, 0 is [0, 0, 0, 0, 0, 0, 0, 0] and -0 is [0, 0, 0, 0, 0, 0, 0, 128].
		// It makes -0's hash val different from 0's.
		if f == 0 {
			f = 0
		}
		b = unsafe.Slice((*byte)(unsafe.Pointer(&f)), unsafe.Sizeof(f))
	case mysql.TypeVarchar, mysql.TypeVarString, mysql.TypeString, mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob:
		flag = compactBytesFlag
		b = row.GetBytes(idx)
		b = ConvertByCollation(b, tp)
	case mysql.TypeDate, mysql.TypeDatetime, mysql.TypeTimestamp:
		flag = uintFlag
		t := row.GetTime(idx)

		var v uint64
		v, err = t.ToPackedUint()
		if err != nil {
			return
		}
		b = unsafe.Slice((*byte)(unsafe.Pointer(&v)), unsafe.Sizeof(v))
	case mysql.TypeDuration:
		flag = durationFlag
		// duration may have negative value, so we cannot use String to encode directly.
		b = row.GetRaw(idx)
	case mysql.TypeNewDecimal:
		flag = decimalFlag
		// If hash is true, we only consider the original value of this decimal and ignore it's precision.
		dec := row.GetMyDecimal(idx)
		b, err = dec.ToHashKey()
		if err != nil {
			return
		}
	case mysql.TypeEnum:
		if mysql.HasEnumSetAsIntFlag(tp.GetFlag()) {
			flag = uvarintFlag
			v := row.GetEnum(idx).Value
			b = unsafe.Slice((*byte)(unsafe.Pointer(&v)), sizeUint64)
		} else {
			flag = compactBytesFlag
			v := row.GetEnum(idx).Value
			str := ""
			if enum, err := types.ParseEnumValue(tp.GetElems(), v); err == nil {
				// str will be empty string if v out of definition of enum.
				str = enum.Name
			}
			b = ConvertByCollation(hack.Slice(str), tp)
		}
	case mysql.TypeSet:
		flag = compactBytesFlag
		s, err := types.ParseSetValue(tp.GetElems(), row.GetSet(idx).Value)
		if err != nil {
			return 0, nil, err
		}
		b = ConvertByCollation(hack.Slice(s.Name), tp)
	case mysql.TypeBit:
		// We don't need to handle errors here since the literal is ensured to be able to store in uint64 in convertToMysqlBit.
		flag = uvarintFlag
		v, err1 := types.BinaryLiteral(row.GetBytes(idx)).ToInt(sc)
		terror.Log(errors.Trace(err1))
		b = unsafe.Slice((*byte)(unsafe.Pointer(&v)), unsafe.Sizeof(v))
	case mysql.TypeJSON:
		flag = jsonFlag
		json := row.GetJSON(idx)
		b = json.HashValue(b)
	default:
		return 0, nil, errors.Errorf("unsupport column type for encode %d", tp.GetType())
	}
	return
}

// HashChunkColumns writes the encoded value of each row's column, which of index `colIdx`, to h.
func HashChunkColumns(sc *stmtctx.StatementContext, h []hash.Hash64, chk *chunk.Chunk, tp *types.FieldType, colIdx int, buf []byte, isNull []bool) (err error) {
	return HashChunkSelected(sc, h, chk, tp, colIdx, buf, isNull, nil, false)
}

// HashChunkSelected writes the encoded value of selected row's column, which of index `colIdx`, to h.
// sel indicates which rows are selected. If it is nil, all rows are selected.
func HashChunkSelected(sc *stmtctx.StatementContext, h []hash.Hash64, chk *chunk.Chunk, tp *types.FieldType, colIdx int, buf []byte,
	isNull, sel []bool, ignoreNull bool) (err error) {
	var b []byte
	column := chk.Column(colIdx)
	rows := chk.NumRows()
	switch tp.GetType() {
	case mysql.TypeTiny, mysql.TypeShort, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong, mysql.TypeYear:
		i64s := column.Int64s()
		for i, v := range i64s {
			if sel != nil && !sel[i] {
				continue
			}
			if column.IsNull(i) {
				buf[0], b = NilFlag, nil
				isNull[i] = !ignoreNull
			} else {
				buf[0] = uvarintFlag
				if !mysql.HasUnsignedFlag(tp.GetFlag()) && v < 0 {
					buf[0] = varintFlag
				}
				b = column.GetRaw(i)
			}

			// As the golang doc described, `Hash.Write` never returns an error.
			// See https://golang.org/pkg/hash/#Hash
			_, _ = h[i].Write(buf)
			_, _ = h[i].Write(b)
		}
	case mysql.TypeFloat:
		f32s := column.Float32s()
		for i, f := range f32s {
			if sel != nil && !sel[i] {
				continue
			}
			if column.IsNull(i) {
				buf[0], b = NilFlag, nil
				isNull[i] = !ignoreNull
			} else {
				buf[0] = floatFlag
				d := float64(f)
				// For negative zero. In memory, 0 is [0, 0, 0, 0, 0, 0, 0, 0] and -0 is [0, 0, 0, 0, 0, 0, 0, 128].
				// It makes -0's hash val different from 0's.
				if d == 0 {
					d = 0
				}
				b = unsafe.Slice((*byte)(unsafe.Pointer(&d)), sizeFloat64)
			}

			// As the golang doc described, `Hash.Write` never returns an error.
			// See https://golang.org/pkg/hash/#Hash
			_, _ = h[i].Write(buf)
			_, _ = h[i].Write(b)
		}
	case mysql.TypeDouble:
		f64s := column.Float64s()
		for i := range f64s {
			f := f64s[i]
			if sel != nil && !sel[i] {
				continue
			}
			if column.IsNull(i) {
				buf[0], b = NilFlag, nil
				isNull[i] = !ignoreNull
			} else {
				buf[0] = floatFlag
				// For negative zero. In memory, 0 is [0, 0, 0, 0, 0, 0, 0, 0] and -0 is [0, 0, 0, 0, 0, 0, 0, 128].
				// It makes -0's hash val different from 0's.
				if f == 0 {
					f = 0
				}
				b = unsafe.Slice((*byte)(unsafe.Pointer(&f)), sizeFloat64)
			}

			// As the golang doc described, `Hash.Write` never returns an error.
			// See https://golang.org/pkg/hash/#Hash
			_, _ = h[i].Write(buf)
			_, _ = h[i].Write(b)
		}
	case mysql.TypeVarchar, mysql.TypeVarString, mysql.TypeString, mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob:
		for i := 0; i < rows; i++ {
			if sel != nil && !sel[i] {
				continue
			}
			if column.IsNull(i) {
				buf[0], b = NilFlag, nil
				isNull[i] = !ignoreNull
			} else {
				buf[0] = compactBytesFlag
				b = column.GetBytes(i)
				b = ConvertByCollation(b, tp)
			}

			// As the golang doc described, `Hash.Write` never returns an error.
			// See https://golang.org/pkg/hash/#Hash
			_, _ = h[i].Write(buf)
			_, _ = h[i].Write(b)
		}
	case mysql.TypeDate, mysql.TypeDatetime, mysql.TypeTimestamp:
		ts := column.Times()
		for i, t := range ts {
			if sel != nil && !sel[i] {
				continue
			}
			if column.IsNull(i) {
				buf[0], b = NilFlag, nil
				isNull[i] = !ignoreNull
			} else {
				buf[0] = uintFlag

				var v uint64
				v, err = t.ToPackedUint()
				if err != nil {
					return
				}
				b = unsafe.Slice((*byte)(unsafe.Pointer(&v)), sizeUint64)
			}

			// As the golang doc described, `Hash.Write` never returns an error.
			// See https://golang.org/pkg/hash/#Hash
			_, _ = h[i].Write(buf)
			_, _ = h[i].Write(b)
		}
	case mysql.TypeDuration:
		for i := 0; i < rows; i++ {
			if sel != nil && !sel[i] {
				continue
			}
			if column.IsNull(i) {
				buf[0], b = NilFlag, nil
				isNull[i] = !ignoreNull
			} else {
				buf[0] = durationFlag
				// duration may have negative value, so we cannot use String to encode directly.
				b = column.GetRaw(i)
			}

			// As the golang doc described, `Hash.Write` never returns an error.
			// See https://golang.org/pkg/hash/#Hash
			_, _ = h[i].Write(buf)
			_, _ = h[i].Write(b)
		}
	case mysql.TypeNewDecimal:
		ds := column.Decimals()
		for i, d := range ds {
			if sel != nil && !sel[i] {
				continue
			}
			if column.IsNull(i) {
				buf[0], b = NilFlag, nil
				isNull[i] = !ignoreNull
			} else {
				buf[0] = decimalFlag
				// If hash is true, we only consider the original value of this decimal and ignore it's precision.
				b, err = d.ToHashKey()
				if err != nil {
					return
				}
			}

			// As the golang doc described, `Hash.Write` never returns an error.
			// See https://golang.org/pkg/hash/#Hash
			_, _ = h[i].Write(buf)
			_, _ = h[i].Write(b)
		}
	case mysql.TypeEnum:
		for i := 0; i < rows; i++ {
			if sel != nil && !sel[i] {
				continue
			}
			if column.IsNull(i) {
				buf[0], b = NilFlag, nil
				isNull[i] = !ignoreNull
			} else if mysql.HasEnumSetAsIntFlag(tp.GetFlag()) {
				buf[0] = uvarintFlag
				v := column.GetEnum(i).Value
				b = unsafe.Slice((*byte)(unsafe.Pointer(&v)), sizeUint64)
			} else {
				buf[0] = compactBytesFlag
				v := column.GetEnum(i).Value
				str := ""
				if enum, err := types.ParseEnumValue(tp.GetElems(), v); err == nil {
					// str will be empty string if v out of definition of enum.
					str = enum.Name
				}
				b = ConvertByCollation(hack.Slice(str), tp)
			}

			// As the golang doc described, `Hash.Write` never returns an error.
			// See https://golang.org/pkg/hash/#Hash
			_, _ = h[i].Write(buf)
			_, _ = h[i].Write(b)
		}
	case mysql.TypeSet:
		for i := 0; i < rows; i++ {
			if sel != nil && !sel[i] {
				continue
			}
			if column.IsNull(i) {
				buf[0], b = NilFlag, nil
				isNull[i] = !ignoreNull
			} else {
				buf[0] = compactBytesFlag
				s, err := types.ParseSetValue(tp.GetElems(), column.GetSet(i).Value)
				if err != nil {
					return err
				}
				b = ConvertByCollation(hack.Slice(s.Name), tp)
			}

			// As the golang doc described, `Hash.Write` never returns an error.
			// See https://golang.org/pkg/hash/#Hash
			_, _ = h[i].Write(buf)
			_, _ = h[i].Write(b)
		}
	case mysql.TypeBit:
		for i := 0; i < rows; i++ {
			if sel != nil && !sel[i] {
				continue
			}
			if column.IsNull(i) {
				buf[0], b = NilFlag, nil
				isNull[i] = !ignoreNull
			} else {
				// We don't need to handle errors here since the literal is ensured to be able to store in uint64 in convertToMysqlBit.
				buf[0] = uvarintFlag
				v, err1 := types.BinaryLiteral(column.GetBytes(i)).ToInt(sc)
				terror.Log(errors.Trace(err1))
				b = unsafe.Slice((*byte)(unsafe.Pointer(&v)), sizeUint64)
			}

			// As the golang doc described, `Hash.Write` never returns an error.
			// See https://golang.org/pkg/hash/#Hash
			_, _ = h[i].Write(buf)
			_, _ = h[i].Write(b)
		}
	case mysql.TypeJSON:
		for i := 0; i < rows; i++ {
			if sel != nil && !sel[i] {
				continue
			}
			if column.IsNull(i) {
				buf[0], b = NilFlag, nil
				isNull[i] = !ignoreNull
			} else {
				buf[0] = jsonFlag
				json := column.GetJSON(i)
				b = b[:0]
				b = json.HashValue(b)
			}

			// As the golang doc described, `Hash.Write` never returns an error..
			// See https://golang.org/pkg/hash/#Hash
			_, _ = h[i].Write(buf)
			_, _ = h[i].Write(b)
		}
	case mysql.TypeNull:
		for i := 0; i < rows; i++ {
			if sel != nil && !sel[i] {
				continue
			}
			isNull[i] = !ignoreNull
			buf[0] = NilFlag
			_, _ = h[i].Write(buf)
		}
	default:
		return errors.Errorf("unsupport column type for encode %d", tp.GetType())
	}
	return
}

// HashChunkRow writes the encoded values to w.
// If two rows are logically equal, it will generate the same bytes.
func HashChunkRow(sc *stmtctx.StatementContext, w io.Writer, row chunk.Row, allTypes []*types.FieldType, colIdx []int, buf []byte) (err error) {
	var b []byte
	for i, idx := range colIdx {
		buf[0], b, err = encodeHashChunkRowIdx(sc, row, allTypes[i], idx)
		if err != nil {
			return errors.Trace(err)
		}
		_, err = w.Write(buf)
		if err != nil {
			return
		}
		_, err = w.Write(b)
		if err != nil {
			return
		}
	}
	return err
}

// EqualChunkRow returns a boolean reporting whether row1 and row2
// with their types and column index are logically equal.
func EqualChunkRow(sc *stmtctx.StatementContext,
	row1 chunk.Row, allTypes1 []*types.FieldType, colIdx1 []int,
	row2 chunk.Row, allTypes2 []*types.FieldType, colIdx2 []int,
) (bool, error) {
	if len(colIdx1) != len(colIdx2) {
		return false, errors.Errorf("Internal error: Hash columns count mismatch, col1: %d, col2: %d", len(colIdx1), len(colIdx2))
	}
	for i := range colIdx1 {
		idx1, idx2 := colIdx1[i], colIdx2[i]
		flag1, b1, err := encodeHashChunkRowIdx(sc, row1, allTypes1[i], idx1)
		if err != nil {
			return false, errors.Trace(err)
		}
		flag2, b2, err := encodeHashChunkRowIdx(sc, row2, allTypes2[i], idx2)
		if err != nil {
			return false, errors.Trace(err)
		}
		if !(flag1 == flag2 && bytes.Equal(b1, b2)) {
			return false, nil
		}
	}
	return true, nil
}

// Decode decodes values from a byte slice generated with EncodeKey or EncodeValue
// before.
// size is the size of decoded datum slice.
func Decode(b []byte, size int) ([]types.Datum, error) {
	if len(b) < 1 {
		return nil, errors.New("invalid encoded key")
	}

	var (
		err    error
		values = make([]types.Datum, 0, size)
	)

	for len(b) > 0 {
		var d types.Datum
		b, d, err = DecodeOne(b)
		if err != nil {
			return nil, errors.Trace(err)
		}

		values = append(values, d)
	}

	return values, nil
}

// DecodeRange decodes the range values from a byte slice that generated by EncodeKey.
// It handles some special values like `MinNotNull` and `MaxValueDatum`.
// loc can be nil and only used in when the corresponding type is `mysql.TypeTimestamp`.
func DecodeRange(b []byte, size int, idxColumnTypes []byte, loc *time.Location) ([]types.Datum, []byte, error) {
	if len(b) < 1 {
		return nil, b, errors.New("invalid encoded key: length of key is zero")
	}

	var (
		err    error
		values = make([]types.Datum, 0, size)
	)

	i := 0
	for len(b) > 1 {
		var d types.Datum
		if idxColumnTypes == nil {
			b, d, err = DecodeOne(b)
		} else {
			if i >= len(idxColumnTypes) {
				return values, b, errors.New("invalid length of index's columns")
			}
			if types.IsTypeTime(idxColumnTypes[i]) {
				// handle datetime values specially since they are encoded to int and we'll get int values if using DecodeOne.
				b, d, err = DecodeAsDateTime(b, idxColumnTypes[i], loc)
			} else if types.IsTypeFloat(idxColumnTypes[i]) {
				b, d, err = DecodeAsFloat32(b, idxColumnTypes[i])
			} else {
				b, d, err = DecodeOne(b)
			}
		}
		if err != nil {
			return values, b, errors.Trace(err)
		}
		values = append(values, d)
		i++
	}

	if len(b) == 1 {
		switch b[0] {
		case NilFlag:
			values = append(values, types.Datum{})
		case bytesFlag:
			values = append(values, types.MinNotNullDatum())
		// `maxFlag + 1` for PrefixNext
		case maxFlag, maxFlag + 1:
			values = append(values, types.MaxValueDatum())
		default:
			return values, b, errors.Errorf("invalid encoded key flag %v", b[0])
		}
	}
	return values, nil, nil
}

// DecodeOne decodes on datum from a byte slice generated with EncodeKey or EncodeValue.
func DecodeOne(b []byte) (remain []byte, d types.Datum, err error) {
	if len(b) < 1 {
		return nil, d, errors.New("invalid encoded key")
	}
	flag := b[0]
	b = b[1:]
	switch flag {
	case intFlag:
		var v int64
		b, v, err = DecodeInt(b)
		d.SetInt64(v)
	case uintFlag:
		var v uint64
		b, v, err = DecodeUint(b)
		d.SetUint64(v)
	case varintFlag:
		var v int64
		b, v, err = DecodeVarint(b)
		d.SetInt64(v)
	case uvarintFlag:
		var v uint64
		b, v, err = DecodeUvarint(b)
		d.SetUint64(v)
	case floatFlag:
		var v float64
		b, v, err = DecodeFloat(b)
		d.SetFloat64(v)
	case bytesFlag:
		var v []byte
		b, v, err = DecodeBytes(b, nil)
		d.SetBytes(v)
	case compactBytesFlag:
		var v []byte
		b, v, err = DecodeCompactBytes(b)
		d.SetBytes(v)
	case decimalFlag:
		var (
			dec             *types.MyDecimal
			precision, frac int
		)
		b, dec, precision, frac, err = DecodeDecimal(b)
		if err == nil {
			d.SetMysqlDecimal(dec)
			d.SetLength(precision)
			d.SetFrac(frac)
		}
	case durationFlag:
		var r int64
		b, r, err = DecodeInt(b)
		if err == nil {
			// use max fsp, let outer to do round manually.
			v := types.Duration{Duration: time.Duration(r), Fsp: types.MaxFsp}
			d.SetMysqlDuration(v)
		}
	case jsonFlag:
		var size int
		size, err = types.PeekBytesAsJSON(b)
		if err != nil {
			return b, d, err
		}
		j := types.BinaryJSON{TypeCode: b[0], Value: b[1:size]}
		d.SetMysqlJSON(j)
		b = b[size:]
	case NilFlag:
	default:
		return b, d, errors.Errorf("invalid encoded key flag %v", flag)
	}
	if err != nil {
		return b, d, errors.Trace(err)
	}
	return b, d, nil
}

// DecodeAsDateTime decodes on datum from []byte of `KindMysqlTime`.
func DecodeAsDateTime(b []byte, tp byte, loc *time.Location) (remain []byte, d types.Datum, err error) {
	if len(b) < 1 {
		return nil, d, errors.New("invalid encoded key")
	}
	flag := b[0]
	b = b[1:]
	var v uint64
	switch flag {
	case uintFlag:
		b, v, err = DecodeUint(b)
	case uvarintFlag:
		// Datetime can be encoded as Uvarint
		b, v, err = DecodeUvarint(b)

	default:
		return b, d, errors.Errorf("invalid encoded key flag %v", flag)
	}
	if err != nil {
		return b, d, err
	}
	t := types.NewTime(types.ZeroCoreTime, tp, 0)
	err = t.FromPackedUint(v)
	if err != nil {
		return b, d, errors.Trace(err)
	}
	if tp == mysql.TypeTimestamp && !t.IsZero() && loc != nil {
		err = t.ConvertTimeZone(time.UTC, loc)
		if err != nil {
			return b, d, err
		}
	}
	d.SetMysqlTime(t)
	return b, d, nil
}

// DecodeAsFloat32 decodes value for mysql.TypeFloat
func DecodeAsFloat32(b []byte, tp byte) (remain []byte, d types.Datum, err error) {
	if len(b) < 1 || tp != mysql.TypeFloat {
		return nil, d, errors.New("invalid encoded key")
	}
	flag := b[0]
	b = b[1:]
	if flag != floatFlag {
		return b, d, errors.Errorf("invalid encoded key flag %v for DecodeAsFloat32", flag)
	}
	var v float64
	b, v, err = DecodeFloat(b)
	d.SetFloat32FromF64(v)
	return b, d, nil
}

// CutOne cuts the first encoded value from b.
// It will return the first encoded item and the remains as byte slice.
func CutOne(b []byte) (data []byte, remain []byte, err error) {
	l, err := peek(b)
	if err != nil {
		return nil, nil, errors.Trace(err)
	}
	return b[:l], b[l:], nil
}

// CutColumnID cuts the column ID from b.
// It will return the remains as byte slice and column ID
func CutColumnID(b []byte) (remain []byte, n int64, err error) {
	if len(b) < 1 {
		return nil, 0, errors.New("invalid encoded key")
	}
	// skip the flag
	b = b[1:]
	return DecodeVarint(b)
}

// SetRawValues set raw datum values from a row data.
func SetRawValues(data []byte, values []types.Datum) error {
	for i := 0; i < len(values); i++ {
		l, err := peek(data)
		if err != nil {
			return errors.Trace(err)
		}
		values[i].SetRaw(data[:l:l])
		data = data[l:]
	}
	return nil
}

// peek peeks the first encoded value from b and returns its length.
func peek(b []byte) (length int, err error) {
	originLength := len(b)
	if len(b) < 1 {
		return 0, errors.New("invalid encoded key")
	}
	flag := b[0]
	length++
	b = b[1:]
	var l int
	switch flag {
	case NilFlag:
	case intFlag, uintFlag, floatFlag, durationFlag:
		// Those types are stored in 8 bytes.
		l = 8
	case bytesFlag:
		l, err = peekBytes(b)
	case compactBytesFlag:
		l, err = peekCompactBytes(b)
	case decimalFlag:
		l, err = types.DecimalPeak(b)
	case varintFlag:
		l, err = peekVarint(b)
	case uvarintFlag:
		l, err = peekUvarint(b)
	case jsonFlag:
		l, err = types.PeekBytesAsJSON(b)
	default:
		return 0, errors.Errorf("invalid encoded key flag %v", flag)
	}
	if err != nil {
		return 0, errors.Trace(err)
	}
	length += l
	if length <= 0 {
		return 0, errors.New("invalid encoded key")
	} else if length > originLength {
		return 0, errors.Errorf("invalid encoded key, "+
			"expected length: %d, actual length: %d", length, originLength)
	}
	return
}

func peekBytes(b []byte) (int, error) {
	offset := 0
	for {
		if len(b) < offset+encGroupSize+1 {
			return 0, errors.New("insufficient bytes to decode value")
		}
		// The byte slice is encoded into many groups.
		// For each group, there are 8 bytes for data and 1 byte for marker.
		marker := b[offset+encGroupSize]
		padCount := encMarker - marker
		offset += encGroupSize + 1
		// When padCount is not zero, it means we get the end of the byte slice.
		if padCount != 0 {
			break
		}
	}
	return offset, nil
}

func peekCompactBytes(b []byte) (int, error) {
	// Get length.
	v, n := binary.Varint(b)
	vi := int(v)
	if n < 0 {
		return 0, errors.New("value larger than 64 bits")
	} else if n == 0 {
		return 0, errors.New("insufficient bytes to decode value")
	}
	if len(b) < vi+n {
		return 0, errors.Errorf("insufficient bytes to decode value, expected length: %v", n)
	}
	return n + vi, nil
}

func peekVarint(b []byte) (int, error) {
	_, n := binary.Varint(b)
	if n < 0 {
		return 0, errors.New("value larger than 64 bits")
	}
	return n, nil
}

func peekUvarint(b []byte) (int, error) {
	_, n := binary.Uvarint(b)
	if n < 0 {
		return 0, errors.New("value larger than 64 bits")
	}
	return n, nil
}

// Decoder is used to decode value to chunk.
type Decoder struct {
	chk      *chunk.Chunk
	timezone *time.Location

	// buf is only used for DecodeBytes to avoid the cost of makeslice.
	buf []byte
}

// NewDecoder creates a Decoder.
func NewDecoder(chk *chunk.Chunk, timezone *time.Location) *Decoder {
	return &Decoder{
		chk:      chk,
		timezone: timezone,
	}
}

// DecodeOne decodes one value to chunk and returns the remained bytes.
func (decoder *Decoder) DecodeOne(b []byte, colIdx int, ft *types.FieldType) (remain []byte, err error) {
	if len(b) < 1 {
		return nil, errors.New("invalid encoded key")
	}
	chk := decoder.chk
	flag := b[0]
	b = b[1:]
	switch flag {
	case intFlag:
		var v int64
		b, v, err = DecodeInt(b)
		if err != nil {
			return nil, errors.Trace(err)
		}
		appendIntToChunk(v, chk, colIdx, ft)
	case uintFlag:
		var v uint64
		b, v, err = DecodeUint(b)
		if err != nil {
			return nil, errors.Trace(err)
		}
		err = appendUintToChunk(v, chk, colIdx, ft, decoder.timezone)
	case varintFlag:
		var v int64
		b, v, err = DecodeVarint(b)
		if err != nil {
			return nil, errors.Trace(err)
		}
		appendIntToChunk(v, chk, colIdx, ft)
	case uvarintFlag:
		var v uint64
		b, v, err = DecodeUvarint(b)
		if err != nil {
			return nil, errors.Trace(err)
		}
		err = appendUintToChunk(v, chk, colIdx, ft, decoder.timezone)
	case floatFlag:
		var v float64
		b, v, err = DecodeFloat(b)
		if err != nil {
			return nil, errors.Trace(err)
		}
		appendFloatToChunk(v, chk, colIdx, ft)
	case bytesFlag:
		b, decoder.buf, err = DecodeBytes(b, decoder.buf)
		if err != nil {
			return nil, errors.Trace(err)
		}
		chk.AppendBytes(colIdx, decoder.buf)
	case compactBytesFlag:
		var v []byte
		b, v, err = DecodeCompactBytes(b)
		if err != nil {
			return nil, errors.Trace(err)
		}
		chk.AppendBytes(colIdx, v)
	case decimalFlag:
		var dec *types.MyDecimal
		var frac int
		b, dec, _, frac, err = DecodeDecimal(b)
		if err != nil {
			return nil, errors.Trace(err)
		}
		if ft.GetDecimal() != types.UnspecifiedLength && frac > ft.GetDecimal() {
			to := new(types.MyDecimal)
			err := dec.Round(to, ft.GetDecimal(), types.ModeHalfUp)
			if err != nil {
				return nil, errors.Trace(err)
			}
			dec = to
		}
		chk.AppendMyDecimal(colIdx, dec)
	case durationFlag:
		var r int64
		b, r, err = DecodeInt(b)
		if err != nil {
			return nil, errors.Trace(err)
		}
		v := types.Duration{Duration: time.Duration(r), Fsp: ft.GetDecimal()}
		chk.AppendDuration(colIdx, v)
	case jsonFlag:
		var size int
		size, err = types.PeekBytesAsJSON(b)
		if err != nil {
			return nil, errors.Trace(err)
		}
		chk.AppendJSON(colIdx, types.BinaryJSON{TypeCode: b[0], Value: b[1:size]})
		b = b[size:]
	case NilFlag:
		chk.AppendNull(colIdx)
	default:
		return nil, errors.Errorf("invalid encoded key flag %v", flag)
	}
	if err != nil {
		return nil, errors.Trace(err)
	}
	return b, nil
}

func appendIntToChunk(val int64, chk *chunk.Chunk, colIdx int, ft *types.FieldType) {
	switch ft.GetType() {
	case mysql.TypeDuration:
		v := types.Duration{Duration: time.Duration(val), Fsp: ft.GetDecimal()}
		chk.AppendDuration(colIdx, v)
	default:
		chk.AppendInt64(colIdx, val)
	}
}

func appendUintToChunk(val uint64, chk *chunk.Chunk, colIdx int, ft *types.FieldType, loc *time.Location) error {
	switch ft.GetType() {
	case mysql.TypeDate, mysql.TypeDatetime, mysql.TypeTimestamp:
		t := types.NewTime(types.ZeroCoreTime, ft.GetType(), ft.GetDecimal())
		var err error
		err = t.FromPackedUint(val)
		if err != nil {
			return errors.Trace(err)
		}
		if ft.GetType() == mysql.TypeTimestamp && !t.IsZero() {
			err = t.ConvertTimeZone(time.UTC, loc)
			if err != nil {
				return errors.Trace(err)
			}
		}
		chk.AppendTime(colIdx, t)
	case mysql.TypeEnum:
		// ignore error deliberately, to read empty enum value.
		enum, err := types.ParseEnumValue(ft.GetElems(), val)
		if err != nil {
			enum = types.Enum{}
		}
		chk.AppendEnum(colIdx, enum)
	case mysql.TypeSet:
		set, err := types.ParseSetValue(ft.GetElems(), val)
		if err != nil {
			return errors.Trace(err)
		}
		chk.AppendSet(colIdx, set)
	case mysql.TypeBit:
		byteSize := (ft.GetFlen() + 7) >> 3
		chk.AppendBytes(colIdx, types.NewBinaryLiteralFromUint(val, byteSize))
	default:
		chk.AppendUint64(colIdx, val)
	}
	return nil
}

func appendFloatToChunk(val float64, chk *chunk.Chunk, colIdx int, ft *types.FieldType) {
	if ft.GetType() == mysql.TypeFloat {
		chk.AppendFloat32(colIdx, float32(val))
	} else {
		chk.AppendFloat64(colIdx, val)
	}
}

// HashGroupKey encodes each row of this column and append encoded data into buf.
// Only use in the aggregate executor.
func HashGroupKey(sc *stmtctx.StatementContext, n int, col *chunk.Column, buf [][]byte, ft *types.FieldType) ([][]byte, error) {
	var err error
	switch ft.EvalType() {
	case types.ETInt:
		i64s := col.Int64s()
		for i := 0; i < n; i++ {
			if col.IsNull(i) {
				buf[i] = append(buf[i], NilFlag)
			} else {
				buf[i] = encodeSignedInt(buf[i], i64s[i], false)
			}
		}
	case types.ETReal:
		f64s := col.Float64s()
		for i := 0; i < n; i++ {
			if col.IsNull(i) {
				buf[i] = append(buf[i], NilFlag)
			} else {
				buf[i] = append(buf[i], floatFlag)
				buf[i] = EncodeFloat(buf[i], f64s[i])
			}
		}
	case types.ETDecimal:
		ds := col.Decimals()
		for i := 0; i < n; i++ {
			if col.IsNull(i) {
				buf[i] = append(buf[i], NilFlag)
			} else {
				buf[i] = append(buf[i], decimalFlag)
				buf[i], err = EncodeDecimal(buf[i], &ds[i], ft.GetFlen(), ft.GetDecimal())
				if terror.ErrorEqual(err, types.ErrTruncated) {
					err = sc.HandleTruncate(err)
				} else if terror.ErrorEqual(err, types.ErrOverflow) {
					err = sc.HandleOverflow(err, err)
				}
				if err != nil {
					return nil, err
				}
			}
		}
	case types.ETDatetime, types.ETTimestamp:
		ts := col.Times()
		for i := 0; i < n; i++ {
			if col.IsNull(i) {
				buf[i] = append(buf[i], NilFlag)
			} else {
				buf[i] = append(buf[i], uintFlag)
				buf[i], err = EncodeMySQLTime(sc, ts[i], mysql.TypeUnspecified, buf[i])
				if err != nil {
					return nil, err
				}
			}
		}
	case types.ETDuration:
		ds := col.GoDurations()
		for i := 0; i < n; i++ {
			if col.IsNull(i) {
				buf[i] = append(buf[i], NilFlag)
			} else {
				buf[i] = append(buf[i], durationFlag)
				buf[i] = EncodeInt(buf[i], int64(ds[i]))
			}
		}
	case types.ETJson:
		for i := 0; i < n; i++ {
			if col.IsNull(i) {
				buf[i] = append(buf[i], NilFlag)
			} else {
				buf[i] = append(buf[i], jsonFlag)
				buf[i] = col.GetJSON(i).HashValue(buf[i])
			}
		}
	case types.ETString:
		for i := 0; i < n; i++ {
			if col.IsNull(i) {
				buf[i] = append(buf[i], NilFlag)
			} else {
				buf[i] = encodeBytes(buf[i], ConvertByCollation(col.GetBytes(i), ft), false)
			}
		}
	default:
		return nil, fmt.Errorf("invalid eval type %v", ft.EvalType())
	}
	return buf, nil
}

// ConvertByCollation converts these bytes according to its collation.
func ConvertByCollation(raw []byte, tp *types.FieldType) []byte {
	collator := collate.GetCollator(tp.GetCollate())
	return collator.Key(string(hack.String(raw)))
}

// ConvertByCollationStr converts this string according to its collation.
func ConvertByCollationStr(str string, tp *types.FieldType) string {
	collator := collate.GetCollator(tp.GetCollate())
	return string(hack.String(collator.Key(str)))
}

// HashCode encodes a Datum into a unique byte slice.
// It is mostly the same as EncodeValue, but it doesn't contain truncation or verification logic in order to make the encoding lossless.
func HashCode(b []byte, d types.Datum) []byte {
	switch d.Kind() {
	case types.KindInt64:
		b = encodeSignedInt(b, d.GetInt64(), false)
	case types.KindUint64:
		b = encodeUnsignedInt(b, d.GetUint64(), false)
	case types.KindFloat32, types.KindFloat64:
		b = append(b, floatFlag)
		b = EncodeFloat(b, d.GetFloat64())
	case types.KindString:
		b = encodeString(b, d, false)
	case types.KindBytes:
		b = encodeBytes(b, d.GetBytes(), false)
	case types.KindMysqlTime:
		b = append(b, uintFlag)
		t := d.GetMysqlTime().CoreTime()
		b = encodeUnsignedInt(b, uint64(t), true)
	case types.KindMysqlDuration:
		// duration may have negative value, so we cannot use String to encode directly.
		b = append(b, durationFlag)
		b = EncodeInt(b, int64(d.GetMysqlDuration().Duration))
	case types.KindMysqlDecimal:
		b = append(b, decimalFlag)
		decStr := d.GetMysqlDecimal().ToString()
		b = encodeBytes(b, decStr, false)
	case types.KindMysqlEnum:
		b = encodeUnsignedInt(b, d.GetMysqlEnum().Value, false)
	case types.KindMysqlSet:
		b = encodeUnsignedInt(b, d.GetMysqlSet().Value, false)
	case types.KindMysqlBit, types.KindBinaryLiteral:
		val := d.GetBinaryLiteral()
		b = encodeBytes(b, val, false)
	case types.KindMysqlJSON:
		b = append(b, jsonFlag)
		j := d.GetMysqlJSON()
		b = append(b, j.TypeCode)
		b = append(b, j.Value...)
	case types.KindNull:
		b = append(b, NilFlag)
	case types.KindMinNotNull:
		b = append(b, bytesFlag)
	case types.KindMaxValue:
		b = append(b, maxFlag)
	default:
		logutil.BgLogger().Warn("trying to calculate HashCode of an unexpected type of Datum",
			zap.Uint8("Datum Kind", d.Kind()),
			zap.Stack("stack"))
	}
	return b
}


text:
// *****************************************************************************
//
//  © Component Factory Pty Ltd 2017. All rights reserved.
//	The software and associated documentation supplied hereunder are the
//  proprietary information of Component Factory Pty Ltd, 13 Swallows Close,
//  Mornington, Vic 3931, Australia and are supplied subject to licence terms.
//
//  Version 4.6.0.0 	www.ComponentFactory.com
// *****************************************************************************

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Text;

namespace ComponentFactory.Krypton.Toolkit
{
    #region IMouseController
    /// <summary>
	/// Interface for processing mouse notifications.
	/// </summary>
	public interface IMouseController
	{
		/// <summary>
		/// Mouse has entered the view.
		/// </summary>
        /// <param name="c">Reference to the source control instance.</param>
		void MouseEnter(Control c);

		/// <summary>
		/// Mouse has moved inside the view.
		/// </summary>
        /// <param name="c">Reference to the source control instance.</param>
		/// <param name="pt">Mouse position relative to control.</param>
		void MouseMove(Control c, Point pt);

		/// <summary>
		/// Mouse button has been pressed in the view.
		/// </summary>
        /// <param name="c">Reference to the source control instance.</param>
		/// <param name="pt">Mouse position relative to control.</param>
		/// <param name="button">Mouse button pressed down.</param>
		/// <returns>True if capturing input; otherwise false.</returns>
		bool MouseDown(Control c, Point pt, MouseButtons button);

		/// <summary>
		/// Mouse button has been released in the view.
		/// </summary>
        /// <param name="c">Reference to the source control instance.</param>
		/// <param name="pt">Mouse position relative to control.</param>
		/// <param name="button">Mouse button released.</param>
		void MouseUp(Control c, Point pt, MouseButtons button);

		/// <summary>
		/// Mouse has left the view.
		/// </summary>
        /// <param name="c">Reference to the source control instance.</param>
        /// <param name="next">Reference to view that is next to have the mouse.</param>
        void MouseLeave(Control c, ViewBase next);

        /// <summary>
        /// Left mouse button double click.
        /// </summary>
        /// <param name="pt">Mouse position relative to control.</param>
        void DoubleClick(Point pt);

        /// <summary>
        /// Should the left mouse down be ignored when present on a visual form border area.
        /// </summary>
        bool IgnoreVisualFormLeftButtonDown { get; }
    }
    #endregion

    #region IKeyController
    /// <summary>
	/// Interface for processing keyboard notifications.
	/// </summary>
    public interface IKeyController
    {
        /// <summary>
        /// Key has been pressed down.
        /// </summary>
        /// <param name="c">Reference to the source control instance.</param>
        /// <param name="e">A KeyEventArgs that contains the event data.</param>
        void KeyDown(Control c, KeyEventArgs e);

        /// <summary>
        /// Key has been pressed.
        /// </summary>
        /// <param name="c">Reference to the source control instance.</param>
        /// <param name="e">A KeyPressEventArgs that contains the event data.</param>
        void KeyPress(Control c, KeyPressEventArgs e);

        /// <summary>
        /// Key has been released.
        /// </summary>
        /// <param name="c">Reference to the source control instance.</param>
        /// <param name="e">A KeyEventArgs that contains the event data.</param>
        /// <returns>True if capturing input; otherwise false.</returns>
        bool KeyUp(Control c, KeyEventArgs e);
    }
    #endregion

    #region ISourceController
    /// <summary>
	/// Interface for processing source notifications.
	/// </summary>
    public interface ISourceController
    {
        /// <summary>
        /// Source control has got the focus.
        /// </summary>
        /// <param name="c">Reference to the source control instance.</param>
        void GotFocus(Control c);

        /// <summary>
        /// Source control has lost the focus.
        /// </summary>
        /// <param name="c">Reference to the source control instance.</param>
        void LostFocus(Control c);
    }
    #endregion
}


text: require('mocha-jsdom')();


text:
package org.apache.nifi.web.security.saml2.registration;

import org.springframework.security.saml2.core.Saml2X509Credential;

import java.security.KeyStore;
import java.util.Collection;

/**
 * SAML2 Credentials Provider translates Certificate and Key entries to SAML2 X.509 Credentials
 */
public interface Saml2CredentialProvider {
    /**
     * Get SAML2 X.509 Credentials from Key Store entries
     *
     * @param keyStore Key Store containing credentials
     * @param keyPassword Optional key password for loading Private Keys
     * @return Collection of SAML2 Credentials
     */
    Collection<Saml2X509Credential> getCredentials(KeyStore keyStore, char[] keyPassword);
}


text:
package com.sourcegasm.riftvision.sensors;

public class LowPassFilter {
	private boolean first = true;
	public double smoothedValue;
	public double smoothing;

	public LowPassFilter(double smoothing) {
		this.smoothing = smoothing;
	}

	public double calculate(double newValue) {
		if (first) {
			smoothedValue = newValue;
			first = false;
			return newValue;
		} else {
			smoothedValue = ((smoothing - 1) * smoothedValue + newValue) / smoothing;
			return smoothedValue;
		}
	}

	public void clear() {
		first = true;
	}
}


text:
package x509svid_test

import (
	"crypto/x509"
	"errors"
	"io/ioutil"
	"os"
	"testing"

	"github.com/spiffe/go-spiffe/v2/internal/pemutil"
	"github.com/spiffe/go-spiffe/v2/spiffeid"
	"github.com/spiffe/go-spiffe/v2/svid/x509svid"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

var (
	keyRSA                 = "testdata/key-pkcs8-rsa.pem"
	certSingle             = "testdata/good-leaf-only.pem"
	leafNoDigitalSignature = "testdata/wrong-leaf-no-digital-signature.pem"
	leafCRLSign            = "testdata/wrong-leaf-crl-sign.pem"
	leafCertSign           = "testdata/wrong-leaf-cert-sign.pem"
	leafCAtrue             = "testdata/wrong-leaf-ca-true.pem"
	leafEmptyID            = "testdata/wrong-leaf-empty-id.pem"
	signNoCA               = "testdata/wrong-intermediate-no-ca.pem"
	signNoKeyCertSign      = "testdata/wrong-intermediate-no-key-cert-sign.pem"

	keyECDSA     = "testdata/key-pkcs8-ecdsa.pem"
	certMultiple = "testdata/good-leaf-and-intermediate.pem"

	certAndKey  = "testdata/good-cert-and-key.pem"
	keyAndCert  = "testdata/good-key-and-cert.pem"
	notPEM      = "testdata/not-pem"
	corruptCert = "testdata/corrupt-cert.pem"
	corruptKey  = "testdata/corrupt-key.pem"
)

func TestLoad_Succeeds(t *testing.T) {
	svid, err := x509svid.Load(certSingle, keyRSA)
	require.NoError(t, err)
	require.NotNil(t, svid)
	require.Equal(t, svid.ID.String(), "spiffe://example.org/workload-1")
}

func TestLoad_FailsCannotReadCertFile(t *testing.T) {
	svid, err := x509svid.Load("testdata/non-existent.pem", keyRSA)
	require.Error(t, err)
	require.Nil(t, svid)
	require.Contains(t, err.Error(), "cannot read certificate file:")
	require.True(t, errors.Is(err, os.ErrNotExist))
}

func TestLoad_FailsCannotReadKeyFile(t *testing.T) {
	svid, err := x509svid.Load(certSingle, "testdata/non-existent.pem")
	require.Error(t, err)
	require.Nil(t, svid)
	require.Contains(t, err.Error(), "cannot read key file:")
	require.True(t, errors.Is(err, os.ErrNotExist))
}

func TestParse(t *testing.T) {
	tests := []struct {
		name           string
		keyPath        string
		certsPath      string
		expID          spiffeid.ID
		expNumCerts    int
		expErrContains string
	}{
		{
			name:        "Single certificate and key",
			keyPath:     keyRSA,
			certsPath:   certSingle,
			expID:       spiffeid.RequireFromString("spiffe://example.org/workload-1"),
			expNumCerts: 1,
		},
		{
			name:        "Certificate with intermediate and key",
			keyPath:     keyECDSA,
			certsPath:   certMultiple,
			expID:       spiffeid.RequireFromString("spiffe://example.org/workload-1"),
			expNumCerts: 2,
		},
		{
			name:        "Key and certificate in the same byte array",
			keyPath:     keyAndCert,
			certsPath:   keyAndCert,
			expID:       spiffeid.RequireFromString("spiffe://example.org/workload-1"),
			expNumCerts: 1,
		},
		{
			name:        "Certificate and Key in the same byte array",
			keyPath:     certAndKey,
			certsPath:   certAndKey,
			expID:       spiffeid.RequireFromString("spiffe://example.org/workload-1"),
			expNumCerts: 1,
		},
		{
			name:           "Missing certificate",
			keyPath:        keyRSA,
			certsPath:      keyRSA,
			expErrContains: "x509svid: certificate validation failed: no certificates found",
		},
		{
			name:           "Missing private key",
			keyPath:        certSingle,
			certsPath:      certSingle,
			expErrContains: "x509svid: private key validation failed: no private key found",
		},
		{
			name:           "Private key not PEM",
			keyPath:        notPEM,
			certsPath:      certSingle,
			expErrContains: "x509svid: cannot parse PEM encoded private key: no PEM blocks found",
		},
		{
			name:           "Certificate not PEM",
			keyPath:        keyRSA,
			certsPath:      notPEM,
			expErrContains: "x509svid: cannot parse PEM encoded certificate: no PEM blocks found",
		},
		{
			name:           "Corrupt private key",
			keyPath:        corruptKey,
			certsPath:      certSingle,
			expErrContains: "x509svid: cannot parse PEM encoded private key: asn1: structure error:",
		},
		{
			name:           "Corrupt certificate",
			keyPath:        keyRSA,
			certsPath:      corruptCert,
			expErrContains: "x509svid: cannot parse PEM encoded certificate: asn1: structure error:",
		},
		{
			name:           "Certificate does not match private key",
			keyPath:        keyRSA,
			certsPath:      certMultiple,
			expErrContains: "x509svid: private key validation failed: leaf certificate does not match private key",
		},
		{
			name:           "Certificate without SPIFFE ID",
			keyPath:        keyRSA,
			certsPath:      leafEmptyID,
			expErrContains: "x509svid: certificate validation failed: cannot get leaf certificate SPIFFE ID: certificate contains no URI SAN",
		},
		{
			name:           "Leaf certificate with CA flag set to true",
			certsPath:      leafCAtrue,
			keyPath:        keyRSA,
			expErrContains: "x509svid: certificate validation failed: leaf certificate must not have CA flag set to true",
		},
		{
			name:           "Leaf certificate without digitalSignature as key usage",
			certsPath:      leafNoDigitalSignature,
			keyPath:        keyRSA,
			expErrContains: "x509svid: certificate validation failed: leaf certificate must have 'digitalSignature' set as key usage",
		},
		{
			name:           "Leaf certificate with certSign as key usage",
			certsPath:      leafCertSign,
			keyPath:        keyRSA,
			expErrContains: "x509svid: certificate validation failed: leaf certificate must not have 'keyCertSign' set as key usage",
		},
		{
			name:           "Leaf certificate with cRLSign as key usage",
			certsPath:      leafCRLSign,
			keyPath:        keyRSA,
			expErrContains: "x509svid: certificate validation failed: leaf certificate must not have 'cRLSign' set as key usage",
		},
		{
			name:           "Signing certificate without CA flag",
			certsPath:      signNoCA,
			keyPath:        keyRSA,
			expErrContains: "x509svid: certificate validation failed: signing certificate must have CA flag set to true",
		},
		{
			name:           "Signing certificate without 'keyCertSign' usage",
			certsPath:      signNoKeyCertSign,
			keyPath:        keyRSA,
			expErrContains: "x509svid: certificate validation failed: signing certificate must have 'keyCertSign' set as key usage",
		},
	}

	for _, test := range tests {
		test := test
		t.Run(test.name, func(t *testing.T) {
			certBytes, err := ioutil.ReadFile(test.certsPath)
			require.NoError(t, err)

			keyBytes, err := ioutil.ReadFile(test.keyPath)
			require.NoError(t, err)

			svid, err := x509svid.Parse(certBytes, keyBytes)
			if test.expErrContains != "" {
				require.Error(t, err)
				assert.Contains(t, err.Error(), test.expErrContains)
				return
			}
			require.NoError(t, err)
			assert.NotNil(t, svid)
			assert.Equal(t, test.expID, svid.ID)
			assert.Len(t, svid.Certificates, test.expNumCerts)
			assert.Equal(t, svid.PrivateKey.Public(), svid.Certificates[0].PublicKey)
		})
	}
}

func TestGetX509SVID(t *testing.T) {
	s, err := x509svid.Load(certSingle, keyRSA)
	require.NoError(t, err)
	svid, err := s.GetX509SVID()
	require.NoError(t, err)
	assert.Equal(t, s, svid)
}

func TestMarshal(t *testing.T) {
	tests := []struct {
		name           string
		keyPath        string
		certsPath      string
		modifySVID     func(*x509svid.SVID)
		expErrContains string
	}{
		{
			name:      "Single certificate and key",
			keyPath:   keyRSA,
			certsPath: certSingle,
		},
		{
			name:      "Multiple certificates and key",
			keyPath:   keyECDSA,
			certsPath: certMultiple,
		},
		{
			name:           "Fails to encode private key",
			keyPath:        keyRSA,
			certsPath:      certSingle,
			expErrContains: "cannot encode private key",
			modifySVID: func(s *x509svid.SVID) {
				s.PrivateKey = nil // Set private key to nil to force an error
			},
		},
		{
			name:           "Fails to marshal certificates",
			keyPath:        keyRSA,
			certsPath:      certSingle,
			expErrContains: "no certificates to marshal",
			modifySVID: func(s *x509svid.SVID) {
				s.Certificates = nil // Set certificates to nil to force an error
			},
		},
	}

	for _, test := range tests {
		test := test
		t.Run(test.name, func(t *testing.T) {
			s, err := x509svid.Load(test.certsPath, test.keyPath)
			require.NoError(t, err)

			if test.modifySVID != nil {
				test.modifySVID(s)
			}

			certs, key, err := s.Marshal()
			if test.expErrContains != "" {
				require.Error(t, err)
				require.Nil(t, certs)
				require.Nil(t, key)
				assert.Contains(t, err.Error(), test.expErrContains)
				return
			}
			require.NoError(t, err)
			require.NotNil(t, certs)
			require.NotNil(t, key)

			expCerts, err := ioutil.ReadFile(test.certsPath)
			require.NoError(t, err)
			assert.Equal(t, expCerts, certs)

			expKey, err := ioutil.ReadFile(test.keyPath)
			require.NoError(t, err)
			assert.Equal(t, expKey, key)
		})
	}
}

func TestMarshalRaw(t *testing.T) {
	tests := []struct {
		name           string
		keyPath        string
		certsPath      string
		modifySVID     func(*x509svid.SVID)
		expErrContains string
	}{
		{
			name:      "Single certificate and key",
			keyPath:   keyRSA,
			certsPath: certSingle,
		},
		{
			name:      "Multiple certificates and key",
			keyPath:   keyECDSA,
			certsPath: certMultiple,
		},
		{
			name:           "Fails to marshal private key",
			keyPath:        keyRSA,
			certsPath:      certSingle,
			expErrContains: "cannot marshal private key",
			modifySVID: func(s *x509svid.SVID) {
				s.PrivateKey = nil // Set private key to nil to force an error
			},
		},
		{
			name:           "Fails to marshal certificates",
			keyPath:        keyRSA,
			certsPath:      certSingle,
			expErrContains: "no certificates to marshal",
			modifySVID: func(s *x509svid.SVID) {
				s.Certificates = nil // Set private key to nil to force an error
			},
		},
	}

	for _, test := range tests {
		test := test
		t.Run(test.name, func(t *testing.T) {
			s, err := x509svid.Load(test.certsPath, test.keyPath)
			require.NoError(t, err)

			if test.modifySVID != nil {
				test.modifySVID(s)
			}

			rawCert, rawKey, err := s.MarshalRaw()
			if test.expErrContains != "" {
				require.Error(t, err)
				require.Nil(t, rawCert)
				require.Nil(t, rawKey)
				assert.Contains(t, err.Error(), test.expErrContains)
				return
			}
			require.NoError(t, err)
			require.NotNil(t, rawCert)
			require.NotNil(t, rawKey)

			expRawCert := loadRawCertificates(t, test.certsPath)
			assert.Equal(t, expRawCert, rawCert)
			expRawKey := loadRawKey(t, test.keyPath)
			assert.Equal(t, expRawKey, rawKey)
		})
	}
}

func TestParseRaw(t *testing.T) {
	tests := []struct {
		name           string
		keyPath        string
		certsPath      string
		rawCerts       []byte
		rawKey         []byte
		expErrContains string
	}{
		{
			name:      "Single certificate and key",
			keyPath:   keyRSA,
			certsPath: certSingle,
			rawCerts:  loadRawCertificates(t, certSingle),
			rawKey:    loadRawKey(t, keyRSA),
		},
		{
			name:      "Multiple certificates and key",
			keyPath:   keyECDSA,
			certsPath: certMultiple,
			rawCerts:  loadRawCertificates(t, certMultiple),
			rawKey:    loadRawKey(t, keyECDSA),
		},
		{
			name:           "Certificate bytes are not DER encoded",
			rawCerts:       []byte("not-DER-encoded"),
			rawKey:         loadRawKey(t, keyRSA),
			expErrContains: "x509svid: cannot parse DER encoded certificate",
		},
		{
			name:           "Key bytes are not DER encoded",
			rawCerts:       loadRawCertificates(t, certSingle),
			rawKey:         []byte("not-DER-encoded"),
			expErrContains: "x509svid: cannot parse DER encoded private key",
		},
	}

	for _, test := range tests {
		test := test
		t.Run(test.name, func(t *testing.T) {
			svid, err := x509svid.ParseRaw(test.rawCerts, test.rawKey)
			if test.expErrContains != "" {
				require.Error(t, err)
				require.Nil(t, svid)
				assert.Contains(t, err.Error(), test.expErrContains)
				return
			}
			require.NoError(t, err)
			require.NotNil(t, svid)
			expectedSVID, err := x509svid.Load(test.certsPath, test.keyPath)
			require.NoError(t, err)
			assert.Equal(t, expectedSVID, svid)
		})
	}
}

func loadRawCertificates(t *testing.T, path string) []byte {
	certsBytes, err := ioutil.ReadFile(path)
	require.NoError(t, err)

	certs, err := pemutil.ParseCertificates(certsBytes)
	require.NoError(t, err)

	var rawBytes []byte
	for _, cert := range certs {
		rawBytes = append(rawBytes, cert.Raw...)
	}
	return rawBytes
}

func loadRawKey(t *testing.T, path string) []byte {
	keyBytes, err := ioutil.ReadFile(path)
	require.NoError(t, err)

	key, err := pemutil.ParsePrivateKey(keyBytes)
	require.NoError(t, err)

	rawKey, err := x509.MarshalPKCS8PrivateKey(key)
	require.NoError(t, err)

	return rawKey
}


text:
// Copyright (c) 2005  Dustin Sallings <dustin@spy.net>

package net.spy.db;

import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import org.jmock.Mock;
import org.jmock.MockObjectTestCase;

/**
 * Test the SQL Runner.
 */
public class SQLRunnerTest extends MockObjectTestCase {

	private static final String QUERY1="update some_table set val=1\n";
	private static final String QUERY2="update other_table set val=1\n"
		+ "otherval=2\n";

	private Mock getSuccessfulMockFor(String query) {
		Mock rv=mock(Statement.class);
		rv.expects(once()).method("setQueryTimeout")
			.with(eq(37));
		rv.expects(once()).method("executeUpdate")
			.with(eq(query))
			.will(returnValue(1));
		rv.expects(once()).method("close");
		return(rv);
	}

	private String getScriptPath() {
		String path=(String)System.getProperties().get("basedir")
			+ "/src/test/net/spy/test/test.sql";
		return(path);
	}

	private void runSuccessfulTest(boolean withAutocommit) throws Exception {
		Mock connMock=mock(Connection.class);
		boolean origAutoCommit=false;

		connMock.expects(once()).method("getAutoCommit")
			.will(returnValue(origAutoCommit));

		if(withAutocommit != origAutoCommit) {
			connMock.expects(once()).method("setAutoCommit")
				.with(eq(withAutocommit));
		}
		connMock.expects(atLeastOnce()).method("createStatement")
			.will(onConsecutiveCalls(
				returnValue(getSuccessfulMockFor(QUERY1).proxy()),
				returnValue(getSuccessfulMockFor(QUERY2).proxy())));
		if(!withAutocommit) {
			connMock.expects(once()).method("commit");
		}
		if(withAutocommit != origAutoCommit) {
			connMock.expects(once()).method("setAutoCommit")
				.with(eq(origAutoCommit));
		}

		InputStream f=new FileInputStream(getScriptPath());
		SQLRunner sr=new SQLRunner((Connection)connMock.proxy());
		sr.setTimeout(37);
		sr.runScript(f, withAutocommit, false);
		f.close();
	}

	/**
	 * Test a successful script execution with autocommit on.
	 */
	public void testSuccessfulScriptWithAutocommit() throws Exception {
		runSuccessfulTest(true);
	}

	/**
	 * Test a successful script with autocommit off.
	 */
	public void testSuccessfulScriptWithoutAutocommit() throws Exception {
		runSuccessfulTest(false);
	}

	/**
	 * Test invocations over a plain unhappy Connection.
	 */
	public void testFailingEarlyFailingOften() throws Exception {
		Mock connMock=mock(Connection.class);

		connMock.expects(once()).method("getAutoCommit")
			.will(throwException(new SQLException("bug off1")));
		connMock.expects(once()).method("rollback")
			.will(throwException(new SQLException("bug off2")));
		// XXX:  This should probably not be invoked in the error case
		connMock.expects(once()).method("setAutoCommit")
			.with(eq(true))
			.will(throwException(new SQLException("bug off3")));

		// Run the script
		InputStream f=new FileInputStream(getScriptPath());
		SQLRunner sr=new SQLRunner((Connection)connMock.proxy());
		try {
			sr.runScript(f);
		} catch(SQLException e) {
			assertEquals("bug off1", e.getMessage());
		}
		f.close();
	}

	private Statement getBadStatement(String query) {
		Mock badMock=mock(Statement.class);
		badMock.expects(once()).method("setQueryTimeout")
			.with(eq(37));
		badMock.expects(once()).method("executeUpdate")
			.with(eq(QUERY2))
			.will(throwException(new SQLException("bug off")));
		badMock.expects(once()).method("close");
		return((Statement)badMock.proxy());
	}

	/**
	 * Test a single failing query.
	 */
	public void testFailingQuery() throws Exception {
		Mock connMock=mock(Connection.class);
		boolean origAutoCommit=true;

		// Set up the connection
		connMock.expects(once()).method("getAutoCommit")
			.will(returnValue(origAutoCommit));

		connMock.expects(once()).method("setAutoCommit")
			.with(eq(false));
		connMock.expects(atLeastOnce()).method("createStatement")
			.will(onConsecutiveCalls(
				returnValue(getSuccessfulMockFor(QUERY1).proxy()),
				returnValue(getBadStatement(QUERY2))));
		connMock.expects(once()).method("rollback");
		connMock.expects(once()).method("setAutoCommit")
			.with(eq(true));

		InputStream f=new FileInputStream(getScriptPath());
		SQLRunner sr=new SQLRunner((Connection)connMock.proxy());
		sr.setTimeout(37);
		try {
			sr.runScript(f);
			fail("Expected an exception out of this invocation");
		} catch(SQLException e) {
			assertEquals("bug off", e.getMessage());
		}
		f.close();
	}

	/**
	 * Test a single failing query with the errok flag.  The whole script will
	 * be considered a success here, but an individual query will fail to
	 * complete.
	 */
	public void testFailingQueryWithErrOk() throws Exception {
		Mock connMock=mock(Connection.class);
		boolean origAutoCommit=false;

		// Set up the connection
		connMock.expects(once()).method("getAutoCommit")
			.will(returnValue(origAutoCommit));

		connMock.expects(once()).method("setAutoCommit")
			.with(eq(true));
		connMock.expects(atLeastOnce()).method("createStatement")
			.will(onConsecutiveCalls(
				returnValue(getSuccessfulMockFor(QUERY1).proxy()),
				returnValue(getBadStatement(QUERY2))));
		connMock.expects(once()).method("setAutoCommit")
			.with(eq(false));

		InputStream f=new FileInputStream(getScriptPath());
		SQLRunner sr=new SQLRunner((Connection)connMock.proxy());
		sr.setTimeout(37);
		sr.runScript(f, true, true);
		f.close();
	}

}