text:
package org.urbangaming.territories.client;
import java.io.*;
import java.util.*;
import org.urbangaming.territories.core.*;

/**
 * This is the class that should be used by the territories admin. This class only allows editing of teams through the
 * console. A map file must already be in existence for this driver to work.
 * @author Andrew Lopreiato
 * @version 1.6 12/8/13
 */
public class ConsoleDriver {

	// CONSTANTS
	public static final String MAP_FILE = "Fall2013.trmp";
	public static final String OUTPUT_MAP = "OutputMap.png";
	// END CONSTANTS

	/**
	 * The main running method.
	 * @param args No options available.
	 */
	public static void main(String[] args) {
		// open up the console
		Scanner inputScanner = new Scanner(System.in);

		// initialize map
		TerritoriesMap territoriesFallMap = null;

		//create list of changing territories
		ArrayList<Territory> changingTerritories = new ArrayList<Territory>();

		try {
			territoriesFallMap = TerritoriesMap.Deserialize(MAP_FILE);

			System.out.println("The following teams are available: ");
			DisplayTeamList(territoriesFallMap);

			PauseUntilConfirmation(inputScanner);

			// check for differences
			for (int i = 0; i < territoriesFallMap.GetAmountOfTerritories(); i++) {
				Territory relevantTerritory = territoriesFallMap.GetTerritory(i);
				Team currentOwner = territoriesFallMap.GetTerritoryOwner(relevantTerritory);

				// if the saved owner is no longer the current owner
				if (!ReaffirmTeamsOwnership(inputScanner, relevantTerritory, currentOwner)) {
					changingTerritories.add(territoriesFallMap.GetTerritory(i));
				}
			}

			// create the differences
			for (int i = 0; i < changingTerritories.size(); i++) {
				SetTerritoryOwner(inputScanner, territoriesFallMap, changingTerritories.get(i));
			}

			// draw the map
			System.out.println("Drawing map...");
			territoriesFallMap.Draw(OUTPUT_MAP);
			System.out.println("Map drawn.");

			// save the map
			System.out.println("Saving map...");
			territoriesFallMap.Serialize(MAP_FILE);
			System.out.println("Map saved.");

		}
		catch (ClassNotFoundException e) {
			System.out.println("This is not a valid file to read.");
		}
		catch (IOException e) {
			System.out.println(e.getLocalizedMessage());
		}

	} // END main

	/**
	 * Will display to the user the teams and their corresponding numbers.
	 * @param teamsList The list to read the teams from.
	 */
	private static void DisplayTeamList (TerritoriesMap tMap) {
		for (int i = 0; i < tMap.GetAmountOfTeams(); i++) {
			System.out.println(i + ": " + (tMap.GetTeam(i).Name));
		}
	} // END DisplayTeamList

	/**
	 * Will prompt the user which team they would like to set the given territory to, and then adds that territory to
	 * the team's territory list.
	 * @param input Scanner to read the input.
	 * @param territory Territory to prompt the user about.
	 * @param teamsList The available teams.
	 */
	private static void SetTerritoryOwner(Scanner input, TerritoriesMap tMap, Territory territory) {
		int userInput = -1;
		while (userInput <= -1) {
			System.out.print("What team owns " + territory.Name + "? ");
			try {
				userInput = input.nextInt();
				if (userInput >= tMap.GetAmountOfTeams() || userInput < 0) {
					throw new IOException("Team " + userInput + "does not exist. ");
				}
			} catch( InputMismatchException e) {
				System.out.print("That is not a number, please try again. ");
				input.nextLine();
				userInput = -1;
			} catch ( IOException e) {
				System.out.print(e.getMessage());
				userInput = -1;
			}

		}
		tMap.SetTerritoriesTeam(territory, tMap.GetTeam(userInput));
	} // END GetTerritoryOwner

	/**
	 * Will prompt the user if the given team still owns the given territory.
	 * @param input Scanner to read the input.
	 * @param territory Territory to prompt the user about.
	 * @param owner Owner to prompt the user with.
	 * @return If the territory still belongs to the team or not.
	 */
	private static Boolean ReaffirmTeamsOwnership(Scanner input, Territory territory, Team owner) {
		System.out.println("Does " + owner.Name + " still own " + territory.Name + "?");
		String answer = null;
		while (answer == null) {
			try {
				answer = input.nextLine();
				answer = answer.toLowerCase();
				if (!(answer.equals("n") || answer.equals("no") || answer.equals("y") || answer.equals("yes") )) {
					throw new IOException("That is not an acceptable answer. Please try again.");
				}
			}
			catch (IOException e) {
				System.out.print(e.getMessage());
				answer = null;
			}
			catch(InputMismatchException e) {
				System.out.print(e.getMessage());
				answer = null;
			}
		}
		if (answer.equals("y") || answer.equals("yes")) {
			return true;
		}
		else {
			return false;
		}
	} // END ReaffirmTeamsOwnership

	/**
	 * Will hold and display three dots until the user presses enter.
	 * @param input Scanner to read the input.
	 */
	private static void PauseUntilConfirmation(Scanner input) {
		System.out.print("...");
		input.nextLine();
	} // END PauseUntilConfirmation

} // END MainDriver

redpajama_set_name: RedPajamaGithub


text:
/**
 * @hack 添加原图参数
 */
/**
 * Initialises the shader.
 *
 * @method init
 */
PIXI.PixiShader.prototype.init = function()
{
    var gl = this.gl;

    var program = PIXI.compileProgram(gl, this.vertexSrc || PIXI.PixiShader.defaultVertexSrc, this.fragmentSrc);

    gl.useProgram(program);

    // get and store the uniforms for the shader
    this.uSampler = gl.getUniformLocation(program, 'uSampler');
    // modify by chenqx, 用来传递 Shader 处理之前的原图
    this.uSourceSampler = gl.getUniformLocation(program, 'uSourceSampler');
    // modify end
    this.projectionVector = gl.getUniformLocation(program, 'projectionVector');
    this.offsetVector = gl.getUniformLocation(program, 'offsetVector');
    this.dimensions = gl.getUniformLocation(program, 'dimensions');
    this.pixelSize = gl.getUniformLocation(program, 'pixelSize');

    // get and store the attributes
    this.aVertexPosition = gl.getAttribLocation(program, 'aVertexPosition');
    this.aTextureCoord = gl.getAttribLocation(program, 'aTextureCoord');
    this.colorAttribute = gl.getAttribLocation(program, 'aColor');

    // Begin worst hack eva //

    // WHY??? ONLY on my chrome pixel the line above returns -1 when using filters?
    // maybe its something to do with the current state of the gl context.
    // I'm convinced this is a bug in the chrome browser as there is NO reason why this should be returning -1 especially as it only manifests on my chrome pixel
    // If theres any webGL people that know why could happen please help :)
    if(this.colorAttribute === -1)
    {
        this.colorAttribute = 2;
    }

    this.attributes = [this.aVertexPosition, this.aTextureCoord, this.colorAttribute];

    if (this.extraAttribute) {
        for (var idx in this.extraAttribute) {
            var key = this.extraAttribute[idx].name;
            this[key] = gl.getAttribLocation(program, key);
            this.attributes.push(this[key]);
        }
    }
    // End worst hack eva //

    // add those custom shaders!
    for (var key in this.uniforms)
    {
        // get the uniform locations..
        this.uniforms[key].uniformLocation = gl.getUniformLocation(program, key);
    }

    this.initUniforms();

    this.program = program;
};


