text:
#include "DTK_ConfigDefs.hpp"
#include "DTK_Types.h"

#include "PointCloudProblemGenerator/ExodusProblemGenerator.hpp"
#include "PointCloudProblemGenerator/PointCloudProblemGenerator.hpp"
#include <ArborX.hpp>
#include <DTK_NearestNeighborOperator.hpp>
#include <DTK_ParallelTraits.hpp>

#include <Kokkos_View.hpp>

#include <Teuchos_Array.hpp>
#include <Teuchos_CommHelpers.hpp>
#include <Teuchos_DefaultComm.hpp>
#include <Teuchos_RCP.hpp>
#include <Teuchos_UnitTestHarness.hpp>

#include <Tpetra_Export.hpp>
#include <Tpetra_Import.hpp>
#include <Tpetra_Map.hpp>
#include <Tpetra_Vector.hpp>

#include <cmath>
#include <limits>
#include <string>

//---------------------------------------------------------------------------//
// Brute-force calculate the nearest neighbors
template <class Device, class... CoordViewProperties>
void computeNeighborsBruteForce(
    const Teuchos::RCP<const Teuchos::Comm<int>> &comm,
    const Kokkos::View<DataTransferKit::Coordinate **, CoordViewProperties...>
        &src_coords,
    const Kokkos::View<DataTransferKit::Coordinate **, CoordViewProperties...>
        &tgt_coords,
    Kokkos::View<DataTransferKit::Coordinate **, Kokkos::LayoutLeft, Device>
        &src_nearest_coords )
{
    // Copy coordinates to the host.
    size_t num_src = src_coords.extent( 0 );
    size_t num_tgt = tgt_coords.extent( 0 );
    size_t space_dim = src_coords.extent( 1 );
    auto host_sources = Kokkos::create_mirror_view( src_coords );
    Kokkos::deep_copy( host_sources, src_coords );
    auto host_targets = Kokkos::create_mirror_view( tgt_coords );
    Kokkos::deep_copy( host_targets, tgt_coords );

    // Get the number of source points on each rank.
    int comm_size = comm->getSize();
    int comm_rank = comm->getRank();
    Teuchos::Array<size_t> rank_num_src( comm_size, 0 );
    rank_num_src[comm_rank] = num_src;
    Teuchos::Array<size_t> local_sizes( comm_size, 0 );
    Teuchos::reduceAll( *comm, Teuchos::REDUCE_SUM, comm_size,
                        rank_num_src.getRawPtr(), local_sizes.getRawPtr() );

    // Compute the global number of source points.
    size_t num_global = 0;
    for ( const auto &l : local_sizes )
        num_global += l;

    // Calculate rank offsets.
    Teuchos::Array<size_t> offsets( comm_size, 0 );
    for ( int i = 1; i < comm_size; ++i )
    {
        offsets[i] = offsets[i - 1] + local_sizes[i - 1];
    }

    // Allocate a send and receive view for creating a global list of source
    // coordinates.
    Kokkos::View<DataTransferKit::Coordinate **, Kokkos::LayoutLeft, Device>
        rank_src_coords( "rank_src_coords", num_global, space_dim );
    Kokkos::View<DataTransferKit::Coordinate **, Kokkos::LayoutLeft, Device>
        all_src_coords( "src_nearest_coords", num_global, space_dim );

    // Copy the local rank source coordinates into the appropriate spot in the
    // send buffer for the global source coordinate view.
    for ( size_t d = 0; d < space_dim; ++d )
    {
        auto src_subview = Kokkos::subview( host_sources, Kokkos::ALL, d );
        auto send_subview = Kokkos::subview( rank_src_coords, Kokkos::ALL, d );
        std::copy( src_subview.data(), src_subview.data() + num_src,
                   send_subview.data() + offsets[comm_rank] );
    }

    // Reduce source coordinate data across all ranks to build a
    // globally-replicated source coordinate view.
    for ( size_t d = 0; d < space_dim; ++d )
    {
        auto send_subview = Kokkos::subview( rank_src_coords, Kokkos::ALL, d );
        auto recv_subview = Kokkos::subview( all_src_coords, Kokkos::ALL, d );
        Teuchos::reduceAll<int, DataTransferKit::Coordinate>(
            *comm, Teuchos::REDUCE_SUM, Teuchos::as<int>( num_global ),
            send_subview.data(), recv_subview.data() );
    }

    // Allocate the nearest neighbors array.
    src_nearest_coords =
        Kokkos::View<DataTransferKit::Coordinate **, Kokkos::LayoutLeft,
                     Device>( "src_nearest_coords", num_tgt, space_dim );

    // Use a brute-force method to find the nearest neighbors. Compute the
    // distance for each target one at a time compared to all source points.
    double distance;
    for ( size_t t = 0; t < num_tgt; ++t )
    {
        // Reset the minimum distance for this target.
        double min_distance = std::numeric_limits<double>::max();

        // Check each source to see if it is the closest.
        for ( size_t s = 0; s < num_global; ++s )
        {
            // Compute the distance between the source and target.
            distance = 0.0;
            for ( size_t d = 0; d < space_dim; ++d )
                distance += ( host_targets( t, d ) - all_src_coords( s, d ) ) *
                            ( host_targets( t, d ) - all_src_coords( s, d ) );
            distance = std::sqrt( distance );

            // If the distance is a new minimum for this target set the
            // source coordinates as the new nearest and update the new
            // minimum distance.
            if ( distance < min_distance )
            {
                for ( size_t d = 0; d < space_dim; ++d )
                    src_nearest_coords( t, d ) = all_src_coords( s, d );
                min_distance = distance;
            }
        }
    }
}

//---------------------------------------------------------------------------//
// Test a problem that is uniquely owned. Returns a view of ints in the
// target decomposition indicating success/failure for each point.
template <class... ViewProperties>
void testUniquelyOwnedProblem(
    const Kokkos::View<DataTransferKit::Coordinate **, ViewProperties...>
        &src_coords,
    const Kokkos::View<double **, ViewProperties...> &src_field,
    const Kokkos::View<DataTransferKit::Coordinate **, ViewProperties...>
        &tgt_coords,
    const Kokkos::View<double **, ViewProperties...> &tgt_field, bool &success,
    Teuchos::FancyOStream &out )
{
    // NOTE following line is there to get rid of unused parameter warning.
    // However, I am not sure why this is a parameter at all since the exact
    // answer is computed via brute force search of the nearest neighbor and
    // applying the field function space onto the coordinates of that nearest
    // neighbor.
    std::ignore = tgt_field;

    // Types.
    using CoordView =
        Kokkos::View<DataTransferKit::Coordinate **, ViewProperties...>;
    using Device = typename CoordView::device_type;
    using ExecutionSpace = typename Device::execution_space;

    // Get the communicator.
    auto comm = Teuchos::DefaultComm<int>::getComm();

    // Sizes
    size_t space_dim = src_coords.extent( 1 );
    size_t num_src = src_coords.extent( 0 );
    size_t num_tgt = tgt_coords.extent( 0 );

    // Data function.
    auto data_func = KOKKOS_LAMBDA( const double x, const double y,
                                    const double z, const int comp )
    {
        return x * ( comp + 1 ) + y * ( comp + 2 ) + z * ( comp + 3 );
    };

    // Generate a source field.
    auto fill_src = KOKKOS_LAMBDA( const size_t i )
    {
        src_field( i, 0 ) = data_func( src_coords( i, 0 ), src_coords( i, 1 ),
                                       src_coords( i, 2 ), 0 );
    };
    Kokkos::parallel_for( Kokkos::RangePolicy<ExecutionSpace>( 0, num_src ),
                          fill_src );
    Kokkos::fence();

    // For now we copy the coordinates to the device default layout for
    // compatibility with the current operator definition.
    Kokkos::View<DataTransferKit::Coordinate **, Device> src_coords_device(
        "src_coords_device", num_src, space_dim );
    Kokkos::deep_copy( src_coords_device, src_coords );

    Kokkos::View<DataTransferKit::Coordinate **, Device> tgt_coords_device(
        "tgt_coords_device", num_tgt, space_dim );
    Kokkos::deep_copy( tgt_coords_device, tgt_coords );

    // Create a nearest neighbor operator.
    DataTransferKit::NearestNeighborOperator<Device> nearest_op(
        *( Teuchos::rcp_dynamic_cast<Teuchos::MpiComm<int> const>( comm )
               ->getRawMpiComm() ),
        src_coords_device, tgt_coords_device );

    // For now we copy the source field and create a target field with views
    // that use the default layout for the device type. Also note that we are
    // only using a single field dimension to be compatible with the current
    // operator definition.
    Kokkos::View<double *, Device> src_field_device( "src_field_device",
                                                     num_src );
    auto src_field_copy = KOKKOS_LAMBDA( const size_t i )
    {
        src_field_device( i ) = src_field( i, 0 );
    };
    Kokkos::parallel_for( Kokkos::RangePolicy<ExecutionSpace>( 0, num_src ),
                          src_field_copy );
    Kokkos::fence();

    Kokkos::View<double *, Device> tgt_field_device( "tgt_field_device",
                                                     num_tgt );

    // Apply the operator.
    nearest_op.apply( src_field_device, tgt_field_device );

    // Create the expected nearest coordinates.
    Kokkos::View<DataTransferKit::Coordinate **, Kokkos::LayoutLeft, Device>
        nearest_src_coords;
    computeNeighborsBruteForce( comm, src_coords, tgt_coords,
                                nearest_src_coords );

    // Check that the field evaluated at the points found via brute-force
    // search is the same as those computed by the operator.
    auto host_tgt_field = Kokkos::create_mirror_view( tgt_field_device );
    Kokkos::deep_copy( host_tgt_field, tgt_field_device );
    double data_val;
    for ( size_t t = 0; t < num_tgt; ++t )
    {
        data_val =
            data_func( nearest_src_coords( t, 0 ), nearest_src_coords( t, 1 ),
                       nearest_src_coords( t, 2 ), 0 );
        TEST_FLOATING_EQUALITY( host_tgt_field( t ), data_val, 1.0e-12 );
    }
}

//---------------------------------------------------------------------------//
// Partition the grids one-to-one
TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ExodusProblemGenerator, one_to_one, Node )
{
    // Type aliases.
    using DeviceType = typename Node::device_type;
    using Scalar = double;

    // Get the communicator.
    auto comm = Teuchos::DefaultComm<int>::getComm();

    // Create a problem generator. Two tet meshes of a sphere centered at
    // (0,0,0) with a radius of 10 are used. The source mesh is represented by
    // a fine tet mesh and the target mesh is represented by a coarse tet mesh.
    std::string src_exodus_file = "fine_sphere.exo";
    std::string tgt_exodus_file = "coarse_sphere.exo";
    DataTransferKit::ExodusProblemGenerator<Scalar, DeviceType, DeviceType>
        generator(
            *( Teuchos::rcp_dynamic_cast<Teuchos::MpiComm<int> const>( comm )
                   ->getRawMpiComm() ),
            src_exodus_file, tgt_exodus_file );

    // Generate a uniquely owned problem.
    Kokkos::View<DataTransferKit::Coordinate **, Kokkos::LayoutLeft, DeviceType>
        src_coords;
    Kokkos::View<DataTransferKit::Coordinate **, Kokkos::LayoutLeft, DeviceType>
        tgt_coords;
    Kokkos::View<Scalar **, Kokkos::LayoutLeft, DeviceType> src_field;
    Kokkos::View<Scalar **, Kokkos::LayoutLeft, DeviceType> tgt_field;
    generator.createUniquelyOwnedProblem( src_coords, src_field, tgt_coords,
                                          tgt_field );

    // Test the problem.
    testUniquelyOwnedProblem( src_coords, src_field, tgt_coords, tgt_field,
                              success, out );
}

//---------------------------------------------------------------------------//
// Partition the grids with standard ghosting from connectivity.
TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( ExodusProblemGenerator, ghosted, Node )
{
    // Type aliases.
    using DeviceType = typename Node::device_type;
    using Scalar = double;
    using TpetraMap = Tpetra::Map<int, DataTransferKit::GlobalOrdinal, Node>;
    using TpetraCoordVector =
        Tpetra::MultiVector<DataTransferKit::Coordinate, int,
                            DataTransferKit::GlobalOrdinal, Node>;
    using TpetraScalarVector =
        Tpetra::MultiVector<Scalar, int, DataTransferKit::GlobalOrdinal, Node>;

    // Get the communicator.
    auto comm = Teuchos::DefaultComm<int>::getComm();

    // Create a problem generator. Two tet meshes of a sphere centered at
    // (0,0,0) with a radius of 10 are used. The source mesh is represented by
    // a fine tet mesh and the target mesh is represented by a coarse tet mesh.
    std::string src_exodus_file = "fine_sphere.exo";
    std::string tgt_exodus_file = "coarse_sphere.exo";
    DataTransferKit::ExodusProblemGenerator<Scalar, DeviceType, DeviceType>
        generator(
            *( Teuchos::rcp_dynamic_cast<Teuchos::MpiComm<int> const>( comm )
                   ->getRawMpiComm() ),
            src_exodus_file, tgt_exodus_file );

    // Generate a ghosted owned problem.
    Kokkos::View<DataTransferKit::Coordinate **, Kokkos::LayoutLeft, DeviceType>
        src_coords;
    Kokkos::View<DataTransferKit::Coordinate **, Kokkos::LayoutLeft, DeviceType>
        tgt_coords;
    Kokkos::View<DataTransferKit::GlobalOrdinal *, Kokkos::LayoutLeft,
                 DeviceType>
        src_gids;
    Kokkos::View<DataTransferKit::GlobalOrdinal *, Kokkos::LayoutLeft,
                 DeviceType>
        tgt_gids;
    Kokkos::View<Scalar **, Kokkos::LayoutLeft, DeviceType> src_field;
    Kokkos::View<Scalar **, Kokkos::LayoutLeft, DeviceType> tgt_field;
    generator.createGhostedProblem( src_coords, src_gids, src_field, tgt_coords,
                                    tgt_gids, tgt_field );

    // Create Tpetra maps from the problem global ids.
    auto invalid_ordinal =
        Teuchos::OrdinalTraits<DataTransferKit::GlobalOrdinal>::invalid();
    Teuchos::RCP<const TpetraMap> src_ghost_map =
        Teuchos::rcp( new TpetraMap( invalid_ordinal, src_gids, 0, comm ) );
    Teuchos::RCP<const TpetraMap> tgt_ghost_map =
        Teuchos::rcp( new TpetraMap( invalid_ordinal, tgt_gids, 0, comm ) );

    // Create Tpetra vectors from the ghosted problem coordinates.
    TpetraCoordVector src_coords_ghost( src_ghost_map, src_coords );
    TpetraCoordVector tgt_coords_ghost( tgt_ghost_map, tgt_coords );
    TpetraScalarVector src_field_ghost( src_ghost_map, src_field );
    TpetraScalarVector tgt_field_ghost( tgt_ghost_map, tgt_field );

    // Create unique Tpetra vectors for coordinates and fields.
    auto src_unique_map = Tpetra::createOneToOne( src_ghost_map );
    auto tgt_unique_map = Tpetra::createOneToOne( tgt_ghost_map );
    int space_dim = src_coords.extent( 1 );
    int field_ncomp = src_field.extent( 1 );
    TpetraCoordVector src_coords_unique( src_unique_map, space_dim );
    TpetraCoordVector tgt_coords_unique( tgt_unique_map, space_dim );
    TpetraScalarVector src_field_unique( src_unique_map, field_ncomp );
    TpetraScalarVector tgt_field_unique( tgt_unique_map, field_ncomp );

    // Export the ghosted problem coordinates to a uniquely owned
    // decomposition.
    auto src_ghost_to_unique_exporter =
        Tpetra::createExport( src_ghost_map, src_unique_map );
    auto tgt_ghost_to_unique_exporter =
        Tpetra::createExport( tgt_ghost_map, tgt_unique_map );
    src_coords_unique.doExport( src_coords_ghost, *src_ghost_to_unique_exporter,
                                Tpetra::REPLACE );
    tgt_coords_unique.doExport( tgt_coords_ghost, *tgt_ghost_to_unique_exporter,
                                Tpetra::REPLACE );
    src_field_unique.doExport( src_field_ghost, *src_ghost_to_unique_exporter,
                               Tpetra::REPLACE );
    tgt_field_unique.doExport( tgt_field_ghost, *tgt_ghost_to_unique_exporter,
                               Tpetra::REPLACE );

    // Test the uniquely owned problem.
    auto src_coords_unique_view =
        src_coords_unique.template getLocalView<DeviceType>();
    auto tgt_coords_unique_view =
        tgt_coords_unique.template getLocalView<DeviceType>();
    auto src_field_unique_view =
        src_field_unique.template getLocalView<DeviceType>();
    auto tgt_field_unique_view =
        tgt_field_unique.template getLocalView<DeviceType>();
    testUniquelyOwnedProblem( src_coords_unique_view, src_field_unique_view,
                              tgt_coords_unique_view, tgt_field_unique_view,
                              success, out );
}

//---------------------------------------------------------------------------//

// Include the test macros.
#include "DataTransferKit_ETIHelperMacros.h"

// Create the test group
#define UNIT_TEST_GROUP( NODE )                                                \
    TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ExodusProblemGenerator, one_to_one,  \
                                          NODE )                               \
    TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( ExodusProblemGenerator, ghosted,     \
                                          NODE )

// Demangle the types
DTK_ETI_MANGLING_TYPEDEFS()

// Instantiate the tests
DTK_INSTANTIATE_N( UNIT_TEST_GROUP )


text:
<?php

/**
 * @file
 * Contains \Drupal\Core\TypedData\Plugin\DataType\Integer.
 */

namespace Drupal\Core\TypedData\Plugin\DataType;

use Drupal\Core\TypedData\Annotation\DataType;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\TypedData\PrimitiveBase;
use Drupal\Core\TypedData\Type\IntegerInterface;

/**
 * The integer data type.
 *
 * The plain value of an integer is a regular PHP integer. For setting the value
 * any PHP variable that casts to an integer may be passed.
 *
 * @DataType(
 *   id = "integer",
 *   label = @Translation("Integer")
 * )
 */
class Integer extends PrimitiveBase implements IntegerInterface {

}

redpajama_set_name: RedPajamaGithub


text:
package com.hazelcast.replicatedmap.impl.operation;

import com.hazelcast.internal.nio.IOUtil;
import com.hazelcast.logging.ILogger;
import com.hazelcast.cluster.Address;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.internal.serialization.Data;
import com.hazelcast.replicatedmap.impl.ReplicatedMapEventPublishingService;
import com.hazelcast.replicatedmap.impl.ReplicatedMapService;
import com.hazelcast.replicatedmap.impl.record.ReplicatedRecordStore;
import com.hazelcast.spi.impl.operationservice.PartitionAwareOperation;
import com.hazelcast.spi.impl.operationservice.impl.OperationServiceImpl;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * This operation will update the local record store with the update received from local/remote partition owner.
 */
public class ReplicateUpdateToCallerOperation extends AbstractSerializableOperation implements PartitionAwareOperation {

    private String name;
    private long callId;
    private Data dataKey;
    private Data dataValue;
    private VersionResponsePair response;
    private long ttl;
    private boolean isRemove;

    public ReplicateUpdateToCallerOperation() {
    }

    public ReplicateUpdateToCallerOperation(String name, long callId, Data dataKey, Data dataValue, VersionResponsePair response,
                                            long ttl, boolean isRemove) {
        this.name = name;
        this.callId = callId;
        this.dataKey = dataKey;
        this.dataValue = dataValue;
        this.response = response;
        this.ttl = ttl;
        this.isRemove = isRemove;
    }

    @Override
    public void run() throws Exception {
        ILogger logger = getLogger();
        ReplicatedMapService service = getService();
        ReplicatedRecordStore store = service.getReplicatedRecordStore(name, true, getPartitionId());
        long currentVersion = store.getVersion();
        long updateVersion = response.getVersion();
        if (currentVersion >= updateVersion) {
            if (logger.isFineEnabled()) {
                logger.fine("Rejecting stale update received for replicated map '" + name + "' (partitionId " + getPartitionId()
                        + ") (current version " + currentVersion + ") (update version " + updateVersion + ")");
            }
            return;
        }
        Object key = store.marshall(dataKey);
        Object value = store.marshall(dataValue);
        if (isRemove) {
            store.removeWithVersion(key, updateVersion);
        } else {
            store.putWithVersion(key, value, ttl, TimeUnit.MILLISECONDS, true, updateVersion);
        }

        publishEvent();
    }

    @Override
    public void afterRun() throws Exception {
        notifyCaller();
    }

    private void publishEvent() {
        ReplicatedMapService service = getService();
        ReplicatedMapEventPublishingService eventPublishingService = service.getEventPublishingService();
        Address thisAddress = getNodeEngine().getThisAddress();
        Data dataOldValue = getNodeEngine().toData(response.getResponse());
        if (isRemove) {
            eventPublishingService.fireEntryListenerEvent(dataKey, dataOldValue, null, name, thisAddress);
        } else {
            eventPublishingService.fireEntryListenerEvent(dataKey, dataOldValue, dataValue, name, thisAddress);
        }
    }

    private void notifyCaller() {
        OperationServiceImpl operationService = (OperationServiceImpl) getNodeEngine().getOperationService();
        operationService.getBackupHandler().notifyBackupComplete(callId);
    }

    @Override
    protected void writeInternal(ObjectDataOutput out) throws IOException {
        out.writeUTF(name);
        out.writeLong(callId);
        IOUtil.writeData(out, dataKey);
        IOUtil.writeData(out, dataValue);
        response.writeData(out);
        out.writeLong(ttl);
        out.writeBoolean(isRemove);
    }

    @Override
    protected void readInternal(ObjectDataInput in) throws IOException {
        name = in.readUTF();
        callId = in.readLong();
        dataKey = IOUtil.readData(in);
        dataValue = IOUtil.readData(in);
        response = new VersionResponsePair();
        response.readData(in);
        ttl = in.readLong();
        isRemove = in.readBoolean();
    }

    @Override
    public int getClassId() {
        return ReplicatedMapDataSerializerHook.REPLICATE_UPDATE_TO_CALLER;
    }
}


text:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Collections.Specialized;
using Axiom.Core;

namespace Multiverse.Tools.WorldEditor
{
	public class ColorValueUITypeEditor : UITypeEditor
	{
		public string type;

		private Color ColorExToColor(ColorEx cx)
		{
			return Color.FromArgb((int)cx.ToARGB());
		}

		private ColorEx ColorToColorEx(Color c)
		{
			return new ColorEx(c.A / 255f, c.R / 255f, c.G / 255f, c.B / 255f);
		}