text:
exports.util = require('./lib/util');
exports.DOMutil = require('./lib/DOMutil');

// data
exports.DataSet = require('./lib/DataSet');
exports.DataView = require('./lib/DataView');
exports.Queue = require('./lib/Queue');

// Network
exports.Network = require('./lib/network/Network');
exports.network = {
  Images: require('./lib/network/Images'),
  dotparser: require('./lib/network/dotparser'),
  gephiParser: require('./lib/network/gephiParser'),
  allOptions: require('./lib/network/options')
};
exports.network.convertDot   = function (input) {return exports.network.dotparser.DOTToGraph(input)};
exports.network.convertGephi = function (input,options) {return exports.network.gephiParser.parseGephi(input,options)};

// bundled external libraries
exports.moment = require('./lib/module/moment');
exports.Hammer = require('./lib/module/hammer');
exports.keycharm = require('keycharm');


text:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.crab.dao.ResourceRoleLinkMapper" >
  <resultMap id="BaseResultMap" type="com.crab.entity.ResourceRoleLinkKey" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 24 13:16:32 CST 2015.
    -->
    <id column="resc_id" property="rescId" jdbcType="INTEGER" />
    <id column="role_id" property="roleId" jdbcType="INTEGER" />
  </resultMap>
  <sql id="Example_Where_Clause" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 24 13:16:32 CST 2015.
    -->
    <where >
      <foreach collection="oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 24 13:16:32 CST 2015.
    -->
    <where >
      <foreach collection="example.oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 24 13:16:32 CST 2015.
    -->
    resc_id, role_id
  </sql>
  <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.crab.entity.ResourceRoleLinkExample" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 24 13:16:32 CST 2015.
    -->
    select
    <if test="distinct" >
      distinct
    </if>
    <include refid="Base_Column_List" />
    from t_resource_role
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null" >
      order by ${orderByClause}
    </if>
  </select>
  <delete id="deleteByPrimaryKey" parameterType="com.crab.entity.ResourceRoleLinkKey" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 24 13:16:32 CST 2015.
    -->
    delete from t_resource_role
    where resc_id = #{rescId,jdbcType=INTEGER}
      and role_id = #{roleId,jdbcType=INTEGER}
  </delete>
  <delete id="deleteByExample" parameterType="com.crab.entity.ResourceRoleLinkExample" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 24 13:16:32 CST 2015.
    -->
    delete from t_resource_role
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.crab.entity.ResourceRoleLinkKey" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 24 13:16:32 CST 2015.
    -->
    insert into t_resource_role (resc_id, role_id)
    values (#{rescId,jdbcType=INTEGER}, #{roleId,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.crab.entity.ResourceRoleLinkKey" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 24 13:16:32 CST 2015.
    -->
    insert into t_resource_role
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="rescId != null" >
        resc_id,
      </if>
      <if test="roleId != null" >
        role_id,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="rescId != null" >
        #{rescId,jdbcType=INTEGER},
      </if>
      <if test="roleId != null" >
        #{roleId,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.crab.entity.ResourceRoleLinkExample" resultType="java.lang.Integer" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 24 13:16:32 CST 2015.
    -->
    select count(*) from t_resource_role
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 24 13:16:32 CST 2015.
    -->
    update t_resource_role
    <set >
      <if test="record.rescId != null" >
        resc_id = #{record.rescId,jdbcType=INTEGER},
      </if>
      <if test="record.roleId != null" >
        role_id = #{record.roleId,jdbcType=INTEGER},
      </if>
    </set>
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map" >
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Fri Apr 24 13:16:32 CST 2015.
    -->
    update t_resource_role
    set resc_id = #{record.rescId,jdbcType=INTEGER},
      role_id = #{record.roleId,jdbcType=INTEGER}
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
</mapper>


text:
package party.pjc.blog.model.vo;

/**
 * 查询 记录数的 通用类,查询
 * @author lvc
 *
 */
public class CountResult {

	private String typeName;	//类型名字
	private int typeId;			// 类型 Id
	private int count;			// 记录数

	public String getTypeName() {
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	public int getTypeId() {
		return typeId;
	}

	public void setTypeId(int typeId) {
		this.typeId = typeId;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public CountResult(String typeName, int typeId, int count) {
		super();
		this.typeName = typeName;
		this.typeId = typeId;
		this.count = count;
	}

	public CountResult() {
		super();
	}

	@Override
	public String toString() {
		return "CountResult [typeName=" + typeName + ", typeId=" + typeId + ", count=" + count + "]";
	}

}


text:
from __future__ import (division, absolute_import, unicode_literals,
                        print_function)

# flake8: noqa (unused import and line too long due to links)

import os
import random
import string
import struct
import wave

try:
    import unittest
except ImportError:
    import unittest2 as unittest

try:
    import unittest.mock as mock
except ImportError:
    import mock

from file_metadata._compat import makedirs, which
from file_metadata.utilities import download

CACHE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'files')

file_download_links = {
    # Audio
    'wikiexample.ogg': 'https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg',
    'drums.mid': 'https://upload.wikimedia.org/wikipedia/commons/6/61/Drum_sample.mid',
    'bell.wav': 'https://upload.wikimedia.org/wikipedia/commons/9/97/156064_marcolo91_bicycle-bell.wav',
    'bell.flac': 'https://upload.wikimedia.org/wikipedia/commons/b/b2/Bell-ring.flac',
    'bell.oga': 'https://upload.wikimedia.org/wikipedia/commons/6/6c/Announcement_on_a_wharf.oga',
    'bell.ogg': 'https://upload.wikimedia.org/wikipedia/commons/3/34/Sound_Effect_-_Door_Bell.ogg',
    'multiline_ffprobe.ogg': 'https://upload.wikimedia.org/wikipedia/commons/5/58/17650_thoschi_issyk-kul.ogg',

    # Videos
    'veins.ogv': 'https://upload.wikimedia.org/wikipedia/commons/f/f2/POROS_3.ogv',
    'ogg_video.ogg': 'https://upload.wikimedia.org/wikipedia/commons/e/e3/2010-06-06-V-German-Flag.ogg',
    'sample.webm': 'https://upload.wikimedia.org/wikipedia/commons/a/a5/02_Punktion_des_ausgebildeten_Knopflochs%281%29.webm',

    # Images
    'ball.png': 'https://upload.wikimedia.org/wikipedia/commons/thumb/5/51/1-ball.svg/226px-1-ball.svg.png',
    'ball.svg': 'https://upload.wikimedia.org/wikipedia/commons/5/51/1-ball.svg',
    'red.png': 'https://upload.wikimedia.org/wikipedia/commons/thumb/6/62/Pure_Red.svg/100px-Pure_Red.svg.png',
    'green.png': 'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/Pure_Green.svg/100px-Pure_Green.svg.png',
    'blue.png': 'https://upload.wikimedia.org/wikipedia/commons/thumb/7/77/Pure_Blue.svg/100px-Pure_Blue.svg.png',
    'red.svg': 'https://upload.wikimedia.org/wikipedia/commons/6/62/Pure_Red.svg',
    'green.svg': 'https://upload.wikimedia.org/wikipedia/commons/c/c5/Pure_Green.svg',
    'blue.svg': 'https://upload.wikimedia.org/wikipedia/commons/7/77/Pure_Blue.svg',
    'animated.svg': 'https://upload.wikimedia.org/wikipedia/commons/f/fd/Animated_pendulum.svg',
    'animated.gif': 'https://upload.wikimedia.org/wikipedia/commons/d/d7/123_Numbers.gif',
    'animated.png': 'https://upload.wikimedia.org/wikipedia/commons/b/b5/Load.png',
    'static.gif': 'https://upload.wikimedia.org/wikipedia/commons/e/ed/Pix.gif',
    'cmyk.jpg': 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/11/17-barcodes-1-e_ces_2012_01.jpg/524px-17-barcodes-1-e_ces_2012_01.jpg',
    'unknown_cmyk.jpg': 'https://upload.wikimedia.org/wikipedia/commons/f/f3/TeXML_dtd.jpg',

    # SVG files with different mimetypes
    'image_svg_xml.svg': 'https://upload.wikimedia.org/wikipedia/commons/6/62/Pure_Red.svg',
    'text_plain.svg': 'https://upload.wikimedia.org/wikipedia/commons/5/57/Color_icon_white.svg',
    'text_html.svg': 'https://upload.wikimedia.org/wikipedia/commons/f/fd/Animated_pendulum.svg',
    'application_xml.svg': 'https://upload.wikimedia.org/wikipedia/commons/0/0b/Sieve_of_Eratosthenes_animation.svg',

    # Images with special exifdata:
    'canon_face.jpg': 'https://upload.wikimedia.org/wikipedia/commons/7/7b/Annagrah-2_041.JPG',
    'nonascii_exifdata.jpg': 'https://upload.wikimedia.org/wikipedia/commons/d/d5/2013-04-25_21-09-18-ecl-lune-mosaic.jpg',

    # Images of faces
    'mona_lisa.jpg': 'https://upload.wikimedia.org/wikipedia/commons/7/7d/Mona_Lisa_color_restoration.jpg',
    'michael_jackson.jpg': 'https://upload.wikimedia.org/wikipedia/commons/7/7e/Michaeljackson_%28cropped%29.jpg',
    'charlie_chaplin.jpg': 'https://upload.wikimedia.org/wikipedia/commons/0/00/Charlie_Chaplin.jpg',
    'baby_face.jpg': 'https://upload.wikimedia.org/wikipedia/commons/1/10/Portrait_of_a_male_baby_%285866018681%29.jpg',
    'baby_partial_face.jpg': 'https://upload.wikimedia.org/wikipedia/commons/1/1f/Sweet_Baby_Kisses_Family_Love.jpg',
    'old_face.jpg': 'https://upload.wikimedia.org/wikipedia/commons/1/11/Brazil_%283042571516%29_%282%29.jpg',
    'beard_face.jpg': 'https://upload.wikimedia.org/wikipedia/commons/6/61/Oskar_Almgren%2C_Stockholm%2C_Sweden_%285859501260%29_%282%29.jpg',
    'cat_face.jpg': 'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c4/Savannah_Cat_portrait.jpg/400px-Savannah_Cat_portrait.jpg',
    'monkey_face.jpg': 'https://upload.wikimedia.org/wikipedia/commons/2/27/Baby_ginger_monkey.jpg',
    'woman.xcf': 'https://upload.wikimedia.org/wikipedia/commons/a/af/Beatrix_Podolska_pedagog_muzykolog_Krakow_2008.xcf',

    # Barcodes / QR Codes / Data matrices
    'qrcode.jpg': 'https://upload.wikimedia.org/wikipedia/commons/5/5b/Qrcode_wikipedia.jpg',
    'barcode.png': 'https://upload.wikimedia.org/wikipedia/commons/1/1f/Rationalized-codabar.png',
    'datamatrix.png': 'https://upload.wikimedia.org/wikipedia/commons/thumb/e/e8/Datamatrix.svg/200px-Datamatrix.svg.png',
    'multibarcodes.png': 'https://upload.wikimedia.org/wikipedia/commons/9/98/DHL_Online-Frankierung_-_Paket_bis_5_kg_-_D-USA.png',
    'vertical_barcode.jpg': 'https://upload.wikimedia.org/wikipedia/commons/9/9c/Final_Ida_Pasto_vs._Santa_Fe.jpg',

    'huge.png': 'https://upload.wikimedia.org/wikipedia/commons/3/31/Grand_paris_express.png',

    'blank.xcf': 'https://upload.wikimedia.org/wikipedia/commons/e/e2/Blank_file.xcf',
    'example.tiff': 'https://upload.wikimedia.org/wikipedia/commons/b/b0/Dabigatran_binding_pockets.tiff',

    # Line drawings
    'simple_line_drawing.jpg': 'https://upload.wikimedia.org/wikipedia/commons/c/c6/Destilacija_rakije.jpg',
    'detailed_line_drawing.jpg': 'https://upload.wikimedia.org/wikipedia/commons/d/db/Compound_Microscope_1876.JPG',
    'very_detailed_line_drawing.jpg': 'https://upload.wikimedia.org/wikipedia/commons/c/cb/Hospital_ward_on_Red_Rover.jpg',
    'dark_line_drawing.jpg': 'https://upload.wikimedia.org/wikipedia/commons/3/3e/Bird_in_flight_line_drawing_art.jpg',

    # Logos
    'wikimedia_logo.png': 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/81/Wikimedia-logo.svg/768px-Wikimedia-logo.svg.png',
    'wikidata_logo.png': 'https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Wikidata-logo.svg/1024px-Wikidata-logo.svg.png',
    'wikipedia_logo.png': 'https://upload.wikimedia.org/wikipedia/commons/thumb/d/de/Wikipedia_Logo_1.0.png/768px-Wikipedia_Logo_1.0.png',
    'commons_logo.png': 'https://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Commons-logo.svg/571px-Commons-logo.svg.png',

    # Geocoded images
    'geotag_osaka.jpg': 'https://upload.wikimedia.org/wikipedia/commons/5/50/Honda_STEPWGN_SPADA%E3%83%BBCool_Spirit_%28RP3%29_rear.JPG',

    # Monochrome colors
    'blackwhite_monochrome.jpg': 'https://upload.wikimedia.org/wikipedia/commons/2/27/0218_-_Taormina_-_Badia_Vecchia_-_Foto_Giovanni_Dall%27Orto%2C_20-May-2008.jpg',
    'blue_monochrome.jpg': 'https://upload.wikimedia.org/wikipedia/commons/9/9f/Paolo_Monti_-_Serie_fotografica_-_BEIC_6358396.jpg',
    'green_monochrome.jpg': 'https://upload.wikimedia.org/wikipedia/commons/e/ea/Edvard-dawkins.jpg',
    'sepia_monochrome.jpg': 'https://upload.wikimedia.org/wikipedia/commons/c/c7/1926_Hupmobile.jpg',

    # Color calibrations
    'it8_top_bar.jpg': 'https://upload.wikimedia.org/wikipedia/commons/thumb/9/97/Waterfall_at_Schooner_Head_house_%28NYPL_b11707223-G89F198_003B%29.tiff/lossy-page1-1280px-Waterfall_at_Schooner_Head_house_%28NYPL_b11707223-G89F198_003B%29.tiff.jpg',
    'it8_bottom_bar.jpg': 'https://upload.wikimedia.org/wikipedia/commons/thumb/b/b5/Two_boys_sitting_in_a_garden_%28NYPL_b11528957-G90F452_008B%29.tiff/lossy-page1-996px-Two_boys_sitting_in_a_garden_%28NYPL_b11528957-G90F452_008B%29.tiff.jpg',

    # Application files
    'text.pdf': 'https://upload.wikimedia.org/wikipedia/commons/a/a7/Life_of_Future.pdf',
    'image.pdf': 'https://upload.wikimedia.org/wikipedia/commons/4/40/AugerTransition1.pdf',
    'empty.djvu': 'https://upload.wikimedia.org/wikipedia/commons/4/42/Vuota.djvu',
}

def fetch_file(name, overwrite=False):
    """
    Fetch a file based on the given key. If the file is not found, it is
    created appropriately by either generating it or downloading it from
    elsewhere.

    :param name:      The name (key) of the file that is needed.
    :param overwrite: Force overwrite if file exists.
    :return:          The absolute path of the requested file.
    """
    filepath = os.path.join(CACHE_DIR, name)

    makedirs(CACHE_DIR, exist_ok=True)

    if os.path.exists(filepath) and not overwrite:  # Use cached file
        return filepath

    # Miscellaneous files
    if name == 'ascii.txt':
        with open(filepath, 'w') as file_handler:
            file_handler.writelines([
                string.ascii_lowercase, '\n', string.ascii_uppercase, '\n',
                string.digits, '\n', string.punctuation, '\n'])
    elif name == 'file.bin':
        with open(filepath, 'wb') as file_hander:
            allascii = ''.join(chr(i) for i in range(128))
            file_hander.write(allascii.encode('ascii'))
    # Music files
    elif name == "noise.wav":
        wav_file = wave.open(filepath, 'w')
        wav_file.setparams((1, 2, 44100, 0, 'NONE', 'not compressed'))
        for _ in range(44100):  # 1 second
            value = struct.pack('h', random.randint(-32767, 32767))
            wav_file.writeframes(value)
        wav_file.close()
    elif name in file_download_links:
        download(file_download_links[name], filepath)
    else:
        raise ValueError('Asked to fetch unknown file {0}.'.format(name))

    return filepath

def which_sideeffect(unavailable_executables):
    def wrapper(command, *args, **kwargs):
        if command in unavailable_executables:
            return None
        return which(command, *args, **kwargs)
    return wrapper

def is_toolserver():
    return os.environ.get('INSTANCEPROJECT', None) == 'tools'

def is_travis():
    return os.environ.get('TRAVIS', None) == 'true'


text:
<?php

namespace Tests\Unit\WindowsAzure\MediaServices\Models;
use WindowsAzure\MediaServices\Models\ContentKey;
use WindowsAzure\MediaServices\Models\ContentKeyTypes;
use WindowsAzure\MediaServices\Models\ProtectionKeyTypes;
use WindowsAzure\Common\Internal\Resources;
use WindowsAzure\Common\Internal\Utilities;

/**
 * Represents access policy object used in media services
 *
 * @category  Microsoft
 * @package   WindowsAzure\MediaServices\Models
 * @author    Azure PHP SDK <azurephpsdk@microsoft.com>
 * @copyright Microsoft Corporation
 * @license   http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
 * @version   Release: 0.4.1_2015-03
 * @link      https://github.com/windowsazure/azure-sdk-for-php
 */

class ContentKeyTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @covers WindowsAzure\MediaServices\Models\ContentKey::createFromOptions
     * @covers WindowsAzure\MediaServices\Models\ContentKey::fromArray
     * @covers WindowsAzure\MediaServices\Models\ContentKey::__construct
     */
    public function testCreateFromOptions(){

        // Setup
        $contentKeyId = 'content-key-id-12563';
        $aesKey = '7868CC14AE5FA7E974FAFFAF072DDE2D250334E9D647C086D088C621B28F9F28';
        $options = array(
                'Id'                       => $contentKeyId,
                'Created'                  => '2013-02-26',
                'LastModified'             => '2013-02-26',
                'ContentKeyType'           => ContentKeyTypes::STORAGE_ENCRYPTION,
                'EncryptedContentKey'      => $aesKey,
                'Name'                     => 'testNameForContentKey',
                'ProtectionKeyId'          => 'protection-key-id-36589',
                'ProtectionKeyType'        => ProtectionKeyTypes::X509_CERTIFICATE_THUMBPRINT,
                'Checksum'                 => 'checksum-of-content-key'
        );
        $created = new \Datetime($options['Created']);
        $modified = new \Datetime($options['LastModified']);

        // Test
        $contentKey = ContentKey::createFromOptions($options);

        // Assert
        $this->assertEquals($contentKeyId, $contentKey->getId());
        $this->assertEquals($created->getTimestamp(), $contentKey->getCreated()->getTimestamp());
        $this->assertEquals($modified->getTimestamp(), $contentKey->getLastModified()->getTimestamp());
        $this->assertEquals($options['ContentKeyType'], $contentKey->getContentKeyType());
        $this->assertEquals($aesKey, $contentKey->getEncryptedContentKey());
        $this->assertEquals($options['Name'], $contentKey->getName());
        $this->assertEquals($options['ProtectionKeyId'], $contentKey->getProtectionKeyId());
        $this->assertEquals($options['ProtectionKeyType'], $contentKey->getProtectionKeyType());
        $this->assertEquals($options['Checksum'], $contentKey->getChecksum());
    }

    /**
     * @covers WindowsAzure\MediaServices\Models\ContentKey::getChecksum
     * @covers WindowsAzure\MediaServices\Models\ContentKey::setChecksum
     */
    public function testGetSetChecksum(){

        // Setup
        $checksum = 'checksum-of-content-key';
        $contentKey = new ContentKey();

        // Test
        $contentKey->setChecksum($checksum);
        $result = $contentKey->getChecksum();

        // Assert
        $this->assertEquals($checksum, $result);
    }

    /**
     * @covers WindowsAzure\MediaServices\Models\ContentKey::getProtectionKeyType
     * @covers WindowsAzure\MediaServices\Models\ContentKey::setProtectionKeyType
     */
    public function testGetSetProtectionKeyType(){

        // Setup
        $contentKey = new ContentKey();
        $protectionKeyType = ProtectionKeyTypes::X509_CERTIFICATE_THUMBPRINT;

        // Test
        $contentKey->setProtectionKeyType($protectionKeyType);
        $result = $contentKey->getProtectionKeyType();

        // Assert
        $this->assertEquals($protectionKeyType, $result);
    }

    /**
     * @covers WindowsAzure\MediaServices\Models\ContentKey::getProtectionKeyId
     * @covers WindowsAzure\MediaServices\Models\ContentKey::setProtectionKeyId
     */
    public function testGetSetProtectionKeyId(){

        // Setup
        $contentKey = new ContentKey();
        $protectionKeyId = 'protection-key-id-36589';

        // Test
        $contentKey->setProtectionKeyId($protectionKeyId);
        $result = $contentKey->getProtectionKeyId();

        // Assert
        $this->assertEquals($protectionKeyId, $result);
    }

    /**
     * @covers WindowsAzure\MediaServices\Models\ContentKey::getName
     * @covers WindowsAzure\MediaServices\Models\ContentKey::setName
     */
    public function testGetSetName(){

        // Setup
        $contentKey = new ContentKey();
        $name = 'test Name For Content Key';

        // Test
        $contentKey->setName($name);
        $result = $contentKey->getName();

        // Assert
        $this->assertEquals($name, $result);
    }

    /**
     * @covers WindowsAzure\MediaServices\Models\ContentKey::getEncryptedContentKey
     * @covers WindowsAzure\MediaServices\Models\ContentKey::setEncryptedContentKey
     */
    public function testGetSetEncryptedContentKey(){

        // Setup
        $contentKey = new ContentKey();
        $aesKey = '7868CC14AE5FA7E974FAFFAF072DDE2D250334E9D647C086D088C621B28F9F28';

        // Test
        $contentKey->setEncryptedContentKey($aesKey);
        $result = $contentKey->getEncryptedContentKey();

        // Assert
        $this->assertEquals($aesKey, $result);
    }

    /**
     * @covers WindowsAzure\MediaServices\Models\ContentKey::getContentKeyType
     * @covers WindowsAzure\MediaServices\Models\ContentKey::setContentKeyType
     */
    public function testGetSetContentKeyType(){

        // Setup
        $contentKeyType = ContentKeyTypes::STORAGE_ENCRYPTION;
        $contentKey = new ContentKey();

        // Test
        $contentKey->setContentKeyType($contentKeyType);
        $result = $contentKey->getContentKeyType();

        // Assert
        $this->assertEquals($contentKeyType, $result);
    }

    /**
     * @covers WindowsAzure\MediaServices\Models\ContentKey::getCreated
     */
    public function testGetCreated(){

        // Setup
        $options = array(
                'Created'                  => '2013-02-26'
        );
        $created = new \Datetime($options['Created']);
        $contentKey = ContentKey::createFromOptions($options);

        // Test
        $result = $contentKey->getCreated();

        // Assert
        $this->assertEquals($created->getTimestamp(), $result->getTimestamp());
    }

    /**
     * @covers WindowsAzure\MediaServices\Models\ContentKey::getLastModified
     */
    public function testGetLastModified(){

        // Setup
        $options = array(
                'LastModified'             => '2013-02-26'
        );
        $modified = new \Datetime($options['LastModified']);
        $contentKey = ContentKey::createFromOptions($options);

        // Test
        $result = $contentKey->getLastModified();

        // Assert
        $this->assertEquals($modified->getTimestamp(), $result->getTimestamp());
    }

    /**
     * @covers WindowsAzure\MediaServices\Models\ContentKey::getId
     * @covers WindowsAzure\MediaServices\Models\ContentKey::setId
     */
    public function testGetSetId(){

        // Setup
        $newContentKeyId = 'content-key-id-14569';
        $contentKey = new ContentKey();

        // Test
        $contentKey->setId($newContentKeyId);
        $result = $contentKey->getId();

        // Assert
        $this->assertEquals($newContentKeyId, $result);
    }

    /**
     * @covers WindowsAzure\MediaServices\Models\ContentKey::setContentKey
     * @covers WindowsAzure\MediaServices\Models\ContentKey::_generateChecksum
     * @covers WindowsAzure\MediaServices\Models\ContentKey::_generateEncryptedContentKey
     */
    public function testSetContentKey(){

        // Setup
$protectionKey = '-----BEGIN CERTIFICATE-----
MIICWDCCAcGgAwIBAgIJAMoW3Bym8NviMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV
BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX
aWRnaXRzIFB0eSBMdGQwHhcNMTQwMzA0MTExNTMxWhcNMTQwNDAzMTExNTMxWjBF
MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50
ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB
gQCnUuC20WGTH8iBp/CcHOthHN/RodGDfiUmtGVAH60toze4lNkdy3xstHrdIkWY
YOc3bTtiOLcl78YXkslkRTwKERCwLYsSQgZZK+bUE17oYpKgxKvW3Zrni8eQVzue
KEjQGyGpdJw7C2RCtx31e941RVlXh2kda1KmF66EUrRywwIDAQABo1AwTjAdBgNV
HQ4EFgQUUeTVs4bfDo7Ap/nWHvBbaPlcMjIwHwYDVR0jBBgwFoAUUeTVs4bfDo7A
p/nWHvBbaPlcMjIwDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQUFAAOBgQCQvweb
V2mQn2pGNuquGkStf+AYEPB4kxqRBBcbxOiWtHZsr/K0zIe0fWEiMKE/7X6CAgtJ
JeryWs9JlCjCqU6O2WMxuOI2JRneprs78/3jieYbAAgpEK9LyhZC6QCT3WKrvh+j
3uVcuoKBfVUFTvVtpSVttL7cNULIGYpP/V1yVg==
-----END CERTIFICATE-----';

        $private = '-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCnUuC20WGTH8iBp/CcHOthHN/RodGDfiUmtGVAH60toze4lNkd
y3xstHrdIkWYYOc3bTtiOLcl78YXkslkRTwKERCwLYsSQgZZK+bUE17oYpKgxKvW
3Zrni8eQVzueKEjQGyGpdJw7C2RCtx31e941RVlXh2kda1KmF66EUrRywwIDAQAB
AoGAJWAG3+9PO1zbHdMUlNqE3VFk0V+y/As+YzHid/tbZJlTxgBBqz0b0vBRjXmt
UAc9Po1AuYTvrCKt/fAE2kf4y+5dw6UeUpsfWbmDbFY1BPbgeETFXDE7DEXb7IJg
YlDKOq7XwdQBUe1s2P7yAhfLggyjnnJKi6i/ZSrWYmrvCGkCQQDTsof9FmFZkrNK
ZjoxFGOl5+2ZzW+3nZUKdiWKRHqZADMrajCtGJqSIFoZMuItiKxy8CXgTlNYxxXu
5wFjmxjFAkEAylcTCAruZ2T+/+2WBpJNRLHZh+ivrSV/9PWr0GUSKBSG4iN/f6w7
7MHCUqJlhLE95xq+TnpcDaFWKqQPTQRF5wJAEZhKEy/0AWTe//UFKyUdryFaryjS
+zjetVLihd5xLhxFJHub9hcQacrEkkmXYN92Lctl6oG4Da3mVcffZq7yXQJBAIYh
Oqh2Npurw658HJu2mCoVi5IgmXQ6C5yizoaSuXqAQPnfdkF4NzE3ME3/ATT5GYP9
onH7gtI2RRx3LP1s+7kCQDa5BheXwTt/yVachh3+eYlWszGbFxbOlFKn20wy68bZ
hfb0RVoAxC2qqoNJKMNuN2Rct/j1Gk8qKp5YtaW12+M=
-----END RSA PRIVATE KEY-----';

        $contentKey = new ContentKey();
        $contentKey->setId('content-key-id-156k');
        $aesKey = base64_decode('KbOoNIjrQONfuyU86hA8mCFNq0sFoZHx0tTFopo+/mg=');
        $checksum = '/IJUKYWw980=';

        // Test
        $contentKey->setContentKey($aesKey, $protectionKey);

        // Assert
        openssl_private_decrypt(
            base64_decode($contentKey->getEncryptedContentKey()),
            $decryptedContentKey,
            openssl_get_privatekey($private),
            OPENSSL_PKCS1_OAEP_PADDING
        );

        $this->assertEquals($decryptedContentKey, $aesKey);
        $this->assertEquals($checksum, $contentKey->getChecksum());
    }

}


text:
#ifndef __OBJECT_EXCEPTION_H__
#define __OBJECT_EXCEPTION_H__

    #include <saffire/objects/object.h>

    typedef struct {
        t_string        *message;       // Exception message
        long            code;           // Exception code
        t_hash_table    *stacktrace;    // Stacktrace list
    } t_exception_object_data;

    typedef struct _exeption_object {
        SAFFIRE_OBJECT_HEADER
        t_exception_object_data data;
        SAFFIRE_OBJECT_FOOTER
    } t_exception_object;

    t_exception_object Object_Exception_struct;
    #define Object_Exception ((t_object *)&Object_Exception_struct)

    #include <saffire/objects/_generated_exceptions.h>

    void object_exception_init(void);
    void object_exception_fini(void);

#endif


text:
package org.apache.lucene.search.grouping;

import java.io.IOException;
import java.util.*;

import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.queries.function.valuesource.BytesRefFieldSource;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.grouping.dv.DVDistinctValuesCollector;
import org.apache.lucene.search.grouping.dv.DVFirstPassGroupingCollector;
import org.apache.lucene.search.grouping.function.FunctionDistinctValuesCollector;
import org.apache.lucene.search.grouping.function.FunctionFirstPassGroupingCollector;
import org.apache.lucene.search.grouping.term.TermDistinctValuesCollector;
import org.apache.lucene.search.grouping.term.TermFirstPassGroupingCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util._TestUtil;
import org.apache.lucene.util.mutable.MutableValue;
import org.apache.lucene.util.mutable.MutableValueStr;

public class DistinctValuesCollectorTest extends AbstractGroupingTestCase {

  private final static NullComparator nullComparator = new NullComparator();

  private final String groupField = "author";
  private final String countField = "publisher";

  public void testSimple() throws Exception {
    Random random = random();
    DocValues.Type[] dvTypes = new DocValues.Type[]{
        DocValues.Type.VAR_INTS,
        DocValues.Type.FLOAT_64,
        DocValues.Type.BYTES_VAR_STRAIGHT,
        DocValues.Type.BYTES_VAR_SORTED
    };
    Directory dir = newDirectory();
    RandomIndexWriter w = new RandomIndexWriter(
        random,
        dir,
        newIndexWriterConfig(TEST_VERSION_CURRENT,
            new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
    boolean canUseDV = !"Lucene3x".equals(w.w.getConfig().getCodec().getName());
    DocValues.Type dvType = canUseDV ? dvTypes[random.nextInt(dvTypes.length)] : null;

    Document doc = new Document();
    addField(doc, groupField, "1", dvType);
    addField(doc, countField, "1", dvType);
    doc.add(new TextField("content", "random text", Field.Store.NO));
    doc.add(new StringField("id", "1", Field.Store.NO));
    w.addDocument(doc);

    // 1
    doc = new Document();
    addField(doc, groupField, "1", dvType);
    addField(doc, countField, "1", dvType);
    doc.add(new TextField("content", "some more random text blob", Field.Store.NO));
    doc.add(new StringField("id", "2", Field.Store.NO));
    w.addDocument(doc);

    // 2
    doc = new Document();
    addField(doc, groupField, "1", dvType);
    addField(doc, countField, "2", dvType);
    doc.add(new TextField("content", "some more random textual data", Field.Store.NO));
    doc.add(new StringField("id", "3", Field.Store.NO));
    w.addDocument(doc);
    w.commit(); // To ensure a second segment

    // 3
    doc = new Document();
    addField(doc, groupField, "2", dvType);
    doc.add(new TextField("content", "some random text", Field.Store.NO));
    doc.add(new StringField("id", "4", Field.Store.NO));
    w.addDocument(doc);

    // 4
    doc = new Document();
    addField(doc, groupField, "3", dvType);
    addField(doc, countField, "1", dvType);
    doc.add(new TextField("content", "some more random text", Field.Store.NO));
    doc.add(new StringField("id", "5", Field.Store.NO));
    w.addDocument(doc);

    // 5
    doc = new Document();
    addField(doc, groupField, "3", dvType);
    addField(doc, countField, "1", dvType);
    doc.add(new TextField("content", "random blob", Field.Store.NO));
    doc.add(new StringField("id", "6", Field.Store.NO));
    w.addDocument(doc);

    // 6 -- no author field
    doc = new Document();
    doc.add(new TextField("content", "random word stuck in alot of other text", Field.Store.YES));
    addField(doc, countField, "1", dvType);
    doc.add(new StringField("id", "6", Field.Store.NO));
    w.addDocument(doc);

    IndexSearcher indexSearcher = newSearcher(w.getReader());
    w.close();

    Comparator<AbstractDistinctValuesCollector.GroupCount<Comparable<Object>>> cmp = new Comparator<AbstractDistinctValuesCollector.GroupCount<Comparable<Object>>>() {

      public int compare(AbstractDistinctValuesCollector.GroupCount<Comparable<Object>> groupCount1, AbstractDistinctValuesCollector.GroupCount<Comparable<Object>> groupCount2) {
        if (groupCount1.groupValue == null) {
          if (groupCount2.groupValue == null) {
            return 0;
          }
          return -1;
        } else if (groupCount2.groupValue == null) {
          return 1;
        } else {
          return groupCount1.groupValue.compareTo(groupCount2.groupValue);
        }
      }

    };

    // === Search for content:random
    AbstractFirstPassGroupingCollector<Comparable<Object>> firstCollector = createRandomFirstPassCollector(dvType, new Sort(), groupField, 10);
    indexSearcher.search(new TermQuery(new Term("content", "random")), firstCollector);
    AbstractDistinctValuesCollector<? extends AbstractDistinctValuesCollector.GroupCount<Comparable<Object>>> distinctValuesCollector
        = createDistinctCountCollector(firstCollector, groupField, countField, dvType);
    indexSearcher.search(new TermQuery(new Term("content", "random")), distinctValuesCollector);

    List<? extends AbstractDistinctValuesCollector.GroupCount<Comparable<Object>>> gcs =  distinctValuesCollector.getGroups();
    Collections.sort(gcs, cmp);
    assertEquals(4, gcs.size());

    compareNull(gcs.get(0).groupValue);
    List<Comparable<?>> countValues = new ArrayList<Comparable<?>>(gcs.get(0).uniqueValues);
    assertEquals(1, countValues.size());
    compare("1", countValues.get(0));

    compare("1", gcs.get(1).groupValue);
    countValues = new ArrayList<Comparable<?>>(gcs.get(1).uniqueValues);
    Collections.sort(countValues, nullComparator);
    assertEquals(2, countValues.size());
    compare("1", countValues.get(0));
    compare("2", countValues.get(1));

    compare("2", gcs.get(2).groupValue);
    countValues = new ArrayList<Comparable<?>>(gcs.get(2).uniqueValues);
    assertEquals(1, countValues.size());
    compareNull(countValues.get(0));

    compare("3", gcs.get(3).groupValue);
    countValues = new ArrayList<Comparable<?>>(gcs.get(3).uniqueValues);
    assertEquals(1, countValues.size());
    compare("1", countValues.get(0));

    // === Search for content:some
    firstCollector = createRandomFirstPassCollector(dvType, new Sort(), groupField, 10);
    indexSearcher.search(new TermQuery(new Term("content", "some")), firstCollector);
    distinctValuesCollector = createDistinctCountCollector(firstCollector, groupField, countField, dvType);
    indexSearcher.search(new TermQuery(new Term("content", "some")), distinctValuesCollector);

    gcs = distinctValuesCollector.getGroups();
    Collections.sort(gcs, cmp);
    assertEquals(3, gcs.size());

    compare("1", gcs.get(0).groupValue);
    countValues = new ArrayList<Comparable<?>>(gcs.get(0).uniqueValues);
    assertEquals(2, countValues.size());
    Collections.sort(countValues, nullComparator);
    compare("1", countValues.get(0));
    compare("2", countValues.get(1));

    compare("2", gcs.get(1).groupValue);
    countValues = new ArrayList<Comparable<?>>(gcs.get(1).uniqueValues);
    assertEquals(1, countValues.size());
    compareNull(countValues.get(0));

    compare("3", gcs.get(2).groupValue);
    countValues = new ArrayList<Comparable<?>>(gcs.get(2).uniqueValues);
    assertEquals(1, countValues.size());
    compare("1", countValues.get(0));

     // === Search for content:blob
    firstCollector = createRandomFirstPassCollector(dvType, new Sort(), groupField, 10);
    indexSearcher.search(new TermQuery(new Term("content", "blob")), firstCollector);
    distinctValuesCollector = createDistinctCountCollector(firstCollector, groupField, countField, dvType);
    indexSearcher.search(new TermQuery(new Term("content", "blob")), distinctValuesCollector);

    gcs = distinctValuesCollector.getGroups();
    Collections.sort(gcs, cmp);
    assertEquals(2, gcs.size());

    compare("1", gcs.get(0).groupValue);
    countValues = new ArrayList<Comparable<?>>(gcs.get(0).uniqueValues);
    // B/c the only one document matched with blob inside the author 1 group
    assertEquals(1, countValues.size());
    compare("1", countValues.get(0));

    compare("3", gcs.get(1).groupValue);
    countValues = new ArrayList<Comparable<?>>(gcs.get(1).uniqueValues);
    assertEquals(1, countValues.size());
    compare("1", countValues.get(0));

    indexSearcher.getIndexReader().close();
    dir.close();
  }

  public void testRandom() throws Exception {
    Random random = random();
    int numberOfRuns = _TestUtil.nextInt(random, 3, 6);
    for (int indexIter = 0; indexIter < numberOfRuns; indexIter++) {
      IndexContext context = createIndexContext();
      for (int searchIter = 0; searchIter < 100; searchIter++) {
        final IndexSearcher searcher = newSearcher(context.indexReader);
        boolean useDv = context.dvType != null && random.nextBoolean();
        DocValues.Type dvType = useDv ? context.dvType : null;
        String term = context.contentStrings[random.nextInt(context.contentStrings.length)];
        Sort groupSort = new Sort(new SortField("id", SortField.Type.STRING));
        int topN = 1 + random.nextInt(10);

        List<AbstractDistinctValuesCollector.GroupCount<Comparable<?>>> expectedResult = createExpectedResult(context, term, groupSort, topN);

        AbstractFirstPassGroupingCollector<Comparable<?>> firstCollector = createRandomFirstPassCollector(dvType, groupSort, groupField, topN);
        searcher.search(new TermQuery(new Term("content", term)), firstCollector);
        AbstractDistinctValuesCollector<? extends AbstractDistinctValuesCollector.GroupCount<Comparable<?>>> distinctValuesCollector
            = createDistinctCountCollector(firstCollector, groupField, countField, dvType);
        searcher.search(new TermQuery(new Term("content", term)), distinctValuesCollector);
        @SuppressWarnings("unchecked")
        List<AbstractDistinctValuesCollector.GroupCount<Comparable<?>>> actualResult = (List<AbstractDistinctValuesCollector.GroupCount<Comparable<?>>>) distinctValuesCollector.getGroups();

        if (VERBOSE) {
          System.out.println("Index iter=" + indexIter);
          System.out.println("Search iter=" + searchIter);
          System.out.println("Collector class name=" + distinctValuesCollector.getClass().getName());
        }

        assertEquals(expectedResult.size(), actualResult.size());
        for (int i = 0; i < expectedResult.size(); i++) {
          AbstractDistinctValuesCollector.GroupCount<Comparable<?>> expected = expectedResult.get(i);
          AbstractDistinctValuesCollector.GroupCount<Comparable<?>> actual = actualResult.get(i);
          assertValues(expected.groupValue, actual.groupValue);
          assertEquals(expected.uniqueValues.size(), actual.uniqueValues.size());
          List<Comparable<?>> expectedUniqueValues = new ArrayList<Comparable<?>>(expected.uniqueValues);
          Collections.sort(expectedUniqueValues, nullComparator);
          List<Comparable<?>> actualUniqueValues = new ArrayList<Comparable<?>>(actual.uniqueValues);
          Collections.sort(actualUniqueValues, nullComparator);
          for (int j = 0; j < expected.uniqueValues.size(); j++) {
            assertValues(expectedUniqueValues.get(j), actualUniqueValues.get(j));
          }
        }
      }
      context.indexReader.close();
      context.directory.close();
    }
  }

  private void assertValues(Object expected, Object actual) {
    if (expected == null) {
      compareNull(actual);
    } else {
      compare(((BytesRef) expected).utf8ToString(), actual);
    }
  }

  private void compare(String expected, Object groupValue) {
    if (BytesRef.class.isAssignableFrom(groupValue.getClass())) {
      assertEquals(expected, ((BytesRef) groupValue).utf8ToString());
    } else if (Double.class.isAssignableFrom(groupValue.getClass())) {
      assertEquals(Double.parseDouble(expected), groupValue);
    } else if (Long.class.isAssignableFrom(groupValue.getClass())) {
      assertEquals(Long.parseLong(expected), groupValue);
    } else if (MutableValue.class.isAssignableFrom(groupValue.getClass())) {
      MutableValueStr mutableValue = new MutableValueStr();
      mutableValue.value = new BytesRef(expected);
      assertEquals(mutableValue, groupValue);
    } else {
      fail();
    }
  }

  private void compareNull(Object groupValue) {
    if (groupValue == null) {
      return; // term based impl...
    }
    // DV based impls..
    if (BytesRef.class.isAssignableFrom(groupValue.getClass())) {
      assertEquals("", ((BytesRef) groupValue).utf8ToString());
    } else if (Double.class.isAssignableFrom(groupValue.getClass())) {
      assertEquals(0.0d, groupValue);
    } else if (Long.class.isAssignableFrom(groupValue.getClass())) {
      assertEquals(0L, groupValue);
      // Function based impl
    } else if (MutableValue.class.isAssignableFrom(groupValue.getClass())) {
      assertFalse(((MutableValue) groupValue).exists());
    } else {
      fail();
    }
  }

  private void addField(Document doc, String field, String value, DocValues.Type type) {
    doc.add(new StringField(field, value, Field.Store.NO));
    if (type == null) {
      return;
    }

    Field valuesField = null;
    switch (type) {
      case VAR_INTS:
        valuesField = new PackedLongDocValuesField(field, Integer.parseInt(value));
        break;
      case FLOAT_64:
        valuesField = new DoubleDocValuesField(field, Double.parseDouble(value));
        break;
      case BYTES_VAR_STRAIGHT:
        valuesField = new StraightBytesDocValuesField(field, new BytesRef(value));
        break;
      case BYTES_VAR_SORTED:
        valuesField = new SortedBytesDocValuesField(field, new BytesRef(value));
        break;
    }
    doc.add(valuesField);
  }

  @SuppressWarnings({"unchecked","rawtypes"})
  private <T extends Comparable> AbstractDistinctValuesCollector<AbstractDistinctValuesCollector.GroupCount<T>> createDistinctCountCollector(AbstractFirstPassGroupingCollector<T> firstPassGroupingCollector,
                                                                      String groupField,
                                                                      String countField,
                                                                      DocValues.Type dvType) {
    Random random = random();
    Collection<SearchGroup<T>> searchGroups = firstPassGroupingCollector.getTopGroups(0, false);
    if (DVFirstPassGroupingCollector.class.isAssignableFrom(firstPassGroupingCollector.getClass())) {
      boolean diskResident = random.nextBoolean();
      return DVDistinctValuesCollector.create(groupField, countField, searchGroups, diskResident, dvType);
    } else if (FunctionFirstPassGroupingCollector.class.isAssignableFrom(firstPassGroupingCollector.getClass())) {
      return (AbstractDistinctValuesCollector) new FunctionDistinctValuesCollector(new HashMap<Object, Object>(), new BytesRefFieldSource(groupField), new BytesRefFieldSource(countField), (Collection) searchGroups);
    } else {
      return (AbstractDistinctValuesCollector) new TermDistinctValuesCollector(groupField, countField, (Collection) searchGroups);
    }
  }

  @SuppressWarnings({"unchecked","rawtypes"})
  private <T> AbstractFirstPassGroupingCollector<T> createRandomFirstPassCollector(DocValues.Type dvType, Sort groupSort, String groupField, int topNGroups) throws IOException {
    Random random = random();
    if (dvType != null) {
      if (random.nextBoolean()) {
        boolean diskResident = random.nextBoolean();
        return DVFirstPassGroupingCollector.create(groupSort, topNGroups, groupField, dvType, diskResident);
      } else if (random.nextBoolean()) {
        return (AbstractFirstPassGroupingCollector<T>) new FunctionFirstPassGroupingCollector(new BytesRefFieldSource(groupField), new HashMap<Object, Object>(), groupSort, topNGroups);
      } else {
        return (AbstractFirstPassGroupingCollector<T>) new TermFirstPassGroupingCollector(groupField, groupSort, topNGroups);
      }
    } else {
      if (random.nextBoolean()) {
        return (AbstractFirstPassGroupingCollector<T>) new FunctionFirstPassGroupingCollector(new BytesRefFieldSource(groupField), new HashMap<Object, Object>(), groupSort, topNGroups);
      } else {
        return (AbstractFirstPassGroupingCollector<T>) new TermFirstPassGroupingCollector(groupField, groupSort, topNGroups);
      }
    }
  }

  @SuppressWarnings({"unchecked","rawtypes"})
  private List<AbstractDistinctValuesCollector.GroupCount<Comparable<?>>> createExpectedResult(IndexContext context,  String term, Sort groupSort, int topN) {
    class GroupCount extends AbstractDistinctValuesCollector.GroupCount<BytesRef> {
      GroupCount(BytesRef groupValue, Collection<BytesRef> uniqueValues) {
        super(groupValue);
        this.uniqueValues.addAll(uniqueValues);
      }
    }

    List result = new ArrayList();
    Map<String, Set<String>> groupCounts = context.searchTermToGroupCounts.get(term);
    int i = 0;
    for (String group : groupCounts.keySet()) {
      if (topN <= i++) {
        break;
      }
      Set<BytesRef> uniqueValues = new HashSet<BytesRef>();
      for (String val : groupCounts.get(group)) {
        uniqueValues.add(val != null ? new BytesRef(val) : null);
      }
      result.add(new GroupCount(group != null ? new BytesRef(group) : null, uniqueValues));
    }
    return result;
  }

  private IndexContext createIndexContext() throws Exception {
    Random random = random();
    DocValues.Type[] dvTypes = new DocValues.Type[]{
        DocValues.Type.BYTES_VAR_STRAIGHT,
        DocValues.Type.BYTES_VAR_SORTED
    };

    Directory dir = newDirectory();
    RandomIndexWriter w = new RandomIndexWriter(
        random,
        dir,
        newIndexWriterConfig(TEST_VERSION_CURRENT,
        new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy())
      );

    boolean canUseDV = !"Lucene3x".equals(w.w.getConfig().getCodec().getName());
    DocValues.Type dvType = canUseDV ? dvTypes[random.nextInt(dvTypes.length)] : null;

    int numDocs = 86 + random.nextInt(1087) * RANDOM_MULTIPLIER;
    String[] groupValues = new String[numDocs / 5];
    String[] countValues = new String[numDocs / 10];
    for (int i = 0; i < groupValues.length; i++) {
      groupValues[i] = generateRandomNonEmptyString();
    }
    for (int i = 0; i < countValues.length; i++) {
      countValues[i] = generateRandomNonEmptyString();
    }

    List<String> contentStrings = new ArrayList<String>();
    Map<String, Map<String, Set<String>>> searchTermToGroupCounts = new HashMap<String, Map<String, Set<String>>>();
    for (int i = 1; i <= numDocs; i++) {
      String groupValue = random.nextInt(23) == 14 ? null : groupValues[random.nextInt(groupValues.length)];
      String countValue = random.nextInt(21) == 13 ? null : countValues[random.nextInt(countValues.length)];
      String content = "random" + random.nextInt(numDocs / 20);
      Map<String, Set<String>> groupToCounts = searchTermToGroupCounts.get(content);
      if (groupToCounts == null) {
        // Groups sort always DOCID asc...
        searchTermToGroupCounts.put(content, groupToCounts = new LinkedHashMap<String, Set<String>>());
        contentStrings.add(content);
      }

      Set<String> countsVals = groupToCounts.get(groupValue);
      if (countsVals == null) {
        groupToCounts.put(groupValue, countsVals = new HashSet<String>());
      }
      countsVals.add(countValue);

      Document doc = new Document();
      doc.add(new StringField("id", String.format(Locale.ROOT, "%09d", i), Field.Store.NO));
      if (groupValue != null) {
        addField(doc, groupField, groupValue, dvType);
      }
      if (countValue != null) {
        addField(doc, countField, countValue, dvType);
      }
      doc.add(new TextField("content", content, Field.Store.NO));
      w.addDocument(doc);
    }

    DirectoryReader reader = w.getReader();
    w.close();
    return new IndexContext(dir, reader, dvType, searchTermToGroupCounts, contentStrings.toArray(new String[contentStrings.size()]));
  }

  private static class IndexContext {

    final Directory directory;
    final DirectoryReader indexReader;
    final DocValues.Type dvType;
    final Map<String, Map<String, Set<String>>> searchTermToGroupCounts;
    final String[] contentStrings;

    IndexContext(Directory directory, DirectoryReader indexReader, DocValues.Type dvType,
                 Map<String, Map<String, Set<String>>> searchTermToGroupCounts, String[] contentStrings) {
      this.directory = directory;
      this.indexReader = indexReader;
      this.dvType = dvType;
      this.searchTermToGroupCounts = searchTermToGroupCounts;
      this.contentStrings = contentStrings;
    }
  }

  private static class NullComparator implements Comparator<Comparable<?>> {

    @SuppressWarnings({"unchecked","rawtypes"})
    public int compare(Comparable a, Comparable b) {
      if (a == b) {
        return 0;
      } else if (a == null) {
        return -1;
      } else if (b == null) {
        return 1;
      } else {
        return a.compareTo(b);
      }
    }

  }

}