		public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
		{
			ColorEx color = (ColorEx) value;
			using (ColorDialog colorDialog = new ColorDialog())
			{
				colorDialog.AllowFullOpen = true;
				colorDialog.AnyColor = true;
				colorDialog.FullOpen = true;
				colorDialog.ShowHelp = true;
				colorDialog.Color = ColorExToColor(color);
                int colorabgr = (int)color.ToABGR();
				colorabgr &= 0x00ffffff;
				int[] colorsabgr = new int[1];
				colorsabgr[0] = colorabgr;
				colorDialog.CustomColors = colorsabgr;
				DialogResult result = colorDialog.ShowDialog();
				if (result == DialogResult.OK)
				{
					color = ColorToColorEx(colorDialog.Color);
				}
				colorDialog.Color = ColorExToColor(color);
			}
			return color;
		}

		public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
		{
			if (context != null && context.Instance != null)
			{
				return UITypeEditorEditStyle.Modal;
			}
			return base.GetEditStyle(context);
		}

		public class ColorValueUITypeEditorFog : ColorValueUITypeEditor
		{
			public ColorValueUITypeEditorFog()
			{
				type = "Fog";
			}
		}

		public class ColorValueUITypeEditorPlantType : ColorValueUITypeEditor
		{
			public ColorValueUITypeEditorPlantType()
			{
				type = "Plant";
			}
		}

        public class ColorValueUITypeEditorGlobalDirectionalLight : ColorValueUITypeEditor
        {
            public ColorValueUITypeEditorGlobalDirectionalLight()
            {
                type = "GlobalDirectionalLight";
            }
        }

        public class ColorValueUITypeEditorGlobalFog : ColorValueUITypeEditor
        {
            public ColorValueUITypeEditorGlobalFog()
            {
                type = "GlobalFog";
            }
        }

        public class ColorValueUITypeEditorPointLight : ColorValueUITypeEditor
        {
            public ColorValueUITypeEditorPointLight()
            {
                type = "PointLight";
            }
        }
	}
}


text:
title: Yarn
localeTitle: пряжа
---
## пряжа

Пряжа - это менеджер пакетов для вашего кода. Он позволяет использовать и распространять код с другими разработчиками со всего мира. Пряжа делает это быстро, надежно и надежно, поэтому вам никогда не придется беспокоиться о зависимостях проекта.

Пряжа позволяет вам использовать решения других разработчиков для различных проблем, что упрощает разработку вашего программного обеспечения. Если у вас есть проблемы, вы можете сообщить о проблемах или внести свой вклад, и когда проблема будет устранена, вы можете использовать пряжу, чтобы поддерживать ее в актуальном состоянии.

Код разделяется через нечто, называемое пакетом (иногда называемым модулем). Пакет содержит весь совместно используемый код, а также файл package.json, который описывает пакет.

Чтобы использовать пряжу, вы должны сначала установить ее в своей системе. В конце этой статьи есть ссылки, которые помогут вам сделать это независимо от вашей операционной системы.

Когда у вас установлена пряжа, вы можете начать ее использовать. Вот некоторые из наиболее распространенных команд, которые вам понадобятся.

### пряжа Использование

**Запуск нового проекта**
```
yarn init
```

Команда `yarn init` откроет интерактивную форму для создания проекта пряжи. `yarn init` создает файл `package.json` котором хранится информация о вашем проекте. Эта интерактивная форма откроется со следующими вопросами:
```
name (your-project):
 version (1.0.0):
 description:
 entry point (index.js):
 git repository:
 author:
 license (MIT):
```

Вы можете либо набрать ответы для каждой опции, либо просто нажать «Ввод», не набрав ничего, чтобы использовать значение по умолчанию или оставить пустым. Вы всегда можете войти в свой любимый текстовый редактор, чтобы изменить файл `package.json` , если это необходимо.

Ваш файл `package.json` должен выглядеть примерно так:
```
{
  "name": "your-new-project",
  "version": "1.0.0",
  "description": "A description of your new project.",
  "main": "index.js",
  "repository": {
    "url": "https://github.com/your-username/your-new-project",
    "type": "git"
  },
  "author": "Your Name <your_name@example.com>",
  "license": "MIT"
 }
```

**Добавление зависимости**
```
yarn add [package]
```

**Модернизация зависимости**
```
yarn upgrade [package]
```

**Удаление зависимости**
```
yarn remove [package]
```

**Установка всех зависимостей проекта**
```
yarn install
```

#### Дополнительная информация:

*   [Сайт пряжи](https://yarnpkg.com)
*   [Пряжа документация](https://yarnpkg.com/en/docs)
*   [Установка пряжи](https://yarnpkg.com/en/docs/install)
*   [Пряжа против npm](https://www.pluralsight.com/guides/node-js/yarn-a-package-manager-for-node-js)


text:
'use strict';

// MODULES //

var isObject = require( '@stdlib/assert/is-plain-object' );
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var format = require( '@stdlib/string/format' );

// MAIN //

/**
* Validates function options.
*
* @private
* @param {Object} opts - destination object
* @param {Options} options - function options
* @param {string} [options.dir] - root directory from which to search for test files
* @param {string} [options.global] - global coverage variable
* @returns {(Error|null)} error object or null
*
* @example
* var opts = {};
* var options = {
*     'coverage': '__coverage__'
* };
*
* var err = validate( opts, options );
* if ( err ) {
*    throw err;
* }
*/
function validate( opts, options ) {
	if ( !isObject( options ) ) {
		return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
	}
	if ( hasOwnProp( options, 'dir' ) ) {
		opts.dir = options.dir;
		if ( !isString( opts.dir ) ) {
			return new TypeError( format( 'invalid option. `%s` option must be a string. Option: `%s`.', 'dir', opts.dir ) );
		}
	}
	if ( hasOwnProp( options, 'global' ) ) {
		opts.global = options.global;
		if ( !isString( opts.global ) ) {
			return new TypeError( format( 'invalid option. `%s` option must be a string. Option: `%s`.', 'global', opts.global ) );
		}
	}
	return null;
}

// EXPORTS //

module.exports = validate;


text:
package com.google.devtools.depan.matchers.eclipse.ui.widgets;

/**
 * A listener for modification made on a RelationshipPicker.
 *
 * @author ycoppel@google.com (Yohann Coppel)
 *
 * @param <E> type of elements in the list.
 * @param <V> type of elements that can change (might be Object if different
 *        objects types can change).
 */
public interface ModificationListener<E, V> {

  /**
   * Callback for a modification on the element E, when property,
   * is set to value.
   *
   * @param element the modified element
   * @param property the changed property
   * @param value the new value
   */
  public void modify(E element, String property, V value);
}


text:
package farmsim.inventory;

import java.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import farmsim.resource.SimpleResource;

/**
 * Singleton Storage instance used for storing managing items
 *
 * @author gelbana
 *
 */
public class Storage extends Observable {
	Logger LOGGER = LoggerFactory.getLogger(Storage.class);
	private ArrayList<SimpleResource> itemList = new ArrayList<SimpleResource>();
	private HashMap<String, Integer> trackingList = new HashMap<String, Integer>();
	private Resources resources = new Resources();
	private int currentLevel;
	int[] toolLevel = { 10, 20, 30, 40, 50 }; // To be implemented
	ArrayList<Integer> storageLevel; // To be implemented

	public Storage() {
		currentLevel = 20; // To be implemented
	}

	/**
	 * Adds item to the storage. If storage is full, prints error to console and
	 * returns non zero
	 *
	 * @return 0 if successful, else 1
	 */
	public int addItem(SimpleResource item) {
		int stack = 0; // Flag whether to add to existing stack or not
		if (itemList.size() < currentLevel) {
			for (int i = 0; i < itemList.size(); i++) { // Check for existing
				if (itemList.get(i).equalsType(item)) {
					itemList.get(i).addToStack(item); // Add to stack
					stack = 1;
					trackUpdate(); // Update tracking list
					setChanged();
				    notifyObservers();
					return 0;
				}
			}
			if (stack != 1) { // If not in storage, add
				itemList.add(item);
				trackUpdate(); // Update tracking list
				setChanged();
			    notifyObservers();
				return 0;
			}
		} else { // Error if storage is full
			LOGGER.info("Storage is full");
			return 1;
		}
		return 1;
	}

	/**
	 * Returns quantity of specified item in the storage. If it does not exist
	 * returns 0
	 *
	 * @return quantity of item in storage
	 */
	public int getQuantity(SimpleResource item) {
		for (int i = 0; i < itemList.size(); i++) {
			if (itemList.get(i).getType().equals(item.getType())) { // Search for item
				return itemList.get(i).getQuantity();
			}
		}
		LOGGER.info("Item is not in storage");
		return 0;

	}

	/**
	 * Returns list
	 *
	 * @return list of items
	 */
	public ArrayList<SimpleResource> getList() {
		return itemList;

	}

	/**
	 *
	 * @return Returns quantity of items requested. If too many requested,
	 *         returns max. If item does not exist, return null. NOT WORKING AT
	 *         THE MOMENT
	 */
	public SimpleResource takeItem(SimpleResource item, int quantity) {
		for (int i = 0; i < itemList.size(); i++) {
			if (itemList.get(i).equals(item)) {
				setChanged();
			    notifyObservers();
				return itemList.get(i).split(quantity);
			}
		}
		LOGGER.info("Item is not in storage");
		return null;
	}

	/**
	 * Gets amount of items in storage
	 *
	 * @return number of items in storage
	 */
	public int getSize() {
		return itemList.size();
	}

	/**
	 * Get current level of storage
	 *
	 * @return current level of storage UNIMPLEMENTED
	 */
	public int getLevel() {
		return this.currentLevel;
	}

	/**
	 * Set level of storage
	 */
	public void setLevel(int level) {
		if (level > 0) {
			this.currentLevel = level;
		}
	}

	/**
	 * Removes item from storage with specified quantity. IT GETS DESTROYED.
	 *
	 * @return 0 if successful, non zero if unsuccessful
	 */
	public int removeItem(SimpleResource item, int quantity) {
		int existingQuantity = 0;

		for (int i = 0; i < itemList.size(); i++) {
			if (itemList.get(i).getType().equals(item.getType())) { // Storage contains item
				existingQuantity = itemList.get(i).getQuantity();
				if (quantity == 0) {
					return 2;
				} else if (existingQuantity < quantity){
					return 1;
				} else if (quantity == existingQuantity) {
					itemList.remove(i);
					setChanged();
				    notifyObservers();
					return 0;
				} else {
					itemList.set(i, itemList.get(i).split(existingQuantity - quantity));
					setChanged();
				    notifyObservers();
					return 0;
				}
			}
		}
		return 3;
	}

	public boolean containsItem(SimpleResource item) {
	    for (int i = 0; i < itemList.size(); i++) {
	        if (itemList.get(i).equalsType(item) && itemList.get(i).getQuantity() >= item.getQuantity()) {
	            return true;
	        }
	    }
	    return false;
	}

	/**
	 * Swap two items in the item list. Used for storage UI purposes.
	 */
	public void swapItem(int position1, int position2) {
		Collections.swap(itemList, position1, position2);
	}

	/**
	 * Add item to tracking list to be display on tab for easy access to
	 * tracked resource count
	 *
	 * @return 0 is successful, 1 if already exists or is full
	 */
	public int trackAdd(SimpleResource item) {
		String addType = item.getType();
		int quantity = item.getQuantity();

		if (!trackingList.containsKey(addType) && trackingList.size() < 4) {
			trackingList.put(addType, quantity);
			return 0;
		}
		return 1;

	}

	/**
	 * Removes item from tracking list if it exists
	 *
	 * @return 0 if successful, 1 otherwise
	 */
	public int trackRemove(SimpleResource item) {
		String addType = item.getType();
		int quantity = item.getQuantity();

		if (trackingList.containsKey(addType)) {
			trackingList.remove(addType, quantity);
			return 0;
		}
		return 1;

	}
	/**
	 * Updates tracking list when item is added or removed
	 */
	private void trackUpdate() {
		for (SimpleResource item : itemList) {
			if (trackingList.containsKey(item.getType()))
				trackingList.replace(item.getType(), item.getQuantity());
		}
	}
}


text:
# Notes

## Keyboard input

https://unix.stackexchange.com/questions/116629/how-do-keyboard-input-and-text-output-work
https://unix.stackexchange.com/questions/12510/relationship-of-keyboard-layout-and-xmodmap/12518#12518

Windows mapping code partially based on the original code in NuclearWinter,
modified with the generous help of the MSDN documentation on MapVirtualKeyEx
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646307(v=vs.85).aspx

## Todo

Replace the focus management with something central. Keyboard focus is central
to keyboard driven UIs and the current code makes this very hard to achieve.
Extend the FocusManager to handle focus events as fallback handler (ie if the
widget itself did not consume the event).

Focus cycling (via tab, shift-tab) should work out of the box. Lets model it
after Java/Swing's model, as it behaves solidly:

- Components can elect to be focusable
- Components can declare themselves a focus-cycle-root
- Navigation rules

  Using the currently focused widget as starting point, first test all
  child widgets recursively, using a depth-first strategy. If no widget
  has been found yet and the current widget is not a focus cycle root,
  try again by searching the sibling widget. When the current widget is
  a focus cycle root, start searching the first child until the search
  has evaluated all possible widgets, then abort and do not move the focus.

  Sibling widgets are traversed in layout sort order. For that sort
  widgets by their y, then x coordinate. This will move the focus along
  rows and columns in the UI.


text:
namespace Halley {
    class Polygon;

    class Triangle {
    public:
        Vector2f a, b, c;

        Triangle() = default;
        Triangle(Vector2f a, Vector2f b, Vector2f c)
	        : a(a), b(b), c(c)
        {}

        Vector3f getBarycentricCoordinates(Vector2f p) const;
        Rect4f getBounds() const;
        bool contains(Vector2f p) const;
        float getDistance(Vector2f p) const;

    	float getArea() const;
        float getPerimeter() const;

        Polygon toPolygon() const;

        Circle getCircumscribedCircle() const;

        Triangle operator+(Vector2f v) const;
        Triangle operator-(Vector2f v) const;
        Triangle operator*(Vector2f v) const;
        Triangle operator/(Vector2f v) const;
        Triangle operator*(float scalar) const;
        Triangle operator/(float scalar) const;

        Triangle& operator+=(Vector2f v);
        Triangle& operator-=(Vector2f v);
        Triangle& operator*=(Vector2f v);
        Triangle& operator/=(Vector2f v);
        Triangle& operator*=(float scalar);
        Triangle& operator/=(float scalar);

        bool operator==(const Triangle& other) const;
        bool operator!=(const Triangle& other) const;
    };
}


text:
package leetcode;

/**
 * Given a number, find the next higher number using only
 * the digits in the given number. For example if the given
 * number is 1234, next higher number with same digits is 1243.
 * @author Debosmit
 *
 */
public class Find_Next_Higher_Number_With_Same_Digits {
	public int getNextHighest(int num) {
		StringBuilder val = new StringBuilder(""+num);

		int pivot;

		// get the digit that has to be switched
		for(pivot = val.length() - 2 ; pivot >=0 ; pivot--) {
			if(val.charAt(pivot) < val.charAt(pivot+1)) {
				// if everything to the right of this pivot
				// is sorted, the smallest of that subset of
				// digits will be at pivot+1; this digit will
				// not necessarily be greater than the digit at pivot;
				// swapping pivot with a number in the sorted set
				// that is greater than the pivot value will yield
				// the required number

				// need to sort indices [pivot+1, val.length()-1]
				// single pass radix sort would work since we have
				// only digits ranging from 0 to 9.

				// get digits for the remainder of the list
				int numDigits = val.length() - (pivot + 1);
				int[] digits = new int[numDigits];
				for(int i = 0 ; i < digits.length ; i++)
					digits[i] = val.charAt(i + (pivot + 1)) - '0';

				// sort the digits
				// note: sorting can also be replaced with reversing
				// order of digits since they must be a0 >= a1 >= a3 ...
				RadixSort Sort = new RadixSort();
				Sort.sort(digits);

				// swap index pivot with appropriate
				// value in digits that is greater than current
				// value of pivot;
				// after this process, array must remain sorted
				// since oldPivotValue is being put at the index of value
				// that is greater than it; it is also ensured that the
				// values at all indices before this are less than
				// or equal to the oldPivotValue
				int oldPivotVal = val.charAt(pivot) - '0';
				for(int i = 0 ; i < digits.length ; i++) {
					if(digits[i] > oldPivotVal) {
						val.setCharAt(pivot, (char)(digits[i] + '0'));
						digits[i] = oldPivotVal;
						break;
					}
				}

				// replace indices [pivot+1, val.length()-1] with
				// values from sorted array
				for(int i = 0 ; i < digits.length ; i++)
					val.setCharAt(i + (pivot + 1), (char)(digits[i] + '0'));
				break;
			}
		}
		// if number is as big as it can get, val stays unchanged
		return Integer.parseInt(val.toString());
	}

	public static void main(String[] args) {
		Find_Next_Higher_Number_With_Same_Digits Next = new
				Find_Next_Higher_Number_With_Same_Digits();

		int num = 12543;
		System.out.println("Num: " + num);
		System.out.println("Next_Higher_Number_With_Same_Digits: " + Next.getNextHighest(num));

		num = 13542;
		System.out.println("\n\nNum: " + num);
		System.out.println("Next_Higher_Number_With_Same_Digits: " + Next.getNextHighest(num));

		num = 136442;
		System.out.println("\n\nNum: " + num);
		System.out.println("Next_Higher_Number_With_Same_Digits: " + Next.getNextHighest(num));

		num = 776442;
		System.out.println("\n\nNum: " + num);
		System.out.println("Next_Higher_Number_With_Same_Digits: " + Next.getNextHighest(num));

		num = 1232;
		System.out.println("\n\nNum: " + num);
		System.out.println("Next_Higher_Number_With_Same_Digits: " + Next.getNextHighest(num));

	}
}


text:
'''OpenGL extension EXT.stereo_tree

This module customises the behaviour of the
OpenGL.raw.GLX.EXT.stereo_tree to provide a more
Python-friendly API

The official definition of this extension is available here:
http://www.opengl.org/registry/specs/EXT/stereo_tree.txt
'''
from OpenGL import platform, constant, arrays
from OpenGL import extensions, wrapper
import ctypes
from OpenGL.raw.GLX import _types, _glgets
from OpenGL.raw.GLX.EXT.stereo_tree import *
from OpenGL.raw.GLX.EXT.stereo_tree import _EXTENSION_NAME

def glInitStereoTreeEXT():
    '''Return boolean indicating whether this extension is available'''
    from OpenGL import extensions
    return extensions.hasGLExtension( _EXTENSION_NAME )

### END AUTOGENERATED SECTION


text:
package org.drools.compiler.integrationtests;

import java.io.InputStreamReader;
import org.drools.compiler.CommonTestMethodBase;
import org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl;
import org.drools.compiler.compiler.DescrBuildError;
import org.drools.compiler.compiler.DrlParser;
import org.drools.compiler.compiler.ParserError;
import org.drools.compiler.rule.builder.dialect.java.JavaDialectConfiguration;
import org.junit.Test;
import org.kie.api.io.ResourceType;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderError;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.builder.conf.LanguageLevelOption;
import org.kie.internal.io.ResourceFactory;

public class ParserTest extends CommonTestMethodBase {

    @Test
    public void testErrorLineNumbers() throws Exception {
        // this test aims to test semantic errors
        // parser errors are another test case
        final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newClassPathResource("errors_in_rule.drl", getClass()), ResourceType.DRL);

        final KnowledgeBuilderError[] errors = kbuilder.getErrors().toArray(new KnowledgeBuilderError[0]);
        assertEquals(3, errors.length);

        final DescrBuildError stiltonError = (DescrBuildError) errors[0];
        assertTrue(stiltonError.getMessage().contains("Stilton"));
        assertNotNull(stiltonError.getDescr());
        assertTrue(stiltonError.getLine() != -1);

        // check that its getting it from the ruleDescr
        assertEquals(stiltonError.getLine(), stiltonError.getDescr().getLine());
        // check the absolute error line number (there are more).
        assertEquals(26, stiltonError.getLine());

        final DescrBuildError poisonError = (DescrBuildError) errors[1];
        assertTrue(poisonError.getMessage().contains("Poison"));
        assertEquals(28, poisonError.getLine());

        final KnowledgeBuilderConfigurationImpl cfg = new KnowledgeBuilderConfigurationImpl();
        final JavaDialectConfiguration javaConf = (JavaDialectConfiguration) cfg.getDialectConfiguration("java");
        switch (javaConf.getCompiler()) {
            case JavaDialectConfiguration.NATIVE:
                assertTrue(errors[2].getMessage().contains("illegal"));
                break;
            case JavaDialectConfiguration.ECLIPSE:
                assertTrue(errors[2].getMessage().contains("add"));
                break;
            case JavaDialectConfiguration.JANINO:
                assertTrue(errors[2].getMessage().contains("Unexpected"));
                break;
            default:
                fail("Unknown compiler used");
        }

        // now check the RHS, not being too specific yet, as long as it has the
        // rules line number, not zero
        final DescrBuildError rhsError = (DescrBuildError) errors[2];
        assertTrue(rhsError.getLine() >= 23 && rhsError.getLine() <= 32);
    }

    @Test
    public void testErrorsParser() throws Exception {
        final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
        assertEquals(0, parser.getErrors().size());
        parser.parse(new InputStreamReader(getClass().getResourceAsStream("errors_parser_multiple.drl")));
        assertTrue(parser.hasErrors());
        assertTrue(parser.getErrors().size() > 0);
        assertTrue(parser.getErrors().get(0) instanceof ParserError);
        final ParserError first = ((ParserError) parser.getErrors().get(0));
        assertTrue(first.getMessage() != null);
        assertFalse(first.getMessage().equals(""));
    }

}


text:
using System;
using System.Diagnostics;
using System.IO;
using System.IO.MemoryMappedFiles;

namespace GigaFFT
{
    public class MMFArray32MeB : IDisposable
    {
        private static string namePrefix = Guid.NewGuid().ToString();
        private static ulong nameSuffix = 0;
        protected MemoryMappedFile[] files;
        protected ulong[] fileNameSuffixes;
        protected MemoryMappedViewAccessor mmvaLO;
        protected MemoryMappedViewAccessor mmvaHI;
        protected ushort immvaLO;
        protected ushort immvaHI;
        #if DEBUG
        private bool readMode;
        #endif

        public MMFArray32MeB(ulong totalCapacityBytes)
        {
            checked
            {
                Debug.Assert((totalCapacityBytes != 0) && ((totalCapacityBytes & (totalCapacityBytes - 1)) == 0), "Total Capacity must be power of 2");
                Debug.Assert((totalCapacityBytes >= 33554432UL) && (totalCapacityBytes <= 1099511627776UL), "Total Capacity must fit between 32MeB..1TeB");
                this.files = new MemoryMappedFile[totalCapacityBytes / 16777216UL]; // 2..65536 files 16MeB each
                this.fileNameSuffixes = new ulong[this.files.Length];
                Debug.Assert((ulong)this.files.Length * 16777216UL == totalCapacityBytes);
                for (ushort index = 0; index < (ushort)this.files.Length; index++)
                {
                    lock (namePrefix)
                    {
                        this.fileNameSuffixes[index] = nameSuffix++;
                    }
                    string fileName = namePrefix + "-" + this.fileNameSuffixes[index].ToString();
                    this.files[index] = MemoryMappedFile.CreateFromFile(@"d:\temp\" + fileName, FileMode.CreateNew, fileName, 16777216L, MemoryMappedFileAccess.ReadWrite);
                }
                this.immvaLO = 0;
                this.immvaHI = (ushort)(this.files.Length / 2);
                this.mmvaLO = this.files[immvaLO].CreateViewAccessor();
                this.mmvaHI = this.files[immvaHI].CreateViewAccessor();
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (mmvaLO != null || mmvaHI != null || files != null)
            {
                if (disposing)
                {
                    checked
                    {
                        mmvaLO.Dispose();
                        mmvaHI.Dispose();
                        for (ulong index = 0; index < (ulong)files.Length; index++ )
                        {
                            files[index].Dispose();
                            string fileName = @"d:\temp\" + namePrefix + "-" + this.fileNameSuffixes[index].ToString();
                            File.Delete(fileName);
                        }
                    }
                    mmvaLO = null;
                    mmvaHI = null;
                    files = null;
                }
            }
        }

        ~MMFArray32MeB()
        {
            Dispose(false);
        }
    }

}


text:
local cjson          = require "cjson"
local helpers        = require "spec.helpers"

local REDIS_HOST     = helpers.redis_host
local REDIS_PORT     = 6379
local REDIS_PASSWORD = ""
local REDIS_DATABASE = 1

local SLEEP_TIME = 0.01
local ITERATIONS = 10

local fmt = string.format

local proxy_client = helpers.proxy_client

local function wait()
  ngx.update_time()
  local now = ngx.now()
  local millis = (now - math.floor(now))
  ngx.sleep(1 - millis)
end

local function flush_redis()
  local redis = require "resty.redis"
  local red = redis:new()
  red:set_timeout(2000)
  local ok, err = red:connect(REDIS_HOST, REDIS_PORT)
  if not ok then
    error("failed to connect to Redis: " .. err)
  end

  if REDIS_PASSWORD and REDIS_PASSWORD ~= "" then
    local ok, err = red:auth(REDIS_PASSWORD)
    if not ok then
      error("failed to connect to Redis: " .. err)
    end
  end

  local ok, err = red:select(REDIS_DATABASE)
  if not ok then
    error("failed to change Redis database: " .. err)
  end

  red:flushall()
  red:close()
end

local function test_limit(path, host, limit)
  wait()
  limit = limit or ITERATIONS
  for i = 1, limit do
    local res = proxy_client():get(path, {
      headers = { Host = host:format(i) },
    })
    assert.res_status(200, res)
  end

  ngx.sleep(SLEEP_TIME) -- Wait for async timer to increment the limit

  local res = proxy_client():get(path, {
    headers = { Host = host:format(1) },
  })
  assert.res_status(429, res)
  assert.equal(limit, tonumber(res.headers["x-ratelimit-limit-video-second"]))
  assert.equal(0, tonumber(res.headers["x-ratelimit-remaining-video-second"]))
end

local function init_db(strategy, policy)
  local bp = helpers.get_db_utils(strategy, {
    "routes",
    "services",
    "plugins",
    "consumers",
    "keyauth_credentials",
  })

  if policy == "redis" then
    flush_redis()
  end

  return bp
end

for _, strategy in helpers.each_strategy() do
for _, policy in ipairs({"local", "cluster", "redis"}) do

describe(fmt("#flaky Plugin: response-ratelimiting (access) with policy: #%s [#%s]", policy, strategy), function()

  lazy_setup(function()
    local bp = init_db(strategy, policy)

    if policy == "local" then
      SLEEP_TIME = 0.001
    else
      SLEEP_TIME = 0.15
    end

    local consumer1 = bp.consumers:insert {custom_id = "provider_123"}
    bp.keyauth_credentials:insert {
      key      = "apikey123",
      consumer = { id = consumer1.id },
    }

    local consumer2 = bp.consumers:insert {custom_id = "provider_124"}
    bp.keyauth_credentials:insert {
      key      = "apikey124",
      consumer = { id = consumer2.id },
    }

    local route1 = bp.routes:insert {
      hosts      = { "test1.com" },
      protocols  = { "http", "https" },
    }

    bp.response_ratelimiting_plugins:insert({
      route = { id = route1.id },
      config   = {
        fault_tolerant = false,
        policy         = policy,
        redis_host     = REDIS_HOST,
        redis_port     = REDIS_PORT,
        redis_password = REDIS_PASSWORD,
        redis_database = REDIS_DATABASE,
        limits         = { video = { second = ITERATIONS } },
      },
    })

    local route2 = bp.routes:insert {
      hosts      = { "test2.com" },
      protocols  = { "http", "https" },
    }

    bp.response_ratelimiting_plugins:insert({
      route = { id = route2.id },
      config   = {
        fault_tolerant = false,
        policy         = policy,
        redis_host     = REDIS_HOST,
        redis_port     = REDIS_PORT,
        redis_password = REDIS_PASSWORD,
        redis_database = REDIS_DATABASE,
        limits         = { video = { second = ITERATIONS*2, minute = ITERATIONS*4 },
                           image = { second = ITERATIONS } },
      },
    })

    local route3 = bp.routes:insert {
      hosts      = { "test3.com" },
      protocols  = { "http", "https" },
    }

    bp.plugins:insert {
      name     = "key-auth",
      route = { id = route3.id },
    }

    bp.response_ratelimiting_plugins:insert({
      route = { id = route3.id },
      config   = {
        policy = policy,
        redis_host     = REDIS_HOST,
        redis_port     = REDIS_PORT,
        redis_password = REDIS_PASSWORD,
        redis_database = REDIS_DATABASE,
        limits = { video = { second = ITERATIONS - 3 }
      } },
    })

    bp.response_ratelimiting_plugins:insert({
      route = { id = route3.id },
      consumer = { id = consumer1.id },
      config      = {
        fault_tolerant = false,
        policy         = policy,
        redis_host     = REDIS_HOST,
        redis_port     = REDIS_PORT,
        redis_password = REDIS_PASSWORD,
        redis_database = REDIS_DATABASE,
        limits         = { video = { second = ITERATIONS - 2 } },
      },
    })

    local route4 = bp.routes:insert {
      hosts      = { "test4.com" },
      protocols  = { "http", "https" },
    }

    bp.response_ratelimiting_plugins:insert({
      route = { id = route4.id },
      config   = {
        fault_tolerant = false,
        policy         = policy,
        redis_host     = REDIS_HOST,
        redis_port     = REDIS_PORT,
        redis_password = REDIS_PASSWORD,
        redis_database = REDIS_DATABASE,
        limits         = {
          video = { second = ITERATIONS * 2 + 2 },
          image = { second = ITERATIONS }
        },
      }
    })

    local route7 = bp.routes:insert {
      hosts      = { "test7.com" },
      protocols  = { "http", "https" },
    }

    bp.response_ratelimiting_plugins:insert({
      route = { id = route7.id },
      config   = {
        fault_tolerant           = false,
        policy                   = policy,
        redis_host               = REDIS_HOST,
        redis_port               = REDIS_PORT,
        redis_password           = REDIS_PASSWORD,
        redis_database           = REDIS_DATABASE,
        block_on_first_violation = true,
        limits                   = {
          video = {
            second = ITERATIONS,
            minute = ITERATIONS * 2,
          },
          image = {
            second = 4,
          },
        },
      }
    })

    local route8 = bp.routes:insert {
      hosts      = { "test8.com" },
      protocols  = { "http", "https" },
    }

    bp.response_ratelimiting_plugins:insert({
      route = { id = route8.id },
      config   = {
        fault_tolerant = false,
        policy         = policy,
        redis_host     = REDIS_HOST,
        redis_port     = REDIS_PORT,
        redis_password = REDIS_PASSWORD,
        redis_database = REDIS_DATABASE,
        limits         = { video = { second = ITERATIONS, minute = ITERATIONS*2 },
                           image = { second = ITERATIONS-1 } },
      }
    })

    local route9 = bp.routes:insert {
      hosts      = { "test9.com" },
      protocols  = { "http", "https" },
    }

    bp.response_ratelimiting_plugins:insert({
      route = { id = route9.id },
      config   = {
        fault_tolerant      = false,
        policy              = policy,
        hide_client_headers = true,
        redis_host          = REDIS_HOST,
        redis_port          = REDIS_PORT,
        redis_password      = REDIS_PASSWORD,
        redis_database      = REDIS_DATABASE,
        limits              = { video = { second = ITERATIONS } },
      }
    })

    local service10 = bp.services:insert()
    bp.routes:insert {
      hosts = { "test-service1.com" },
      service = service10,
    }
    bp.routes:insert {
      hosts = { "test-service2.com" },
      service = service10,
    }

    bp.response_ratelimiting_plugins:insert({
      service = { id = service10.id },
      config = {
        fault_tolerant = false,
        policy         = policy,
        redis_host     = REDIS_HOST,
        redis_port     = REDIS_PORT,
        redis_password = REDIS_PASSWORD,
        redis_database = REDIS_DATABASE,
        limits         = { video = { second = ITERATIONS } },
      }
    })

    assert(helpers.start_kong({
      database   = strategy,
      nginx_conf = "spec/fixtures/custom_nginx.template",
    }))
  end)

  lazy_teardown(function()
    helpers.stop_kong(nil, true)
  end)

  describe("Without authentication (IP address)", function()

    it("returns remaining counter", function()
      wait()
      local n = math.floor(ITERATIONS / 2)
      for _ = 1, n do
        local res = proxy_client():get("/response-headers?x-kong-limit=video=1", {
          headers = { Host = "test1.com" },
        })
        assert.res_status(200, res)
      end

      ngx.sleep(SLEEP_TIME) -- Wait for async timer to increment the limit

      local res = proxy_client():get("/response-headers?x-kong-limit=video=1", {
        headers = { Host = "test1.com" },
      })
      assert.res_status(200, res)
      assert.equal(ITERATIONS, tonumber(res.headers["x-ratelimit-limit-video-second"]))
      assert.equal(ITERATIONS - n - 1, tonumber(res.headers["x-ratelimit-remaining-video-second"]))
    end)

    it("blocks if exceeding limit", function()
      test_limit("/response-headers?x-kong-limit=video=1", "test1.com")
    end)

    it("counts against the same service register from different routes", function()
      wait()
      local n = math.floor(ITERATIONS / 2)
      for i = 1, n do
        local res = proxy_client():get("/response-headers?x-kong-limit=video=1, test=" .. ITERATIONS, {
          headers = { Host = "test-service1.com" },
        })
        assert.res_status(200, res)
      end

      for i = n+1, ITERATIONS do
        local res = proxy_client():get("/response-headers?x-kong-limit=video=1, test=" .. ITERATIONS, {
          headers = { Host = "test-service2.com" },
        })
        assert.res_status(200, res)
      end

      ngx.sleep(SLEEP_TIME) -- Wait for async timer to increment the list

      -- Additonal request, while limit is ITERATIONS/second
      local res = proxy_client():get("/response-headers?x-kong-limit=video=1, test=" .. ITERATIONS, {
        headers = { Host = "test-service1.com" },
      })
      assert.res_status(429, res)
    end)

    it("handles multiple limits", function()
      wait()
      local n = math.floor(ITERATIONS / 2)
      local res
      for i = 1, n do
        if i == n then
          ngx.sleep(SLEEP_TIME) -- Wait for async timer to increment the limit
        end
        res = proxy_client():get("/response-headers?x-kong-limit=video=2, image=1", {
          headers = { Host = "test2.com" },
        })
        assert.res_status(200, res)
      end

      assert.equal(ITERATIONS * 2, tonumber(res.headers["x-ratelimit-limit-video-second"]))
      assert.equal(ITERATIONS * 2 - (n * 2), tonumber(res.headers["x-ratelimit-remaining-video-second"]))
      assert.equal(ITERATIONS * 4, tonumber(res.headers["x-ratelimit-limit-video-minute"]))
      assert.equal(ITERATIONS * 4 - (n * 2), tonumber(res.headers["x-ratelimit-remaining-video-minute"]))
      assert.equal(ITERATIONS, tonumber(res.headers["x-ratelimit-limit-image-second"]))
      assert.equal(ITERATIONS - n, tonumber(res.headers["x-ratelimit-remaining-image-second"]))

      for i = n+1, ITERATIONS do
        res = proxy_client():get("/response-headers?x-kong-limit=video=1, image=1", {
          headers = { Host = "test2.com" },
        })
        assert.res_status(200, res)
      end

      ngx.sleep(SLEEP_TIME) -- Wait for async timer to increment the limit

      local res = proxy_client():get("/response-headers?x-kong-limit=video=1, image=1", {
        headers = { Host = "test2.com" },
      })

      assert.equal(0, tonumber(res.headers["x-ratelimit-remaining-image-second"]))
      assert.equal(ITERATIONS * 4 - (n * 2) - (ITERATIONS - n), tonumber(res.headers["x-ratelimit-remaining-video-minute"]))
      assert.equal(ITERATIONS * 2 - (n * 2) - (ITERATIONS - n), tonumber(res.headers["x-ratelimit-remaining-video-second"]))
      assert.res_status(429, res)
    end)
  end)

  describe("With authentication", function()
    describe("API-specific plugin", function()
      it("blocks if exceeding limit and a per consumer & route setting", function()
        test_limit("/response-headers?apikey=apikey123&x-kong-limit=video=1", "test3.com", ITERATIONS - 2)
      end)

      it("blocks if exceeding limit and a per route setting", function()
        test_limit("/response-headers?apikey=apikey124&x-kong-limit=video=1", "test3.com", ITERATIONS - 3)
      end)
    end)
  end)

  describe("Upstream usage headers", function()
    it("should append the headers with multiple limits", function()
      wait()
      local res = proxy_client():get("/get", {
        headers = { Host = "test8.com" },
      })
      local json = cjson.decode(assert.res_status(200, res))
      assert.equal(ITERATIONS-1, tonumber(json.headers["x-ratelimit-remaining-image"]))
      assert.equal(ITERATIONS, tonumber(json.headers["x-ratelimit-remaining-video"]))

      -- Actually consume the limits
      local res = proxy_client():get("/response-headers?x-kong-limit=video=2, image=1", {
        headers = { Host = "test8.com" },
      })
      assert.res_status(200, res)

      ngx.sleep(SLEEP_TIME) -- Wait for async timer to increment the limit

      local res = proxy_client():get("/get", {
        headers = { Host = "test8.com" },
      })
      local body = cjson.decode(assert.res_status(200, res))
      assert.equal(ITERATIONS-2, tonumber(body.headers["x-ratelimit-remaining-image"]))
      assert.equal(ITERATIONS-2, tonumber(body.headers["x-ratelimit-remaining-video"]))
    end)

    it("combines multiple x-kong-limit headers from upstream", function()
      wait()
      for _ = 1, ITERATIONS do
        local res = proxy_client():get("/response-headers?x-kong-limit=video%3D2&x-kong-limit=image%3D1", {
          headers = { Host = "test4.com" },
        })
        assert.res_status(200, res)
      end

      proxy_client():get("/response-headers?x-kong-limit=video%3D1", {
        headers = { Host = "test4.com" },
      })

      ngx.sleep(SLEEP_TIME) -- Wait for async timer to increment the limit

      local res = proxy_client():get("/response-headers?x-kong-limit=video%3D2&x-kong-limit=image%3D1", {
        headers = { Host = "test4.com" },
      })

      assert.res_status(429, res)
      assert.equal(0, tonumber(res.headers["x-ratelimit-remaining-image-second"]))
      assert.equal(1, tonumber(res.headers["x-ratelimit-remaining-video-second"]))
    end)
  end)

  it("should block on first violation", function()
    wait()
    local res = proxy_client():get("/response-headers?x-kong-limit=video=2, image=4", {
      headers = { Host = "test7.com" },
    })
    assert.res_status(200, res)

    ngx.sleep(SLEEP_TIME) -- Wait for async timer to increment the limit

    local res = proxy_client():get("/response-headers?x-kong-limit=video=2", {
      headers = { Host = "test7.com" },
    })
    local body = assert.res_status(429, res)
    local json = cjson.decode(body)
    assert.same({ message = "API rate limit exceeded for 'image'" }, json)
  end)

  describe("Config with hide_client_headers", function()
    it("does not send rate-limit headers when hide_client_headers==true", function()
      wait()
      local res = proxy_client():get("/status/200", {
        headers = { Host = "test9.com" },
      })

      assert.res_status(200, res)
      assert.is_nil(res.headers["x-ratelimit-remaining-video-second"])
      assert.is_nil(res.headers["x-ratelimit-limit-video-second"])
    end)
  end)
end)

describe(fmt("#flaky Plugin: response-ratelimiting (expirations) with policy: #%s [#%s]", policy, strategy), function()

  lazy_setup(function()
    local bp = init_db(strategy, policy)

    local route = bp.routes:insert {
      hosts      = { "expire1.com" },
      protocols  = { "http", "https" },
    }

    bp.response_ratelimiting_plugins:insert {
      route = { id = route.id },
      config   = {
        policy         = policy,
        redis_host     = REDIS_HOST,
        redis_port     = REDIS_PORT,
        redis_password = REDIS_PASSWORD,
        fault_tolerant = false,
        limits         = { video = { second = ITERATIONS } },
      }
    }

    assert(helpers.start_kong({
      database   = strategy,
      nginx_conf = "spec/fixtures/custom_nginx.template",
    }))
  end)

  lazy_teardown(function()
    helpers.stop_kong(nil, true)
  end)

  it("expires a counter", function()
    wait()
    local res = proxy_client():get("/response-headers?x-kong-limit=video=1", {
      headers = { Host = "expire1.com" },
    })

    ngx.sleep(SLEEP_TIME) -- Wait for async timer to increment the limit

    assert.res_status(200, res)
    assert.equal(ITERATIONS, tonumber(res.headers["x-ratelimit-limit-video-second"]))
    assert.equal(ITERATIONS-1, tonumber(res.headers["x-ratelimit-remaining-video-second"]))

    ngx.sleep(0.01)
    wait() -- Wait for counter to expire

    local res = proxy_client():get("/response-headers?x-kong-limit=video=1", {
      headers = { Host = "expire1.com" },
    })

    ngx.sleep(SLEEP_TIME) -- Wait for async timer to increment the limit

    assert.res_status(200, res)
    assert.equal(ITERATIONS, tonumber(res.headers["x-ratelimit-limit-video-second"]))
    assert.equal(ITERATIONS-1, tonumber(res.headers["x-ratelimit-remaining-video-second"]))
  end)
end)

describe(fmt("#flaky Plugin: response-ratelimiting (access - global for single consumer) with policy: #%s [#%s]", policy, strategy), function()

  lazy_setup(function()
    local bp = init_db(strategy, policy)

    local consumer = bp.consumers:insert {
      custom_id = "provider_126",
    }

    bp.key_auth_plugins:insert()

    bp.keyauth_credentials:insert {
      key      = "apikey126",
      consumer = { id = consumer.id },
    }

    -- just consumer, no no route or service
    bp.response_ratelimiting_plugins:insert({
      consumer = { id = consumer.id },
      config = {
        fault_tolerant = false,
        policy         = policy,
        redis_host     = REDIS_HOST,
        redis_port     = REDIS_PORT,
        redis_password = REDIS_PASSWORD,
        redis_database = REDIS_DATABASE,
        limits         = { video = { second = ITERATIONS } },
      }
    })

    for i = 1, ITERATIONS do
      bp.routes:insert({ hosts = { fmt("test%d.com", i) } })
    end

    assert(helpers.start_kong({
      database   = strategy,
      nginx_conf = "spec/fixtures/custom_nginx.template",
    }))
  end)

  lazy_teardown(function()
    helpers.stop_kong(nil, true)
  end)

  it("blocks when the consumer exceeds their quota, no matter what service/route used", function()
    test_limit("/response-headers?apikey=apikey126&x-kong-limit=video=1", "test%d.com")
  end)
end)

describe(fmt("#flaky Plugin: response-ratelimiting (access - global) with policy: #%s [#%s]", policy, strategy), function()

  lazy_setup(function()
    local bp = init_db(strategy, policy)

    -- global plugin (not attached to route, service or consumer)
    bp.response_ratelimiting_plugins:insert({
      config = {
        fault_tolerant = false,
        policy         = policy,
        redis_host     = REDIS_HOST,
        redis_port     = REDIS_PORT,
        redis_password = REDIS_PASSWORD,
        redis_database = REDIS_DATABASE,
        limits         = { video = { second = ITERATIONS } },
      }
    })

    for i = 1, ITERATIONS do
      bp.routes:insert({ hosts = { fmt("test%d.com", i) } })
    end

    assert(helpers.start_kong({
      database   = strategy,
      nginx_conf = "spec/fixtures/custom_nginx.template",
    }))
  end)

  lazy_teardown(function()
    helpers.stop_kong(nil, true)
  end)

  before_each(function()
    wait()
  end)

  it("blocks if exceeding limit", function()
    wait()
    for i = 1, ITERATIONS do
      local res = proxy_client():get("/response-headers?x-kong-limit=video=1", {
        headers = { Host = fmt("test%d.com", i) },
      })
      assert.res_status(200, res)
    end

    ngx.sleep(SLEEP_TIME) -- Wait for async timer to increment the limit

    -- last query, while limit is ITERATIONS/second
    local res = proxy_client():get("/response-headers?x-kong-limit=video=1", {
      headers = { Host = "test1.com" },
    })
    assert.res_status(429, res)
    assert.equal(0, tonumber(res.headers["x-ratelimit-remaining-video-second"]))
    assert.equal(ITERATIONS, tonumber(res.headers["x-ratelimit-limit-video-second"]))
  end)
end)

describe(fmt("#flaky Plugin: response-ratelimiting (fault tolerance) with policy: #%s [#%s]", policy, strategy), function()
  if policy == "cluster" then
    local bp, db

    pending("fault tolerance tests for cluster policy temporarily disabled", function()

      before_each(function()
        bp, db = init_db(strategy, policy)

        local route1 = bp.routes:insert {
          hosts = { "failtest1.com" },
        }

        bp.response_ratelimiting_plugins:insert {
          route = { id = route1.id },
          config   = {
            fault_tolerant = false,
            policy         = policy,
            redis_host     = REDIS_HOST,
            redis_port     = REDIS_PORT,
            redis_password = REDIS_PASSWORD,
            limits         = { video = { second = ITERATIONS} },
          }
        }

        local route2 = bp.routes:insert {
          hosts = { "failtest2.com" },
        }

        bp.response_ratelimiting_plugins:insert {
          route = { id = route2.id },
          config   = {
            fault_tolerant = true,
            policy         = policy,
            redis_host     = REDIS_HOST,
            redis_port     = REDIS_PORT,
            redis_password = REDIS_PASSWORD,
            limits         = { video = {second = ITERATIONS} }
          }
        }

        assert(helpers.start_kong({
          database   = strategy,
          nginx_conf = "spec/fixtures/custom_nginx.template",
        }))

        wait()
      end)

      after_each(function()
        helpers.stop_kong(nil, true)
      end)

      it("does not work if an error occurs", function()
        local res = proxy_client():get("/response-headers?x-kong-limit=video=1", {
          headers = { Host = "failtest1.com" },
        })
        assert.res_status(200, res)
        assert.equal(ITERATIONS, tonumber(res.headers["x-ratelimit-limit-video-second"]))
        assert.equal(ITERATIONS, tonumber(res.headers["x-ratelimit-remaining-video-second"]))

        -- Simulate an error on the database
        -- (valid SQL and CQL)
        db.connector:query("DROP TABLE response_ratelimiting_metrics;")
        -- FIXME this leaves the database in a bad state after this test,
        -- affecting subsequent tests.

        -- Make another request
        local res = proxy_client():get("/response-headers?x-kong-limit=video=1", {
          headers = { Host = "failtest1.com" },
        })
        local body = assert.res_status(500, res)
        local json = cjson.decode(body)
        assert.same({ message = "An unexpected error occurred" }, json)
      end)

      it("keeps working if an error occurs", function()
        local res = proxy_client():get("/response-headers?x-kong-limit=video=1", {
          headers = { Host = "failtest2.com" },
        })
        assert.res_status(200, res)
        assert.equal(ITERATIONS, tonumber(res.headers["x-ratelimit-limit-video-second"]))
        assert.equal(ITERATIONS, tonumber(res.headers["x-ratelimit-remaining-video-second"]))

        -- Simulate an error on the database
        -- (valid SQL and CQL)
        db.connector:query("DROP TABLE response_ratelimiting_metrics;")
        -- FIXME this leaves the database in a bad state after this test,
        -- affecting subsequent tests.

        -- Make another request
        local res = proxy_client():get("/response-headers?x-kong-limit=video=1", {
          headers = { Host = "failtest2.com" },
        })
        assert.res_status(200, res)
        assert.is_nil(res.headers["x-ratelimit-limit-video-second"])
        assert.is_nil(res.headers["x-ratelimit-remaining-video-second"])
      end)
    end)
  end

  if policy == "redis" then

    before_each(function()
      local bp = init_db(strategy, policy)

      local route1 = bp.routes:insert {
        hosts      = { "failtest3.com" },
        protocols  = { "http", "https" },
      }

      bp.response_ratelimiting_plugins:insert {
        route = { id = route1.id },
        config   = {
          fault_tolerant = false,
          policy         = policy,
          redis_host     = "5.5.5.5",
          limits         = { video = { second = ITERATIONS } },
        }
      }

      local route2 = bp.routes:insert {
        hosts      = { "failtest4.com" },
        protocols  = { "http", "https" },
      }

      bp.response_ratelimiting_plugins:insert {
        route = { id = route2.id },
        config   = {
          fault_tolerant = true,
          policy         = policy,
          redis_host     = "5.5.5.5",
          limits         = { video = { second = ITERATIONS } },
        }
      }

      assert(helpers.start_kong({
        database   = strategy,
        nginx_conf = "spec/fixtures/custom_nginx.template",
      }))

      wait()
    end)

    after_each(function()
      helpers.stop_kong(nil, true)
    end)

    it("does not work if an error occurs", function()
      -- Make another request
      local res = proxy_client():get("/status/200", {
        headers = { Host = "failtest3.com" },
      })
      local body = assert.res_status(500, res)
      local json = cjson.decode(body)
      assert.same({ message = "An unexpected error occurred" }, json)
    end)
    it("keeps working if an error occurs", function()
      -- Make another request
      local res = proxy_client():get("/status/200", {
        headers = { Host = "failtest4.com" },
      })
      assert.res_status(200, res)
      assert.falsy(res.headers["x-ratelimit-limit-video-second"])
      assert.falsy(res.headers["x-ratelimit-remaining-video-second"])
    end)
  end
end)

end
end


text:
"""Tests for slim.inception_v4."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf
from tensorflow.contrib import slim as contrib_slim

from nets import inception

class InceptionTest(tf.test.TestCase):

  def testBuildLogits(self):
    batch_size = 5
    height, width = 299, 299
    num_classes = 1000
    inputs = tf.random.uniform((batch_size, height, width, 3))
    logits, end_points = inception.inception_v4(inputs, num_classes)
    auxlogits = end_points['AuxLogits']
    predictions = end_points['Predictions']
    self.assertTrue(auxlogits.op.name.startswith('InceptionV4/AuxLogits'))
    self.assertListEqual(auxlogits.get_shape().as_list(),
                         [batch_size, num_classes])
    self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
    self.assertListEqual(logits.get_shape().as_list(),
                         [batch_size, num_classes])
    self.assertTrue(predictions.op.name.startswith(
        'InceptionV4/Logits/Predictions'))
    self.assertListEqual(predictions.get_shape().as_list(),
                         [batch_size, num_classes])

  def testBuildPreLogitsNetwork(self):
    batch_size = 5
    height, width = 299, 299
    num_classes = None
    inputs = tf.random.uniform((batch_size, height, width, 3))
    net, end_points = inception.inception_v4(inputs, num_classes)
    self.assertTrue(net.op.name.startswith('InceptionV4/Logits/AvgPool'))
    self.assertListEqual(net.get_shape().as_list(), [batch_size, 1, 1, 1536])
    self.assertFalse('Logits' in end_points)
    self.assertFalse('Predictions' in end_points)

  def testBuildWithoutAuxLogits(self):
    batch_size = 5
    height, width = 299, 299
    num_classes = 1000
    inputs = tf.random.uniform((batch_size, height, width, 3))
    logits, endpoints = inception.inception_v4(inputs, num_classes,
                                               create_aux_logits=False)
    self.assertFalse('AuxLogits' in endpoints)
    self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
    self.assertListEqual(logits.get_shape().as_list(),
                         [batch_size, num_classes])

  def testAllEndPointsShapes(self):
    batch_size = 5
    height, width = 299, 299
    num_classes = 1000
    inputs = tf.random.uniform((batch_size, height, width, 3))
    _, end_points = inception.inception_v4(inputs, num_classes)
    endpoints_shapes = {'Conv2d_1a_3x3': [batch_size, 149, 149, 32],
                        'Conv2d_2a_3x3': [batch_size, 147, 147, 32],
                        'Conv2d_2b_3x3': [batch_size, 147, 147, 64],
                        'Mixed_3a': [batch_size, 73, 73, 160],
                        'Mixed_4a': [batch_size, 71, 71, 192],
                        'Mixed_5a': [batch_size, 35, 35, 384],
                        # 4 x Inception-A blocks
                        'Mixed_5b': [batch_size, 35, 35, 384],
                        'Mixed_5c': [batch_size, 35, 35, 384],
                        'Mixed_5d': [batch_size, 35, 35, 384],
                        'Mixed_5e': [batch_size, 35, 35, 384],
                        # Reduction-A block
                        'Mixed_6a': [batch_size, 17, 17, 1024],
                        # 7 x Inception-B blocks
                        'Mixed_6b': [batch_size, 17, 17, 1024],
                        'Mixed_6c': [batch_size, 17, 17, 1024],
                        'Mixed_6d': [batch_size, 17, 17, 1024],
                        'Mixed_6e': [batch_size, 17, 17, 1024],
                        'Mixed_6f': [batch_size, 17, 17, 1024],
                        'Mixed_6g': [batch_size, 17, 17, 1024],
                        'Mixed_6h': [batch_size, 17, 17, 1024],
                        # Reduction-A block
                        'Mixed_7a': [batch_size, 8, 8, 1536],
                        # 3 x Inception-C blocks
                        'Mixed_7b': [batch_size, 8, 8, 1536],
                        'Mixed_7c': [batch_size, 8, 8, 1536],
                        'Mixed_7d': [batch_size, 8, 8, 1536],
                        # Logits and predictions
                        'AuxLogits': [batch_size, num_classes],
                        'global_pool': [batch_size, 1, 1, 1536],
                        'PreLogitsFlatten': [batch_size, 1536],
                        'Logits': [batch_size, num_classes],
                        'Predictions': [batch_size, num_classes]}
    self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
    for endpoint_name in endpoints_shapes:
      expected_shape = endpoints_shapes[endpoint_name]
      self.assertTrue(endpoint_name in end_points)
      self.assertListEqual(end_points[endpoint_name].get_shape().as_list(),
                           expected_shape)

  def testBuildBaseNetwork(self):
    batch_size = 5
    height, width = 299, 299
    inputs = tf.random.uniform((batch_size, height, width, 3))
    net, end_points = inception.inception_v4_base(inputs)
    self.assertTrue(net.op.name.startswith(
        'InceptionV4/Mixed_7d'))
    self.assertListEqual(net.get_shape().as_list(), [batch_size, 8, 8, 1536])
    expected_endpoints = [
        'Conv2d_1a_3x3', 'Conv2d_2a_3x3', 'Conv2d_2b_3x3', 'Mixed_3a',
        'Mixed_4a', 'Mixed_5a', 'Mixed_5b', 'Mixed_5c', 'Mixed_5d',
        'Mixed_5e', 'Mixed_6a', 'Mixed_6b', 'Mixed_6c', 'Mixed_6d',
        'Mixed_6e', 'Mixed_6f', 'Mixed_6g', 'Mixed_6h', 'Mixed_7a',
        'Mixed_7b', 'Mixed_7c', 'Mixed_7d']
    self.assertItemsEqual(end_points.keys(), expected_endpoints)
    for name, op in end_points.items():
      self.assertTrue(op.name.startswith('InceptionV4/' + name))

  def testBuildOnlyUpToFinalEndpoint(self):
    batch_size = 5
    height, width = 299, 299
    all_endpoints = [
        'Conv2d_1a_3x3', 'Conv2d_2a_3x3', 'Conv2d_2b_3x3', 'Mixed_3a',
        'Mixed_4a', 'Mixed_5a', 'Mixed_5b', 'Mixed_5c', 'Mixed_5d',
        'Mixed_5e', 'Mixed_6a', 'Mixed_6b', 'Mixed_6c', 'Mixed_6d',
        'Mixed_6e', 'Mixed_6f', 'Mixed_6g', 'Mixed_6h', 'Mixed_7a',
        'Mixed_7b', 'Mixed_7c', 'Mixed_7d']
    for index, endpoint in enumerate(all_endpoints):
      with tf.Graph().as_default():
        inputs = tf.random.uniform((batch_size, height, width, 3))
        out_tensor, end_points = inception.inception_v4_base(
            inputs, final_endpoint=endpoint)
        self.assertTrue(out_tensor.op.name.startswith(
            'InceptionV4/' + endpoint))
        self.assertItemsEqual(all_endpoints[:index+1], end_points.keys())

  def testVariablesSetDevice(self):
    batch_size = 5
    height, width = 299, 299
    num_classes = 1000
    inputs = tf.random.uniform((batch_size, height, width, 3))
    # Force all Variables to reside on the device.
    with tf.compat.v1.variable_scope('on_cpu'), tf.device('/cpu:0'):
      inception.inception_v4(inputs, num_classes)
    with tf.compat.v1.variable_scope('on_gpu'), tf.device('/gpu:0'):
      inception.inception_v4(inputs, num_classes)
    for v in tf.compat.v1.get_collection(
        tf.compat.v1.GraphKeys.GLOBAL_VARIABLES, scope='on_cpu'):
      self.assertDeviceEqual(v.device, '/cpu:0')
    for v in tf.compat.v1.get_collection(
        tf.compat.v1.GraphKeys.GLOBAL_VARIABLES, scope='on_gpu'):
      self.assertDeviceEqual(v.device, '/gpu:0')

  def testHalfSizeImages(self):
    batch_size = 5
    height, width = 150, 150
    num_classes = 1000
    inputs = tf.random.uniform((batch_size, height, width, 3))
    logits, end_points = inception.inception_v4(inputs, num_classes)
    self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
    self.assertListEqual(logits.get_shape().as_list(),
                         [batch_size, num_classes])
    pre_pool = end_points['Mixed_7d']
    self.assertListEqual(pre_pool.get_shape().as_list(),
                         [batch_size, 3, 3, 1536])

  def testGlobalPool(self):
    batch_size = 1
    height, width = 350, 400
    num_classes = 1000
    inputs = tf.random.uniform((batch_size, height, width, 3))
    logits, end_points = inception.inception_v4(inputs, num_classes)
    self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
    self.assertListEqual(logits.get_shape().as_list(),
                         [batch_size, num_classes])
    pre_pool = end_points['Mixed_7d']
    self.assertListEqual(pre_pool.get_shape().as_list(),
                         [batch_size, 9, 11, 1536])

  def testGlobalPoolUnknownImageShape(self):
    batch_size = 1
    height, width = 350, 400
    num_classes = 1000
    with self.test_session() as sess:
      inputs = tf.compat.v1.placeholder(tf.float32, (batch_size, None, None, 3))
      logits, end_points = inception.inception_v4(
          inputs, num_classes, create_aux_logits=False)
      self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
      self.assertListEqual(logits.get_shape().as_list(),
                           [batch_size, num_classes])
      pre_pool = end_points['Mixed_7d']
      images = tf.random.uniform((batch_size, height, width, 3))
      sess.run(tf.compat.v1.global_variables_initializer())
      logits_out, pre_pool_out = sess.run([logits, pre_pool],
                                          {inputs: images.eval()})
      self.assertTupleEqual(logits_out.shape, (batch_size, num_classes))
      self.assertTupleEqual(pre_pool_out.shape, (batch_size, 9, 11, 1536))

  def testUnknownBatchSize(self):
    batch_size = 1
    height, width = 299, 299
    num_classes = 1000
    with self.test_session() as sess:
      inputs = tf.compat.v1.placeholder(tf.float32, (None, height, width, 3))
      logits, _ = inception.inception_v4(inputs, num_classes)
      self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
      self.assertListEqual(logits.get_shape().as_list(),
                           [None, num_classes])
      images = tf.random.uniform((batch_size, height, width, 3))
      sess.run(tf.compat.v1.global_variables_initializer())
      output = sess.run(logits, {inputs: images.eval()})
      self.assertEquals(output.shape, (batch_size, num_classes))

  def testEvaluation(self):
    batch_size = 2
    height, width = 299, 299
    num_classes = 1000
    with self.test_session() as sess:
      eval_inputs = tf.random.uniform((batch_size, height, width, 3))
      logits, _ = inception.inception_v4(eval_inputs,
                                         num_classes,
                                         is_training=False)
      predictions = tf.argmax(input=logits, axis=1)
      sess.run(tf.compat.v1.global_variables_initializer())
      output = sess.run(predictions)
      self.assertEquals(output.shape, (batch_size,))

  def testTrainEvalWithReuse(self):
    train_batch_size = 5
    eval_batch_size = 2
    height, width = 150, 150
    num_classes = 1000
    with self.test_session() as sess:
      train_inputs = tf.random.uniform((train_batch_size, height, width, 3))
      inception.inception_v4(train_inputs, num_classes)
      eval_inputs = tf.random.uniform((eval_batch_size, height, width, 3))
      logits, _ = inception.inception_v4(eval_inputs,
                                         num_classes,
                                         is_training=False,
                                         reuse=True)
      predictions = tf.argmax(input=logits, axis=1)
      sess.run(tf.compat.v1.global_variables_initializer())
      output = sess.run(predictions)
      self.assertEquals(output.shape, (eval_batch_size,))

  def testNoBatchNormScaleByDefault(self):
    height, width = 299, 299
    num_classes = 1000
    inputs = tf.compat.v1.placeholder(tf.float32, (1, height, width, 3))
    with contrib_slim.arg_scope(inception.inception_v4_arg_scope()):
      inception.inception_v4(inputs, num_classes, is_training=False)

    self.assertEqual(tf.compat.v1.global_variables('.*/BatchNorm/gamma:0$'), [])

  def testBatchNormScale(self):
    height, width = 299, 299
    num_classes = 1000
    inputs = tf.compat.v1.placeholder(tf.float32, (1, height, width, 3))
    with contrib_slim.arg_scope(
        inception.inception_v4_arg_scope(batch_norm_scale=True)):
      inception.inception_v4(inputs, num_classes, is_training=False)

    gamma_names = set(
        v.op.name
        for v in tf.compat.v1.global_variables('.*/BatchNorm/gamma:0$'))
    self.assertGreater(len(gamma_names), 0)
    for v in tf.compat.v1.global_variables('.*/BatchNorm/moving_mean:0$'):
      self.assertIn(v.op.name[:-len('moving_mean')] + 'gamma', gamma_names)

if __name__ == '__main__':
  tf.test.main()


text:
#include "JniUtil.h"

#ifndef JNI_QUERY_H
#define JNI_QUERY_H

/**
* @author Grégory Van den Borre
*/
#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT POINTER JNICALL Java_jni_JniQuery_throwRay(
    JNIEnv* env,
    jobject o,
    POINTER pointer,
    jfloat x,
    jfloat y,
    jboolean poly);

JNIEXPORT jlongArray JNICALL Java_jni_JniQuery_throwPlaneRay(
    JNIEnv* env,
    jobject o,
    POINTER pointer,
    jfloat left,
    jfloat top,
    jfloat right,
    jfloat bottom);

#ifdef __cplusplus
}
#endif
#endif


text:
class AddIndexesToNotifications < ActiveRecord::Migration
  def change
    add_index :notifications, [:user_id, :created_at]
    add_index :notifications, [:user_id, :section, :created_at]
  end
end


text:
module Fog
  module Compute
    class Linode
      class Real
        def linode_disk_createfromimage(linode_id, image_id, label, size, password, sshkey)
          request(
            :expects  => 200,
            :method   => 'GET',
            :query    => {
              :api_action => 'linode.disk.createfromimage',
              :linodeId => linode_id,
              :imageId => image_id,
              :label => label,
              :size => size,
              :rootPass => password,
              :rootSSHKey => sshkey
            }
          )
        end
      end

      class Mock
        def linode_disk_createfromimage(linode_id, image_id, label, size, password, sshkey)
          response = Excon::Response.new
          response.status = 200
          response.body = {
            "ERRORARRAY" => [],
            "ACTION"     => "linode.disk.createfromimage",
            "DATA"       => { "JobID" => Fog::Mock.random_numbers(4),
                              "DiskID" => Fog::Mock.random_numbers(5) }
          }
          response
        end
      end
    end
  end
end


text:
package Problems;

import java.io.*;
import java.util.StringTokenizer;

/**
 * 문제 번호 : 1912
 * 문제 이름 : 연속합
 * 문제 주소 : https://www.acmicpc.net/problem/1912
 */

class P_1912 {

    public void printArr(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    int[] data;

    public void inputData() throws IOException {
        FileInputStream fis = new FileInputStream("test.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(fis));

        // BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int N = Integer.parseInt(br.readLine());
        data = new int[N];
        StringTokenizer st = new StringTokenizer(br.readLine());
        int index = 0;
        while (st.hasMoreTokens()) {
            data[index++] = Integer.parseInt(st.nextToken());
        }
        //printArr(data);

    }

    public int Kadane(int[] arr) {
        int max_ending_here = 0;
        int max_so_far = Integer.MIN_VALUE;

        for (int i = 0; i < arr.length; i++) {
            max_ending_here += arr[i];
            if (max_ending_here > max_so_far) {
                max_so_far = max_ending_here;
            }
            if (max_ending_here < 0) {
                max_ending_here = 0;
            }
        }

        return max_so_far;
    }

    public void Solve() throws IOException {
        inputData();
        int result = Kadane(data);
        System.out.println(result);
    }

    public static void main(String[] args) throws IOException {
        new P_1912().Solve();
    }

}


text:
package com.thoughtworks.go.helper;

import com.thoughtworks.go.domain.AccessToken;
import com.thoughtworks.go.util.TestingClock;
import org.apache.commons.lang3.RandomStringUtils;

public class AccessTokenMother {
    public static AccessToken.AccessTokenWithDisplayValue randomAccessToken() {
        return randomAccessTokenForUser(RandomStringUtils.randomAlphabetic(32));
    }

    public static AccessToken.AccessTokenWithDisplayValue randomAccessTokenForUser(String username) {
        return AccessToken.create(RandomStringUtils.randomAlphabetic(32), username, RandomStringUtils.randomAlphabetic(32), new TestingClock());
    }
}


text:
<!DOCTYPE html>

  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width initial-scale=1">

  <title>ABOUT</title>
  <meta name="description" content="">

  <link rel="stylesheet" href="/css/main.css">

  <link rel="canonical" href="http://drew.lanen.ga/about/">
  <link rel="alternate" type="application/atom+xml" title="drew.lanen.ga" href="http://drew.lanen.ga/feed.xml" />
  <link rel="apple-touch-icon" href="http://drew.lanen.ga/apple-touch-icon.png" />
  <link rel="apple-touch-icon" href="/assets/images/apple-touch-icon.png" />
  <link rel="icon" type="image/x-icon" href="/assets/images/d.ico" />

  <meta property="og:title" content="ABOUT">

  <meta property="og:type" content="article">
  <meta property="og:url" content="http://drew.lanen.ga/about/">
  <meta property="og:site_name" content="drew.lanen.ga">

        ABOUT

    drew.lanen.ga

Drew Lanenga

Data Scientist

PROFESSIONAL

Data Scientist @ Lytics

December 2013 – Present

Chief Scientist @ HighFive

June 2013 – December 2013

Hacker In Residence @ Techstars/Nike+

March 2013 – June 2013

Data Scientist @ Qualtrics

July 2009 – September 2012

Pollster @ UCEP

August 2008 – April 2009

PERSONAL

My wife is a photographer.  We have two daughters, both of whom are named after movie characters.  We live in Portland, OR.

You won't find any comment forms here.  If you want to talk about something here, you can find me at @drewlanenga or drew@lanen.ga.

drew lanenga © 2017