text:
package edu.ucsc.cgl.conductor

import java.io.ByteArrayInputStream
import java.net.URI

import com.amazonaws.AmazonClientException
import com.amazonaws.auth._
import com.amazonaws.auth.profile.ProfileCredentialsProvider
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.model._
import com.typesafe.scalalogging.slf4j.LazyLogging
import org.apache.commons.io.IOUtils
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs._
import org.apache.hadoop.hdfs.DistributedFileSystem
import org.apache.hadoop.mapred
import org.apache.hadoop.mapred._
import org.apache.hadoop.util.Progressable
import org.apache.spark.SparkContext._
import org.apache.spark.{SparkConf, SparkContext}

import scala.collection.JavaConverters._
import scala.collection.mutable.ArrayBuffer

case class Config( s3PartSize: Int = 64 * 1024 * 1024,
                   hdfsBlockSize: Int = 64 * 1024 * 1024,
                   concat: Boolean = false,
                   awsCredentials: Option[ExplicitCredentials] = None,
                   src: URI,
                   dst: URI )

object Conductor
{
    def main( args: Array[String] )
    {
        val parser = new scopt.OptionParser[Config]( "scopt" )
        {
            arg[String]( "SRC_URL" ) required() action { ( x, c ) =>
                c.copy( src = new URI( x ) )
            } text "The URL to transfer from. Either s3://BUCKET/KEY or hdfs://HOST[:PORT]/PATH."

            arg[String]( "DST_URL" ) required() action { ( x, c ) =>
                c.copy( dst = new URI( x ) )
            } text "The URL to transfer to. Either hdfs://HOST[:PORT]/PATH or s3://BUCKET/KEY."

            // FIXME: Add examples to the help text.

            opt[Unit]( 'C', "concat" ) action { ( _, c ) =>
                c.copy( concat = true )
            } text "Concatenate all parts after the transfer."

            opt[Int]( 'p', "s3-part-size" ) action { ( x, c ) =>
                c.copy( s3PartSize = x * 1024 * 1024 )
            } text "The size of each S3 part in MB; default: 64"

            opt[Int]( 'b', "hdfs-block-size" ) action { ( x, c ) =>
                c.copy( hdfsBlockSize = x * 1024 * 1024 )
            } text "The block size in MB for files created on HDFS. The default is 64. Must " +
                "divide the S3 part size evenly. This option is only used when downloading to HDFS."

            opt[String]( 'c', "aws-credentials" ) action { ( x, c ) =>
                val a = x.split( ':' )
                c.copy( awsCredentials = Some( ExplicitCredentials( a( 0 ), a( 1 ) ) ) )
            } text "The access and secret key to use for AWS, separated by a colon. By default, " +
                "credentials are looked up in environment variables (AWS_ACCESS_KEY_ID or " +
                "AWS_ACCESS_KEY for the access key and AWS_SECRET_ACCESS_KEY or AWS_SECRET_KEY " +
                "for the secret key) or, if that fails, in system properties (aws.accessKeyId " +
                "and aws.secretKey) or, if that fails, read from ~/.aws/credentials. See " +
                "http://docs.aws.amazon.com/AWSSdkDocsJava/latest/DeveloperGuide/credentials.html" +
                "for details. Using this flag may lead to exposure of the secret key. Prefer to " +
                "use the other configuration mechanisms outlined above."
        }

        val dummyUrl: URI = new URI( "" )
        java.lang.System.exit(
            parser.parse( args, Config( src = dummyUrl, dst = dummyUrl ) ) match {
                case Some( config ) =>
                    val credentials = Credentials( config.awsCredentials )
                    assert( config.s3PartSize % config.hdfsBlockSize == 0,
                        "S3 part size must be a multiple of HDFS block size." )
                    val (src, dst) = (config.src, config.dst)
                    val schemes = ("s3", "hdfs")
                    assert( List( schemes, schemes.swap ).contains( (src.getScheme, dst.getScheme) ),
                        "Unsupported combination of source and destination URL schemes. " +
                            "Expecting s3:// to hdfs:// or vice versa." )
                    val transfer = if( src.getScheme == "s3" ) {
                        new Download( config, credentials )
                    } else {
                        new Upload( config, credentials )
                    }
                    transfer.run( )
                    0
                case None =>
                    1
            } )
    }

    val blockSizeConfName = "edu.ucsc.cgl.conductor.Conductor.blockSize"
}

abstract class Transfer extends Serializable
{
    def sparkContext: SparkContext =
    {
        new SparkContext( new SparkConf( ).setAppName( this.getClass.getName ) )
    }

    def run( )
}

class Download( config: Config, credentials: Credentials ) extends Transfer
{
    assert( config.src.getScheme == "s3" )
    assert( config.dst.getScheme == "hdfs" )
    val srcBucket = config.src.getHost
    val srcPath = config.src.getPath
    assert( srcPath( 0 ) == '/', "The path to the source file must be valid and start with '/'." )
    val srcKey = srcPath.substring( 1 )
    val splitDst = new URI( config.dst.toString + ".parts" )

    def run( )
    {
        val s3 = new AmazonS3Client( credentials.toAwsCredentials )
        val objects =
            s3.listObjects( srcBucket, srcKey + "/" ).getObjectSummaries
                .asScala
                .filter( !_.getKey.endsWith( "_SUCCESS" ) )
                .sortBy( _.getKey )
        val keys = objects.map( _.getKey )
        val isFile = objects.isEmpty
        val sc = sparkContext
        try {
            val destination = if( config.concat ) splitDst else config.dst
            if( isFile ) {
                val size = s3.getObjectMetadata( srcBucket, srcKey ).getContentLength
                val partitions = partition( size )
                // Inject block size such that BinaryOutputFormat can extract it
                sc.hadoopConfiguration.setInt( Conductor.blockSizeConfName, config.hdfsBlockSize )
                sc.parallelize( partitions, partitions.size )
                    .map( partition => (partition, downloadPart( partition )) )
                    .saveAsHadoopFile(
                        destination.toString,
                        classOf[Object],
                        classOf[Array[Byte]],
                        classOf[BinaryOutputFormat[Object]] )
            } else {
                if( config.concat ) {
                    val srcPartSize = objects.head.getSize
                    if( objects.init.exists( p =>
                        p.getSize != srcPartSize || p.getSize % config.hdfsBlockSize != 0 )
                    ) {
                        throw new AssertionError(
                            "For parts to be concatenated, the size of all but the last part " +
                                "must be equal and divisible by the HDFS block size." )
                    }
                }
                sc.parallelize( keys )
                    .zipWithIndex( )
                    .foreach { case (key, index) => downloadFile( key, index, destination ) }
            }
            if( config.concat ) {
                concat( sc )
            } else {
                createSuccessFile( sc )
            }
        } finally {
            sc.stop( )
        }
    }

    def partition( size: Long ): ArrayBuffer[Partition] =
    {
        val numPartitions = size / config.s3PartSize
        val remainder = (size % config.s3PartSize).toInt
        val partitions = new ArrayBuffer[Partition]
        for( i <- 0L until numPartitions ) {
            partitions += new Partition( i * config.s3PartSize, config.s3PartSize )
        }
        if( remainder > 0 ) {
            partitions += new Partition( size - remainder, remainder )
        }
        partitions
    }

    def downloadPart( partition: Partition ): Array[Byte] =
    {
        val size = partition.getSize
        val start = partition.getStart
        assert( size > 0, "Partitions cannot be empty." )
        assert( size <= config.s3PartSize, "No partition can exceed the specified part size." )
        val s3 = new AmazonS3Client( credentials.toAwsCredentials )
        val req = new GetObjectRequest( srcBucket, srcKey )
        req.setRange( start, start + size - 1 )
        val in = s3.getObject( req ).getObjectContent
        try {
            /*
             * Knowing the input size is obviously an advantage over blindly copying from a stream
             * of unknown size. IOW, I expect the result array to be allocated exactly once.
             * Currently I can only find this in commons.io. Guava has it, too, but doesn't expose
             * it publicly. Also, anything based on ByteArrayOutputStream will likely incur one
             * final redundant copy when the ByteArrayOutputStream.toByteArray method is called.
             * scalax.io calls Thread.sleep() in its copy methods which leads to immediate
             * disqualification. But it doesn't allow specifying a known size either AFAICT.
             */
            IOUtils.toByteArray( in, size )
        } finally {
            in.close( )
        }
    }

    def downloadFile( key: String, index: Long, destination: URI ): Unit =
    {
        val slaveS3 = new AmazonS3Client( credentials.toAwsCredentials )
        val file = slaveS3.getObject( srcBucket, key )
        var partName = key.split( "/" )( 1 )
        if( config.concat ) {
            val partDigit = 5
            val formattedNumber =
                index.toString.reverse.padTo( partDigit, '0' ).reverse
            partName = "part-" + formattedNumber
        }
        val content = IOUtils.toByteArray( file.getObjectContent )
        val hadoopConfig = new Configuration( )
        val hdfsRootURI = new URI( config.dst.getScheme, config.dst.getHost, "/", "" )
        val hdfs = FileSystem.get( hdfsRootURI, hadoopConfig )
        try {
            val filePath = new Path( destination.toString + "/" + partName )
            val stream = hdfs.create(
                filePath,
                true,
                hdfs.getConf.getInt( "io.file.buffer.size", 4096 ),
                hdfs.getDefaultReplication( filePath ),
                config.hdfsBlockSize
            )
            try {
                stream.write( content )
            } finally {
                stream.close( )
            }
        } finally {
            hdfs.close( )
        }
    }

    def concat( sc: SparkContext ): Unit =
    {
        val dfs = FileSystem.get( config.dst, sc.hadoopConfiguration )
            .asInstanceOf[DistributedFileSystem]
        val splitDstPath = new Path( splitDst )
        val dstPath = new Path( config.dst )
        val parts = dfs.listStatus( splitDstPath, new PathFilter
        {
            override def accept( path: Path ): Boolean = path.getName.startsWith( "part-" )
        } ).map( _.getPath ).sortBy( _.getName )
        val Array( firstPart, otherParts@_* ) = parts
        if( otherParts.nonEmpty ) {
            dfs.concat( firstPart, otherParts.toArray )
        }
        dfs.rename( firstPart, dstPath )
        dfs.delete( splitDstPath, true )
    }

    def createSuccessFile( sc: SparkContext ): Unit =
    {
        val dfs = FileSystem.get( config.dst, sc.hadoopConfiguration )
            .asInstanceOf[DistributedFileSystem]
        try {
            dfs.createNewFile( new Path( config.dst.toString + "/_SUCCESS" ) )
        } finally {
            dfs.close( )
        }
    }
}

class Upload( config: Config, credentials: Credentials ) extends Transfer
{
    assert( config.src.getScheme == "hdfs" )
    assert( config.dst.getScheme == "s3" )
    val dstBucket = config.dst.getHost
    val dstPath = config.dst.getPath
    assert( dstPath( 0 ) == '/', "The path to the destination file must start with '/'." )
    val dstKey = dstPath.substring( 1 )
    val maxPartNumber = 10000

    def run( )
    {
        if( config.concat ) {
            uploadParts( )
        } else {
            uploadObjects( )
        }
    }

    def uploadParts( )
    {
        val sc = sparkContext
        try {
            val indexedRDD =
                sc.hadoopFile[Partition, Array[Byte], BinaryInputFormat]( config.src.toString )
                    // FIXME: why is this filter necessary?
                    .filter { case (partition, data) => !data.isEmpty }
                    .zipWithIndex( )
            val s3 = new AmazonS3Client( credentials.toAwsCredentials )
            val request = new InitiateMultipartUploadRequest( dstBucket, dstKey )
            val uploadId = s3.initiateMultipartUpload( request ).getUploadId
            try {
                // PartETag objects aren't serializable so we need to substitute them with tuples
                val partsAsTuples = indexedRDD.map { case ((partition, block), partNum) =>
                    (uploadPart( uploadId, partition, block, partNum ).getETag, partNum)
                }.collect( )
                // Now recreate the parts as PartETag objects
                val parts = partsAsTuples
                    .map { case (etag, partNum) => new PartETag( partNum.toInt + 1, etag ) }
                    .toBuffer
                    .asJava
                // And finalize the upload
                s3.completeMultipartUpload( new CompleteMultipartUploadRequest( )
                    .withBucketName( dstBucket )
                    .withKey( dstKey )
                    .withUploadId( uploadId )
                    .withPartETags( parts ) )
            } catch {
                case e: Exception =>
                    val request = new AbortMultipartUploadRequest( dstBucket, dstKey, uploadId )
                    s3.abortMultipartUpload( request )
                    throw e
            }
        } finally {
            sc.stop( )
        }
    }

    def uploadPart( uploadId: String,
                    partition: Partition,
                    block: Array[Byte],
                    partNum: Long ): PartETag =
    {
        val s3 = new AmazonS3Client( credentials.toAwsCredentials )
        val stream = new ByteArrayInputStream( block )
        val response = s3.uploadPart( new UploadPartRequest( )
            .withBucketName( dstBucket )
            .withKey( dstKey )
            .withUploadId( uploadId )
            .withPartNumber( partNum.toInt + 1 )
            .withInputStream( stream )
            .withPartSize( block.length ) )
        response.getPartETag
    }

    def uploadObjects( )
    {
        val sc = sparkContext
        try {
            val indexedRDD =
                sc.hadoopFile[Partition, Array[Byte], BinaryInputFormat]( config.src.toString )
                    // FIXME: why is this filter necessary?
                    .filter { case (partition, data) => !data.isEmpty }
                    .zipWithIndex( )
            val s3 = new AmazonS3Client( credentials.toAwsCredentials )
            indexedRDD.foreach { case ((partition, block), part) =>
                uploadObject( partition, block, part.toInt + 1 )
            }
            val metadata = new ObjectMetadata( )
            metadata.setContentLength( 0 )
            val emptyStream = new ByteArrayInputStream( new Array[Byte]( 0 ) )
            s3.putObject( dstBucket, dstKey + "/_SUCCESS", emptyStream, metadata )
        } finally {
            sc.stop( )
        }
    }

    def uploadObject( partition: Partition, block: Array[Byte], partNum: Int ) =
    {
        val s3 = new AmazonS3Client( credentials.toAwsCredentials )
        val partDigit = 5
        val metadata = new ObjectMetadata( )
        metadata.setContentLength( block.length )
        val stream = new ByteArrayInputStream( block )
        val formattedNumber: String = partNum.toString.reverse.padTo( partDigit, '0' ).reverse
        s3.putObject( dstBucket, dstKey + "/part-" + formattedNumber, stream, metadata )
    }
}

class BinaryOutputFormat[K] extends FileOutputFormat[K, Array[Byte]]
{

    override def getRecordWriter( ignored: FileSystem,
                                  job: JobConf,
                                  name: String,
                                  progress: Progressable ): mapred.RecordWriter[K, Array[Byte]] =
    {
        val file: Path = FileOutputFormat.getTaskOutputPath( job, name )
        val fs: FileSystem = file.getFileSystem( job )
        /*
         * Concatenation only works if all but the last part use an integral
         * number of blocks. We ensure this by setting the block size for this
         * "file" (quotes because it really is a directory) to the partition
         * size. That way, each part except the last one will take up one block.
         */
        // FIXME: This call duplicates too much internal logic from FileSystem
        val fileOut: FSDataOutputStream = fs.create(
            file,
            true,
            fs.getConf.getInt( "io.file.buffer.size", 4096 ),
            fs.getDefaultReplication( file ),
            job.getInt( Conductor.blockSizeConfName, 0 ),
            progress )
        new mapred.RecordWriter[K, Array[Byte]]
        {
            override def write( key: K, value: Array[Byte] ): Unit = { fileOut.write( value ) }

            override def close( reporter: Reporter ): Unit = { fileOut.close( ) }
        }
    }
}

class BinaryInputFormat extends FileInputFormat[Partition, Array[Byte]]
{
    override def getRecordReader( inputSplit: InputSplit,
                                  jobConf: JobConf,
                                  reporter: Reporter ): RecordReader[Partition, Array[Byte]] =
    {
        val fileSplit = inputSplit.asInstanceOf[FileSplit]
        val path: Path = fileSplit.getPath
        val fs: FileSystem = path.getFileSystem( jobConf )
        val fileSize = fs.getFileStatus( path ).getLen
        val fileIn: FSDataInputStream = fs.open( path )
        fileIn.seek( fileSplit.getStart )

        new RecordReader[Partition, Array[Byte]]
        {
            var x = true

            override def next( k: Partition, v: Array[Byte] ): Boolean =
            {
                if( x ) {
                    IOUtils.read( fileIn, v, 0, fileSplit.getLength.toInt )
                    x = false
                    true
                } else {
                    x
                }
            }

            override def getProgress: Float = { fileIn.getPos.toFloat / fileSize.toFloat }

            override def getPos: Long = { fileIn.getPos }

            override def createKey( ): Partition =
            {
                new Partition( fileSplit.getStart, fileSplit.getLength.toInt )
            }

            override def createValue( ): Array[Byte] =
            {
                new Array[Byte]( fileSplit.getLength.toInt )
            }

            override def close( ): Unit = { fileIn.close( ) }
        }
    }
}

class Partition( start: Long, size: Int ) extends Serializable
{
    def getStart: Long = { start }

    def getSize: Int = { size }
}

abstract class Credentials
{
    def toAwsCredentials: AWSCredentials
}

object Credentials extends LazyLogging
{
    def apply( forcedCreds: Option[ExplicitCredentials] = None ) =
    {
        forcedCreds match {
            case Some( creds ) =>
                logger.warn( "Using forced credentials with access key {}.", creds.accessKeyId )
                creds
            case None =>
                // If we can get configured credentials on the driver, use those on every worker
                // node. Otherwise fall back to instance profile credentials which will be read
                // from instance metadata on each node. Note that even if the driver node has access
                // to instance profile credentials, chances are that we wouldn't be able to use them
                // on worker instances as they are specific to each instance.
                try {
                    val creds = new ExplicitCredentials( new ConfiguredCredentials( ).toAwsCredentials )
                    logger.info( "Using configured credentials with access key {}.", creds.accessKeyId )
                    creds
                } catch {
                    case e: AmazonClientException =>
                        val creds = new ImplicitCredentials
                        logger.info( "Using implicit credentials with access key {}. Credentials " +
                            "may differ from node to node.", creds.toAwsCredentials.getAWSAccessKeyId )
                        creds
                }
        }
    }
}

@SerialVersionUID( 0L )
case class ExplicitCredentials( accessKeyId: String, secretKey: String )
    extends Credentials with Serializable
{
    def this( awsCredentials: AWSCredentials )
    {
        this( awsCredentials.getAWSAccessKeyId, awsCredentials.getAWSSecretKey )
    }

    def toAwsCredentials: AWSCredentials =
    {
        new BasicAWSCredentials( accessKeyId, secretKey )
    }
}

@SerialVersionUID( 0L )
case class ConfiguredCredentials( )
    extends Credentials with Serializable
{
    def toAwsCredentials: AWSCredentials =
    {
        new AWSCredentialsProviderChain(
            // Looks at AWS_ACCESS_KEY_ID or AWS_ACCESS_KEY for the access key. And AWS_SECRET_KEY
            // or AWS_SECRET_ACCESS_KEY for the secret key.
            new EnvironmentVariableCredentialsProvider,
            // Looks at Java system properties called aws.accessKeyId and aws.secretKey
            new SystemPropertiesCredentialsProvider,
            // Looks at the file referenced by the AWS_CREDENTIAL_PROFILES_FILE environment
            // variable if defined or ~/.aws/credentials otherwise. From that file it loads the
            // profile referenced by the AWS_PROFILE environment variable or the aws.profile system
            // property. Otherwise it loads the default profile.
            new ProfileCredentialsProvider
        ).getCredentials
    }
}

@SerialVersionUID( 0L )
case class ImplicitCredentials( )
    extends Credentials with Serializable
{
    def toAwsCredentials: AWSCredentials =
    {
        new AWSCredentialsProviderChain(
            new InstanceProfileCredentialsProvider
        ).getCredentials
    }
}


text:
'use strict';

/**
 * Module dependencies.
 */

var classlevels = require('../../app/controllers/classlevels.server.controller');

module.exports = function(app) {
	// Classname Routes
	app.route('/clslevels')
		.get(classlevels.list)
		.post(classlevels.create);
	app.route('/clslevelscreate')
		.get(classlevels.createtest);

	app.route('/clslevels/:classId')
		.get(classlevels.read)
		.put(classlevels.update)
		.delete(classlevels.delete);

	// Finish by binding the article middleware
	app.param('classId', classlevels.ClassByID);
};

redpajama_set_name: RedPajamaGithub


text:
* Line number changes

## 0.1.0 - First Release
* First Release


text:
require "rails_helper"

feature 'User authentication' do
  scenario "existing user signs in" do
    token = "usergithubtoken"
    user = create(:user)

    sign_in_as(user, token)

    expect(page).to have_content user.username
    expect(analytics).to have_tracked("Signed In").for_user(user)
  end

  scenario "new user signs in" do
    token = "usergithubtoken"
    username = "croaky"
    user = build(:user, username: username)

    sign_in_as(user, token)

    expect(page).to have_content(username)
  end

  scenario 'user signs out' do
    token = "usergithubtoken"
    user = create(:user)

    sign_in_as(user, token)
    find('a[href="/sign_out"]').click

    expect(page).not_to have_content user.username
  end
end


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SimpleBlog.Infrastructure;

namespace SimpleBlog.Areas.Admin.Controllers
{
    //Set roles - Admin: not accessible to non-authorized nor non-admin users
    [Authorize(Roles = "admin")]
    [SelectedTab("Users")] // pass the active string tab
    public class UsersController : Controller
    {
        // GET: Admin/Users
        public ActionResult Index()
        {
            return View();
        }
    }
}


text:
My Personal Scripts
===================

This is a collection of scripts I mostly wrote. Be aware that this stuff works
for me, it might not work for you. I do not take responsibility if your hard
drive explodes.


text:
CodeIgniter
###################

The CodeIgniter team would like to thank EllisLab, all the
contributors to the CodeIgniter project and you, the CodeIgniter user.


text:
require "ng/preload/src/package"

module Ng
  module Preload
    module Src
      # http://prioritized.net/blog/gemify-assets-for-rails/
      class Engine < ::Rails::Engine; end
    end
  end
end


text:
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
#endif // WX_PRECOMP

#include "wx/atomic.h"
#include "wx/thread.h"
#include "wx/dynarray.h"
#include "wx/log.h"

WX_DEFINE_ARRAY_PTR(wxThread *, wxArrayThread);

// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------

// number of times to run the loops: the code takes too long to run if we use
// the bigger value with generic atomic operations implementation
#ifdef wxHAS_ATOMIC_OPS
    static const wxInt32 ITERATIONS_NUM = 10000000;
#else
    static const wxInt32 ITERATIONS_NUM = 1000;
#endif

// ----------------------------------------------------------------------------
// test class
// ----------------------------------------------------------------------------

class AtomicTestCase : public CppUnit::TestCase
{
public:
    AtomicTestCase() { }

    enum ETestType
    {
        IncAndDecMixed,
        IncOnly,
        DecOnly
    };

private:
    class MyThread : public wxThread
    {
    public:
        MyThread(wxAtomicInt &operateOn, ETestType testType) : wxThread(wxTHREAD_JOINABLE),
            m_operateOn(operateOn), m_testType(testType) {}

        // thread execution starts here
        virtual void *Entry();

    public:
        wxAtomicInt &m_operateOn;
        ETestType m_testType;
    };

    CPPUNIT_TEST_SUITE( AtomicTestCase );
        CPPUNIT_TEST( TestNoThread );
        CPPUNIT_TEST( TestDecReturn );
        CPPUNIT_TEST( TestTwoThreadsMix );
        CPPUNIT_TEST( TestTenThreadsMix );
        CPPUNIT_TEST( TestTwoThreadsSeparate );
        CPPUNIT_TEST( TestTenThreadsSeparate );
    CPPUNIT_TEST_SUITE_END();

    void TestNoThread();
    void TestDecReturn();
    void TestTenThreadsMix() { TestWithThreads(10, IncAndDecMixed); }
    void TestTwoThreadsMix() { TestWithThreads(2, IncAndDecMixed); }
    void TestTenThreadsSeparate() { TestWithThreads(10, IncOnly); }
    void TestTwoThreadsSeparate() { TestWithThreads(2, IncOnly); }
    void TestWithThreads(int count, ETestType testtype);

    wxDECLARE_NO_COPY_CLASS(AtomicTestCase);
};

// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( AtomicTestCase );

// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( AtomicTestCase, "AtomicTestCase" );

void AtomicTestCase::TestNoThread()
{
    wxAtomicInt int1 = 0,
                int2 = 0;

    for ( wxInt32 i = 0; i < ITERATIONS_NUM; ++i )
    {
        wxAtomicInc(int1);
        wxAtomicDec(int2);
    }

    CPPUNIT_ASSERT( int1 == ITERATIONS_NUM );
    CPPUNIT_ASSERT( int2 == -ITERATIONS_NUM );
}

void AtomicTestCase::TestDecReturn()
{
    wxAtomicInt i(0);
    wxAtomicInc(i);
    wxAtomicInc(i);
    CPPUNIT_ASSERT( i == 2 );

    CPPUNIT_ASSERT( wxAtomicDec(i) > 0 );
    CPPUNIT_ASSERT( wxAtomicDec(i) == 0 );
}

void AtomicTestCase::TestWithThreads(int count, ETestType testType)
{
    wxAtomicInt    int1=0;

    wxArrayThread  threads;

    int i;
    for ( i = 0; i < count; ++i )
    {
        ETestType actualThreadType;
        switch(testType)
        {
        default:
            actualThreadType = testType;
            break;
        case IncOnly:
            actualThreadType =  (i&1)==0 ? IncOnly : DecOnly;
            break;
        }

        MyThread *thread = new MyThread(int1, actualThreadType);

        if ( thread->Create() != wxTHREAD_NO_ERROR )
        {
            wxLogError(wxT("Can't create thread!"));
        }
        else
            threads.Add(thread);
    }

    for ( i = 0; i < count; ++i )
    {
        threads[i]->Run();
    }

    for ( i = 0; i < count; ++i )
    {
        // each thread should return 0, else it detected some problem
        CPPUNIT_ASSERT (threads[i]->Wait() == (wxThread::ExitCode)0);
    }

    CPPUNIT_ASSERT( int1 == 0 );
}

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

void *AtomicTestCase::MyThread::Entry()
{
    wxInt32 negativeValuesSeen = 0;

    for ( wxInt32 i = 0; i < ITERATIONS_NUM; ++i )
    {
        switch ( m_testType )
        {
            case AtomicTestCase::IncAndDecMixed:
                wxAtomicInc(m_operateOn);
                wxAtomicDec(m_operateOn);

                if (m_operateOn < 0)
                    ++negativeValuesSeen;
                break;

            case AtomicTestCase::IncOnly:
                wxAtomicInc(m_operateOn);
                break;

            case AtomicTestCase::DecOnly:
                wxAtomicDec(m_operateOn);
                break;
        }
    }

    return wxUIntToPtr(negativeValuesSeen);
}


text:
<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

	public function _initSession(){
		$session = new Zend_Session_Namespace('session_doc');
		Zend_Registry::set('session_doc', $session);
	}

	 public function  _initRegistry(){
		$this->bootstrap('multidb');
		$docDb = $this->getResource('multidb');
		Zend_Registry::set('db_doc', $docDb->getDb('doc'));
	}

	/*
	protected function _initSetting()
	{
		$setting = new Zend_Config_Ini(APPLICATION_PATH . '/configs/setting.ini', APPLICATION_ENV);
		Zend_Registry::set('setting', $setting);
		return $setting;
	}*/

	protected function _initPlugin()
	{
		$this->bootstrap('frontcontroller');
		$frontController = $this->getResource('frontcontroller');
		/* @var $frontController Zend_Controller_Front */

		$frontController->registerPlugin(new Application_Plugin_Adminauth());
	}
}


text:
<!DOCTYPE html>

          flowchart TD

          subgraph " "
            recipe([recipe.yaml])

            ingestUserCode([steps/ingest.py]) --> ingestMLPStep[["ingest"]]
            ingestMLPStep --> dataParquet[(ingested_data)]

            ingestScoringUserCode([steps/ingest.py]) --> ingestScoringMLPStep[["ingest_scoring"]]
            ingestScoringMLPStep --> dataScoringParquet[(ingested_scoring_data)]

            dataScoring[(ingested_scoring_data)] --> predictMLPStep[["predict"]]
            predictMLPStep --> dataScored[(scored_data)]
          end

          data[(ingested_data)] --> splitStep[["split"]]
          transformUserCode([steps/transform.py]) --> transformMLPStep
          splitUserCode([steps/split.py]) --> splitStep
          splitStep --> splitData0[(training_data)]
          splitStep --> splitData1[(validation_data)]
          splitStep --> splitData2[(test_data)]
          splitData0 --> transformMLPStep[["transform"]]
          splitData1 --> transformMLPStep[["transform"]]

          transformMLPStep --> transformedParquet[(transformed_training_data,
 transformed_validation_data)]
          transformMLPStep --> transformer{{transformer}}
          transformedParquet --> trainMLPStep[["train"]]

          trainUserCode([steps/train.py]) --> trainMLPStep
          customMetricsUserCode([steps/custom_metrics.py]) --> trainMLPStep
          transformer --> trainMLPStep
          trainMLPStep --> run{{run}}
          trainMLPStep --> model{{model}}
          trainMLPStep --> predictedTrainingData[(predicted_training_data)]

          model --> evaluateMLPStep[["evaluate"]]
          splitData1 --> evaluateMLPStep
          splitData2 --> evaluateMLPStep
          customMetricsUserCode --> evaluateMLPStep
          evaluateMLPStep --> model_validation_status{{model_validation_status}}
          run --> registerMLPStep[["register"]]
          run --> evaluateMLPStep

          model_validation_status --> registerMLPStep
          registerMLPStep --> registered_model_version{{registered_model_version}}

          click ingestMLPStep renderMoreInformation "{{ingest_step_help}}"
          click ingestUserCode renderMoreInformation "{{ingest_user_code_help}}"
          click splitStep renderMoreInformation "{{split_step_help}}"
          click transformMLPStep renderMoreInformation "{{transform_step_help}}"
          click transformUserCode renderMoreInformation "{{transform_user_code_help}}"
          click trainMLPStep renderMoreInformation "{{train_step_help}}"
          click trainUserCode renderMoreInformation "{{train_user_code_help}}"
          click evaluateMLPStep renderMoreInformation "{{evaluate_step_help}}"
          click registerMLPStep renderMoreInformation "{{register_step_help}}"
          click customMetricsUserCode renderMoreInformation "{{custom_metrics_user_code_help}}"
          click splitUserCode renderMoreInformation "{{split_user_code_help}}"
          click ingestScoringUserCode renderMoreInformation "{{ingest_user_code_help}}"
          click ingestScoringMLPStep renderMoreInformation "{{ingest_scoring_step_help}}"
          click predictMLPStep renderMoreInformation "{{predict_step_help}}"

          click recipe renderMoreInformation "{{recipe_yaml_help}}"
          click dataParquet renderMoreInformation "{{ingested_data_help}}"
          click data renderMoreInformation "{{ingested_data_help}}"
          click splitData0 renderMoreInformation "{{training_data_help}}"
          click splitData1 renderMoreInformation "{{validation_data_help}}"
          click splitData2 renderMoreInformation "{{test_data_help}}"
          click transformedParquet renderMoreInformation "{{transformed_training_and_validation_data_help}}"
          click run renderMoreInformation "{{mlflow_run_help}}"
          click model renderMoreInformation "{{fitted_model_help}}"
          click predictedTrainingData renderMoreInformation "{{predicted_training_data_help}}"
          click transformer renderMoreInformation "{{fitted_transformer_help}}"
          click model_validation_status renderMoreInformation "{{model_validation_status_help}}"
          click registered_model_version renderMoreInformation "{{registered_model_version_help}}"
          click dataScoringParquet renderMoreInformation "{{ingested_scoring_data_help}}"
          click dataScoring renderMoreInformation "{{ingested_scoring_data_help}}"
          click dataScored renderMoreInformation "{{scored_data_help}}"


text:
> An FRC statistics website.

This is the frontend portion of the website. The backend can be found in [its own repository](https://www.github.com/tervay/AutoScout-API).

### Installation

```bash
# Might work with other versions ¯\_(ツ)_/¯

$ node -v
v8.9.4

$ npm -v
5.6.0
```

```bash
# install dependencies
$ npm i

# run dev server
$ npm start  # or npm run start:dev

# run prod server
$ NODE_ENV=production npm start  # or npm run start:prod

# specify port on production server
$ NODE_ENV=production PORT=80 npm start

# run ESLint
$ npm run lint
```

### Usage

If you want to access the site on a local network device (e.g. a phone), you will need an [ngrok](https://ngrok.com/)
instance running in order to access the backend API. Once you have the backend server running, run ngrok:

```bash
$ ngrok http 8000  # or whatever port the backend is running on
```

Then, modify `src/js/api.js` to properly reflect the ngrok url that gets printed.

Additionally, you won't see any data load unless you have a local instance of the API running. You can do that by
following the setup guide in [the backend repo](https://www.github.com/tervay/AutoScout-API).

### Contribute

Feel free to open any issues regarding anything. You are free to open any PRs you'd like. However, please make sure
your code is well tested before opening a PR.

### License

MIT license.

----

Many thanks to [TheBlueAlliance](https://github.com/the-blue-alliance/the-blue-alliance) for providing an awesome
product and an easy to use API, to [Matt529](https://github.com/Matt529) for helping me get this project off the
ground, and to FIRST for giving the world FRC.


text:
namespace WinFormsApplication.Core
{
    public class DataGridViewItem
    {
        public string Name { get; set; }
        public int Number { get; set; }
        public bool IsChecked { get; set; }
    }
}


text:
layout: post
title:  Perl 6 changes - 2013W12
---
I really should optimize my blog layout, so empty intro wouldn't be
ugly. But I guess I'm too lazy to do it right now, so I have to explain
stuff. Every week, I list the list of changes in Perl 6 specifications,
and its implementations. That's just everything, boring, I know. Still,
it doesn't look that bad on Planet Six, so I guess I will continue to
make these articles :-).

This week, the Rakudo 2013.03 was released. Every change mentioned here
was implemented in it.

### New features
#### Perl 6 specification
* Added `X::Range::InvalidArg`.

#### Rakudo Perl
* Removed lots of `pir::` usage.
* Multi dispatch is now implemented in NQP, not C.
* Released Rakudo 2013.03.


text:
ACCEPTED

#### According to
International Plant Names Index

#### Published in
F. W. H. A. von Humboldt et al. , Nov. gen. sp. 3:59[folio]; 3:76[quarto]. 1818

#### Original name
null

### Remarks
null


text:
package com.hutong.supersdk.sdk.handler.and;

import com.hutong.supersdk.sdk.handler.iosyy.IOSYYIToolsSDK;
import org.springframework.stereotype.Component;

@Component("andIToolsSDK")
public class AndIToolsSDK extends IOSYYIToolsSDK{

	private static final String SDK_ID = "AndITools";

	@Override
	public String getSDKId() {
		return SDK_ID;
	}

}


text:
package org.apache.sysds.runtime.functionobjects;

import org.apache.sysds.runtime.matrix.data.MatrixIndexes;
import org.apache.sysds.runtime.matrix.data.MatrixValue.CellIndex;
import org.apache.sysds.runtime.meta.DataCharacteristics;

public class ReduceDiag extends IndexFunction
{

	private static final long serialVersionUID = 734843929521413928L;

	private static ReduceDiag singleObj = null;

	private ReduceDiag() {
		// nothing to do here
	}

	public static ReduceDiag getReduceDiagFnObject() {
		if ( singleObj == null )
			singleObj = new ReduceDiag();
		return singleObj;
	}

	/*
	 * NOTE: index starts from 1 for cells in a matrix, but index starts from 0 for cells inside a block
	 */

	@Override
	public void execute(MatrixIndexes in, MatrixIndexes out) {
		out.setIndexes(1, 1);
	}

	@Override
	public void execute(CellIndex in, CellIndex out) {
		out.set(0, 0);
	}

	@Override
	public boolean computeDimension(int row, int col, CellIndex retDim) {
		retDim.set(1, 1);
		return true;
	}

	@Override
	public boolean computeDimension(DataCharacteristics in, DataCharacteristics out) {
		out.set(1, 1, 1, 1);
		return true;
	}
}


text:
Sponsors

Interested in sponsoring Ivory Hacks? Contact sponsors@ivoryhacks.io!


text:
namespace perfetto {
namespace protos {
namespace gen {
class TraceStats;
}  // namespace gen
}  // namespace protos
}  // namespace perfetto

namespace tracing {
class TracePacketTokenizer;

// Helpers for deriving information from Perfetto's tracing session statistics.
double COMPONENT_EXPORT(TRACING_CPP)
    GetTraceBufferUsage(const perfetto::protos::gen::TraceStats&);
bool COMPONENT_EXPORT(TRACING_CPP)
    HasLostData(const perfetto::protos::gen::TraceStats&);

void COMPONENT_EXPORT(TRACING_CPP) ReadTraceStats(
    const perfetto::TracingSession::GetTraceStatsCallbackArgs& args,
    base::OnceCallback<void(bool success, float percent_full, bool data_loss)>
        on_stats_callback,
    const scoped_refptr<base::SequencedTaskRunner>& task_runner);

void COMPONENT_EXPORT(TRACING_CPP) ReadTraceAsJson(
    const perfetto::TracingSession::ReadTraceCallbackArgs& args,
    const scoped_refptr<
        base::RefCountedData<std::unique_ptr<TracePacketTokenizer>>>& tokenizer,
    base::OnceCallback<void(std::unique_ptr<std::string>)> on_data_callback,
    base::OnceClosure on_data_complete_callback,
    const scoped_refptr<base::SequencedTaskRunner>& task_runner);

void COMPONENT_EXPORT(TRACING_CPP) ReadTraceAsProtobuf(
    const perfetto::TracingSession::ReadTraceCallbackArgs& args,
    base::OnceCallback<void(std::unique_ptr<std::string>)> on_data_callback,
    base::OnceClosure on_data_complete_callback,
    const scoped_refptr<base::SequencedTaskRunner>& task_runner);

}  // namespace tracing

#endif  // SERVICES_TRACING_PUBLIC_CPP_PERFETTO_PERFETTO_SESSION_H_


text:
"use strict";

const { expect } = require("chai");
const sinon = require("sinon");

const { parseRecvLine, parseRecvLines } = require("../postgresql");

const has = Object.prototype.hasOwnProperty;

describe("Parse event", function() {
  it("parses begin transaction", function() {
    expect(parseRecvLine("BEGIN 123")).to.deep.equal({
      type: "BEGIN",
      tx: "123",
    });
  })

  it("parses commit transaction", function() {
    expect(parseRecvLine("COMMIT 123")).to.deep.equal({
      type: "COMMIT",
      tx: "123",
    });
  })

  it("parses commit transaction with timestamp", function() {
    expect(parseRecvLine("COMMIT 123 (at 2000-01-01 etc)")).to.deep.equal({
      type: "COMMIT",
      tx: "123",
    });
  })

  it("parses UPDATE", function() {
    expect(parseRecvLine("table public.foo: UPDATE:")).to.deep.equal({
      type: "UPDATE",
      schema: "public",
      name: "foo",
      rows: [{}],
    });
  })

  it("parses INSERT", function() {
    expect(parseRecvLine("table public.foo: INSERT:")).to.deep.equal({
      type: "INSERT",
      schema: "public",
      name: "foo",
      rows: [{}],
    });
  })

  it("parses DELETE", function() {
    expect(parseRecvLine("table public.foo: DELETE:")).to.deep.equal({
      type: "DELETE",
      schema: "public",
      name: "foo",
      rows: [{}],
    });
  })

  it("parses null column", function() {
    expect(parseRecvLine("table public.foo: UPDATE: c1[text]:null")).to.deep.equal({
      type: "UPDATE",
      schema: "public",
      name: "foo",
      rows: [{ c1: null }],
    });
  })

  it("parses boolean column", function() {
    expect(parseRecvLine("table public.foo: UPDATE: c1[boolean]:true c2[boolean]:false")).to.deep.equal({
      type: "UPDATE",
      schema: "public",
      name: "foo",
      rows: [{ c1: true, c2: false }],
    });
  })

  it("parses number column", function() {
    expect(parseRecvLine("table public.foo: UPDATE: c1[real]:-7.25")).to.deep.equal({
      type: "UPDATE",
      schema: "public",
      name: "foo",
      rows: [{ c1: -7.25 }],
    });
  })

  it("parses integer column", function() {
    expect(parseRecvLine("table public.foo: UPDATE: c1[integer]:-7")).to.deep.equal({
      type: "UPDATE",
      schema: "public",
      name: "foo",
      rows: [{ c1: -7 }],
    });
  })

  it("parses text column", function() {
    expect(parseRecvLine("table public.foo: UPDATE: c1[text]:'\\Hello, ''Al''\n'")).to.deep.equal({
      type: "UPDATE",
      schema: "public",
      name: "foo",
      rows: [{ c1: "\\Hello, 'Al'\n" }],
    });
  })

  it("parses json column", function() {
    expect(parseRecvLine("table public.foo: UPDATE: c1[jsonb]:'[1, 2]' c2[json]:'null'")).to.deep.equal({
      type: "UPDATE",
      schema: "public",
      name: "foo",
      rows: [{ c1: [1, 2], c2: null }],
    });
  })

  it("parses quoted schema", function() {
    expect(parseRecvLine(`table "quoted.schema".foo: UPDATE:`)).to.deep.equal({
      type: "UPDATE",
      schema: "quoted.schema",
      name: "foo",
      rows: [{}],
    });
  })

  it("parses quoted table name", function() {
    expect(parseRecvLine(`table public."quoted.table": UPDATE:`)).to.deep.equal({
      type: "UPDATE",
      schema: "public",
      name: "quoted.table",
      rows: [{}],
    });
  })

  it("parses quoted column name", function() {
    expect(parseRecvLine(`table public.foo: UPDATE: "myCol"[text]:'Hello'`)).to.deep.equal({
      type: "UPDATE",
      schema: "public",
      name: "foo",
      rows: [{ myCol: "Hello" }],
    });
  })
})

describe("Parse lines", function() {
  it("parses single line", function() {
    let handleEvent = sinon.stub();
    return parseRecvLines(`table public.foo: UPDATE: col[text]:2\n`, handleEvent).then(rest => {
      expect(rest).to.equal("");
      sinon.assert.calledWith(handleEvent, {
        type: "UPDATE",
        schema: "public",
        name: "foo",
        rows: [{ col: "2" }],
      });
    });
  })

  it("parses two lines", function() {
    let handleEvent = sinon.stub();
    return parseRecvLines(`table public.foo: UPDATE: col[text]:2\ntable public.foo: UPDATE: col[text]:3\n`, handleEvent).then(rest => {
      expect(rest).to.equal("");
      sinon.assert.calledTwice(handleEvent);
    });
  })

  it("ignores partial line", function() {
    let handleEvent = sinon.stub();
    return parseRecvLines(`table public.foo: UPDATE: col[text]:2\ntable public.foo:`, handleEvent).then(rest => {
      expect(rest).to.equal("table public.foo:");
      sinon.assert.calledOnce(handleEvent);
    });
  })

  it("newlines in single quotes do not terminate line", function() {
    let handleEvent = sinon.stub();
    return parseRecvLines(`table public.foo: UPDATE: col[text]:'foo\n'\n`, handleEvent).then(rest => {
      expect(rest).to.equal("");
      sinon.assert.calledWith(handleEvent, {
        type: "UPDATE",
        schema: "public",
        name: "foo",
        rows: [{ col: "foo\n" }],
      });
    });
  })

  it("newlines in double quotes do not terminate line", function() {
    let handleEvent = sinon.stub();
    return parseRecvLines(`table public.foo: UPDATE: "c\nol"[text]:3\n`, handleEvent).then(rest => {
      expect(rest).to.equal("");
      sinon.assert.calledWith(handleEvent, {
        type: "UPDATE",
        schema: "public",
        name: "foo",
        rows: [{ "c\nol": "3" }],
      });
    });
  })
})


text:
using System;
using HaloEzAPI.Converter;
using Newtonsoft.Json;

namespace HaloEzAPI.Model.Response.Stats.Halo5
{
    public class StatTimelapse
    {
        public Guid Id { get; set; }
        [JsonConverter(typeof(TimeSpanConverter))]
        public TimeSpan Timelapse { get; set; }
    }
}


text:
const char WebRtcTestBase::kAudioVideoCallConstraints[] =
    "{audio: true, video: true}";
const char WebRtcTestBase::kVideoCallConstraintsQVGA[] =
   "{video: {mandatory: {minWidth: 320, maxWidth: 320, "
   " minHeight: 240, maxHeight: 240}}}";
const char WebRtcTestBase::kVideoCallConstraints360p[] =
   "{video: {mandatory: {minWidth: 640, maxWidth: 640, "
   " minHeight: 360, maxHeight: 360}}}";
const char WebRtcTestBase::kVideoCallConstraintsVGA[] =
   "{video: {mandatory: {minWidth: 640, maxWidth: 640, "
   " minHeight: 480, maxHeight: 480}}}";
const char WebRtcTestBase::kVideoCallConstraints720p[] =
   "{video: {mandatory: {minWidth: 1280, maxWidth: 1280, "
   " minHeight: 720, maxHeight: 720}}}";
const char WebRtcTestBase::kVideoCallConstraints1080p[] =
    "{video: {mandatory: {minWidth: 1920, maxWidth: 1920, "
    " minHeight: 1080, maxHeight: 1080}}}";
const char WebRtcTestBase::kAudioOnlyCallConstraints[] = "{audio: true}";
const char WebRtcTestBase::kVideoOnlyCallConstraints[] = "{video: true}";
const char WebRtcTestBase::kOkGotStream[] = "ok-got-stream";
const char WebRtcTestBase::kFailedWithNotAllowedError[] =
    "failed-with-error-NotAllowedError";
const char WebRtcTestBase::kAudioVideoCallConstraints360p[] =
   "{audio: true, video: {mandatory: {minWidth: 640, maxWidth: 640, "
   " minHeight: 360, maxHeight: 360}}}";
const char WebRtcTestBase::kAudioVideoCallConstraints720p[] =
   "{audio: true, video: {mandatory: {minWidth: 1280, maxWidth: 1280, "
   " minHeight: 720, maxHeight: 720}}}";
const char WebRtcTestBase::kUseDefaultCertKeygen[] = "null";
const char WebRtcTestBase::kUseDefaultAudioCodec[] = "";
const char WebRtcTestBase::kUseDefaultVideoCodec[] = "";
const char WebRtcTestBase::kVP9Profile0Specifier[] = "profile-id=0";
const char WebRtcTestBase::kVP9Profile2Specifier[] = "profile-id=2";
const char WebRtcTestBase::kUndefined[] = "undefined";

namespace {

base::LazyInstance<bool>::DestructorAtExit hit_javascript_errors_ =
    LAZY_INSTANCE_INITIALIZER;

// Intercepts all log messages. We always attach this handler but only look at
// the results if the test requests so. Note that this will only work if the
// WebrtcTestBase-inheriting test cases do not run in parallel (if they did they
// would race to look at the log, which is global to all tests).
bool JavascriptErrorDetectingLogHandler(int severity,
                                        const char* file,
                                        int line,
                                        size_t message_start,
                                        const std::string& str) {
  if (file == NULL || std::string("CONSOLE") != file)
    return false;

  // TODO(crbug.com/918871): Fix AppRTC and stop ignoring this error.
  if (str.find("Synchronous XHR in page dismissal") != std::string::npos)
    return false;

  bool contains_uncaught = str.find("\"Uncaught ") != std::string::npos;
  if (severity == logging::LOG_ERROR ||
      (severity == logging::LOG_INFO && contains_uncaught)) {
    hit_javascript_errors_.Get() = true;
  }

  return false;
}

std::vector<std::string> JsonArrayToVectorOfStrings(
    const std::string& json_array) {
  std::unique_ptr<base::Value> value =
      base::JSONReader::ReadDeprecated(json_array);
  EXPECT_TRUE(value);
  EXPECT_TRUE(value->is_list());
  std::unique_ptr<base::ListValue> list =
      base::ListValue::From(std::move(value));
  std::vector<std::string> vector;
  vector.reserve(list->GetSize());
  for (size_t i = 0; i < list->GetSize(); ++i) {
    base::Value* item;
    EXPECT_TRUE(list->Get(i, &item));
    EXPECT_TRUE(item->is_string());
    std::string item_str;
    EXPECT_TRUE(item->GetAsString(&item_str));
    vector.push_back(std::move(item_str));
  }
  return vector;
}

}  // namespace

WebRtcTestBase::WebRtcTestBase(): detect_errors_in_javascript_(false) {
  // The handler gets set for each test method, but that's fine since this
  // set operation is idempotent.
  logging::SetLogMessageHandler(&JavascriptErrorDetectingLogHandler);
  hit_javascript_errors_.Get() = false;

  EnablePixelOutput();
}

WebRtcTestBase::~WebRtcTestBase() {
  if (detect_errors_in_javascript_) {
    EXPECT_FALSE(hit_javascript_errors_.Get())
        << "Encountered javascript errors during test execution (Search "
        << "for Uncaught or ERROR:CONSOLE in the test output).";
  }
}

bool WebRtcTestBase::GetUserMediaAndAccept(
    content::WebContents* tab_contents) const {
  return GetUserMediaWithSpecificConstraintsAndAccept(
      tab_contents, kAudioVideoCallConstraints);
}

bool WebRtcTestBase::GetUserMediaWithSpecificConstraintsAndAccept(
    content::WebContents* tab_contents,
    const std::string& constraints) const {
  std::string result;
  permissions::PermissionRequestManager::FromWebContents(tab_contents)
      ->set_auto_response_for_test(
          permissions::PermissionRequestManager::ACCEPT_ALL);
  permissions::PermissionRequestObserver observer(tab_contents);
  GetUserMedia(tab_contents, constraints);
  EXPECT_TRUE(observer.request_shown());
  EXPECT_TRUE(content::ExecuteScriptAndExtractString(
      tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result));
  return kOkGotStream == result;
}

bool WebRtcTestBase::GetUserMediaWithSpecificConstraintsAndAcceptIfPrompted(
    content::WebContents* tab_contents,
    const std::string& constraints) const {
  std::string result;
  permissions::PermissionRequestManager::FromWebContents(tab_contents)
      ->set_auto_response_for_test(
          permissions::PermissionRequestManager::ACCEPT_ALL);
  GetUserMedia(tab_contents, constraints);
  EXPECT_TRUE(content::ExecuteScriptAndExtractString(
      tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result));
  return kOkGotStream == result;
}

void WebRtcTestBase::GetUserMediaAndDeny(content::WebContents* tab_contents) {
  return GetUserMediaWithSpecificConstraintsAndDeny(tab_contents,
                                                    kAudioVideoCallConstraints);
}

void WebRtcTestBase::GetUserMediaWithSpecificConstraintsAndDeny(
    content::WebContents* tab_contents,
    const std::string& constraints) const {
  std::string result;
  permissions::PermissionRequestManager::FromWebContents(tab_contents)
      ->set_auto_response_for_test(
          permissions::PermissionRequestManager::DENY_ALL);
  permissions::PermissionRequestObserver observer(tab_contents);
  GetUserMedia(tab_contents, constraints);
  EXPECT_TRUE(observer.request_shown());
  EXPECT_TRUE(content::ExecuteScriptAndExtractString(
      tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result));
  EXPECT_EQ(kFailedWithNotAllowedError, result);
}

void WebRtcTestBase::GetUserMediaAndDismiss(
    content::WebContents* tab_contents) const {
  std::string result;
  permissions::PermissionRequestManager::FromWebContents(tab_contents)
      ->set_auto_response_for_test(
          permissions::PermissionRequestManager::DISMISS);
  permissions::PermissionRequestObserver observer(tab_contents);
  GetUserMedia(tab_contents, kAudioVideoCallConstraints);
  EXPECT_TRUE(observer.request_shown());
  // A dismiss should be treated like a deny.
  EXPECT_TRUE(content::ExecuteScriptAndExtractString(
      tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result));
  EXPECT_EQ(kFailedWithNotAllowedError, result);
}

void WebRtcTestBase::GetUserMediaAndExpectAutoAcceptWithoutPrompt(
    content::WebContents* tab_contents) const {
  std::string result;
  // We issue a GetUserMedia() request. We expect that the origin already has a
  // sticky "accept" permission (e.g. because the caller previously called
  // GetUserMediaAndAccept()), and therefore the GetUserMedia() request
  // automatically succeeds without a prompt.
  // If the caller made a mistake, a prompt may show up instead. For this case,
  // we set an auto-response to avoid leaving the prompt hanging. The choice of
  // DENY_ALL makes sure that the response to the prompt doesn't accidentally
  // result in a newly granted media stream permission.
  permissions::PermissionRequestManager::FromWebContents(tab_contents)
      ->set_auto_response_for_test(
          permissions::PermissionRequestManager::DENY_ALL);
  permissions::PermissionRequestObserver observer(tab_contents);
  GetUserMedia(tab_contents, kAudioVideoCallConstraints);
  EXPECT_FALSE(observer.request_shown());
  EXPECT_TRUE(content::ExecuteScriptAndExtractString(
      tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result));
  EXPECT_EQ(kOkGotStream, result);
}

void WebRtcTestBase::GetUserMediaAndExpectAutoDenyWithoutPrompt(
    content::WebContents* tab_contents) const {
  std::string result;
  // We issue a GetUserMedia() request. We expect that the origin already has a
  // sticky "deny" permission (e.g. because the caller previously called
  // GetUserMediaAndDeny()), and therefore the GetUserMedia() request
  // automatically succeeds without a prompt.
  // If the caller made a mistake, a prompt may show up instead. For this case,
  // we set an auto-response to avoid leaving the prompt hanging. The choice of
  // ACCEPT_ALL makes sure that the response to the prompt doesn't accidentally
  // result in a newly granted media stream permission.
  permissions::PermissionRequestManager::FromWebContents(tab_contents)
      ->set_auto_response_for_test(
          permissions::PermissionRequestManager::ACCEPT_ALL);
  permissions::PermissionRequestObserver observer(tab_contents);
  GetUserMedia(tab_contents, kAudioVideoCallConstraints);
  EXPECT_FALSE(observer.request_shown());
  EXPECT_TRUE(content::ExecuteScriptAndExtractString(
      tab_contents->GetMainFrame(), "obtainGetUserMediaResult();", &result));
  EXPECT_EQ(kFailedWithNotAllowedError, result);
}

void WebRtcTestBase::GetUserMedia(content::WebContents* tab_contents,
                                  const std::string& constraints) const {
  // Request user media: this will launch the media stream info bar or bubble.
  std::string result;
  EXPECT_TRUE(content::ExecuteScriptAndExtractString(
      tab_contents, "doGetUserMedia(" + constraints + ");", &result));
  EXPECT_TRUE(result == "request-callback-denied" ||
              result == "request-callback-granted");
}

content::WebContents* WebRtcTestBase::OpenPageAndGetUserMediaInNewTab(
    const GURL& url) const {
  return OpenPageAndGetUserMediaInNewTabWithConstraints(
      url, kAudioVideoCallConstraints);
}

content::WebContents*
WebRtcTestBase::OpenPageAndGetUserMediaInNewTabWithConstraints(
    const GURL& url,
    const std::string& constraints) const {
  chrome::AddTabAt(browser(), GURL(url::kAboutBlankURL), -1, true);
  ui_test_utils::NavigateToURL(browser(), url);
  content::WebContents* new_tab =
      browser()->tab_strip_model()->GetActiveWebContents();
  // Accept if necessary, but don't expect a prompt (because auto-accept is also
  // okay).
  permissions::PermissionRequestManager::FromWebContents(new_tab)
      ->set_auto_response_for_test(
          permissions::PermissionRequestManager::ACCEPT_ALL);
  GetUserMedia(new_tab, constraints);
  std::string result;
  EXPECT_TRUE(content::ExecuteScriptAndExtractString(
      new_tab->GetMainFrame(), "obtainGetUserMediaResult();", &result));
  EXPECT_EQ(kOkGotStream, result);
  return new_tab;
}

content::WebContents* WebRtcTestBase::OpenTestPageAndGetUserMediaInNewTab(
    const std::string& test_page) const {
  return OpenPageAndGetUserMediaInNewTab(
      embedded_test_server()->GetURL(test_page));
}

content::WebContents* WebRtcTestBase::OpenTestPageInNewTab(
    const std::string& test_page) const {
  chrome::AddTabAt(browser(), GURL(url::kAboutBlankURL), -1, true);
  GURL url = embedded_test_server()->GetURL(test_page);
  ui_test_utils::NavigateToURL(browser(), url);
  content::WebContents* new_tab =
      browser()->tab_strip_model()->GetActiveWebContents();
  // Accept if necessary, but don't expect a prompt (because auto-accept is also
  // okay).
  permissions::PermissionRequestManager::FromWebContents(new_tab)
      ->set_auto_response_for_test(
          permissions::PermissionRequestManager::ACCEPT_ALL);
  return new_tab;
}

void WebRtcTestBase::CloseLastLocalStream(
    content::WebContents* tab_contents) const {
  EXPECT_EQ("ok-stopped",
            ExecuteJavascript("stopLocalStream();", tab_contents));
}

// Convenience method which executes the provided javascript in the context
// of the provided web contents and returns what it evaluated to.
std::string WebRtcTestBase::ExecuteJavascript(
    const std::string& javascript,
    content::WebContents* tab_contents) const {
  std::string result;
  EXPECT_TRUE(content::ExecuteScriptAndExtractString(
      tab_contents, javascript, &result));
  return result;
}

void WebRtcTestBase::ChangeToLegacyGetStats(content::WebContents* tab) const {
  content::ExecuteScriptAsync(tab, "changeToLegacyGetStats()");
}

void WebRtcTestBase::SetupPeerconnectionWithLocalStream(
    content::WebContents* tab,
    const std::string& certificate_keygen_algorithm) const {
  SetupPeerconnectionWithoutLocalStream(tab, certificate_keygen_algorithm);
  EXPECT_EQ("ok-added", ExecuteJavascript("addLocalStream()", tab));
}

void WebRtcTestBase::SetupPeerconnectionWithoutLocalStream(
    content::WebContents* tab,
    const std::string& certificate_keygen_algorithm) const {
  std::string javascript = base::StringPrintf(
      "preparePeerConnection(%s)", certificate_keygen_algorithm.c_str());
  EXPECT_EQ("ok-peerconnection-created", ExecuteJavascript(javascript, tab));
}

void WebRtcTestBase::SetupPeerconnectionWithCertificateAndLocalStream(
    content::WebContents* tab,
    const std::string& certificate) const {
  SetupPeerconnectionWithCertificateWithoutLocalStream(tab, certificate);
  EXPECT_EQ("ok-added", ExecuteJavascript("addLocalStream()", tab));
}

void WebRtcTestBase::SetupPeerconnectionWithCertificateWithoutLocalStream(
    content::WebContents* tab,
    const std::string& certificate) const {
  std::string javascript = base::StringPrintf(
      "preparePeerConnectionWithCertificate(%s)", certificate.c_str());
  EXPECT_EQ("ok-peerconnection-created", ExecuteJavascript(javascript, tab));
}

void WebRtcTestBase::SetupPeerconnectionWithConstraintsAndLocalStream(
    content::WebContents* tab,
    const std::string& constraints,
    const std::string& certificate_keygen_algorithm) const {
  std::string javascript = base::StringPrintf(
      "preparePeerConnection(%s, %s)", certificate_keygen_algorithm.c_str(),
      constraints.c_str());
  EXPECT_EQ("ok-peerconnection-created", ExecuteJavascript(javascript, tab));
  EXPECT_EQ("ok-added", ExecuteJavascript("addLocalStream()", tab));
}

std::string WebRtcTestBase::CreateLocalOffer(
    content::WebContents* from_tab) const {
  std::string response = ExecuteJavascript("createLocalOffer({})", from_tab);
  EXPECT_EQ("ok-", response.substr(0, 3)) << "Failed to create local offer: "
      << response;

  std::string local_offer = response.substr(3);
  return local_offer;
}

std::string WebRtcTestBase::CreateAnswer(std::string local_offer,
                                         content::WebContents* to_tab) const {
  std::string javascript =
      base::StringPrintf("receiveOfferFromPeer('%s', {})", local_offer.c_str());
  std::string response = ExecuteJavascript(javascript, to_tab);
  EXPECT_EQ("ok-", response.substr(0, 3))
      << "Receiving peer failed to receive offer and create answer: "
      << response;

  std::string answer = response.substr(3);
  response = ExecuteJavascript(
      base::StringPrintf("verifyDefaultCodecs('%s')", answer.c_str()),
      to_tab);
  EXPECT_EQ("ok-", response.substr(0, 3))
      << "Receiving peer failed to verify default codec: " << response;
  return answer;
}

void WebRtcTestBase::ReceiveAnswer(const std::string& answer,
                                   content::WebContents* from_tab) const {
  ASSERT_EQ(
      "ok-accepted-answer",
      ExecuteJavascript(
          base::StringPrintf("receiveAnswerFromPeer('%s')", answer.c_str()),
          from_tab));
}

void WebRtcTestBase::GatherAndSendIceCandidates(
    content::WebContents* from_tab,
    content::WebContents* to_tab) const {
  std::string ice_candidates =
      ExecuteJavascript("getAllIceCandidates()", from_tab);

  EXPECT_EQ("ok-received-candidates", ExecuteJavascript(
      base::StringPrintf("receiveIceCandidates('%s')", ice_candidates.c_str()),
      to_tab));
}

void WebRtcTestBase::CreateDataChannel(content::WebContents* tab,
                                       const std::string& label) {
  EXPECT_EQ("ok-created",
            ExecuteJavascript("createDataChannel('" + label + "')", tab));
}

void WebRtcTestBase::NegotiateCall(content::WebContents* from_tab,
                                   content::WebContents* to_tab) const {
  std::string local_offer = CreateLocalOffer(from_tab);
  std::string answer = CreateAnswer(local_offer, to_tab);
  ReceiveAnswer(answer, from_tab);

  // Send all ICE candidates (wait for gathering to finish if necessary).
  GatherAndSendIceCandidates(to_tab, from_tab);
  GatherAndSendIceCandidates(from_tab, to_tab);
}

void WebRtcTestBase::VerifyLocalDescriptionContainsCertificate(
    content::WebContents* tab,
    const std::string& certificate) const {
  std::string javascript = base::StringPrintf(
      "verifyLocalDescriptionContainsCertificate(%s)", certificate.c_str());
  EXPECT_EQ("ok-verified", ExecuteJavascript(javascript, tab));
}

void WebRtcTestBase::HangUp(content::WebContents* from_tab) const {
  EXPECT_EQ("ok-call-hung-up", ExecuteJavascript("hangUp()", from_tab));
}

void WebRtcTestBase::DetectErrorsInJavaScript() {
  detect_errors_in_javascript_ = true;
}

void WebRtcTestBase::StartDetectingVideo(
    content::WebContents* tab_contents,
    const std::string& video_element) const {
  std::string javascript = base::StringPrintf(
      "startDetection('%s', 320, 240)", video_element.c_str());
  EXPECT_EQ("ok-started", ExecuteJavascript(javascript, tab_contents));
}

bool WebRtcTestBase::WaitForVideoToPlay(
    content::WebContents* tab_contents) const {
  bool is_video_playing = test::PollingWaitUntil(
      "isVideoPlaying()", "video-playing", tab_contents);
  EXPECT_TRUE(is_video_playing);
  return is_video_playing;
}

std::string WebRtcTestBase::GetStreamSize(
    content::WebContents* tab_contents,
    const std::string& video_element) const {
  std::string javascript =
      base::StringPrintf("getStreamSize('%s')", video_element.c_str());
  std::string result = ExecuteJavascript(javascript, tab_contents);
  EXPECT_TRUE(base::StartsWith(result, "ok-", base::CompareCase::SENSITIVE));
  return result.substr(3);
}

bool WebRtcTestBase::OnWin8OrHigher() const {
#if defined(OS_WIN)
  return base::win::GetVersion() >= base::win::Version::WIN8;
#else
  return false;
#endif
}

void WebRtcTestBase::OpenDatabase(content::WebContents* tab) const {
  EXPECT_EQ("ok-database-opened", ExecuteJavascript("openDatabase()", tab));
}

void WebRtcTestBase::CloseDatabase(content::WebContents* tab) const {
  EXPECT_EQ("ok-database-closed", ExecuteJavascript("closeDatabase()", tab));
}

void WebRtcTestBase::DeleteDatabase(content::WebContents* tab) const {
  EXPECT_EQ("ok-database-deleted", ExecuteJavascript("deleteDatabase()", tab));
}

void WebRtcTestBase::GenerateAndCloneCertificate(
    content::WebContents* tab, const std::string& keygen_algorithm) const {
  std::string javascript = base::StringPrintf(
      "generateAndCloneCertificate(%s)", keygen_algorithm.c_str());
  EXPECT_EQ("ok-generated-and-cloned", ExecuteJavascript(javascript, tab));
}

void WebRtcTestBase::VerifyStatsGeneratedCallback(
    content::WebContents* tab) const {
  EXPECT_EQ("ok-got-stats",
            ExecuteJavascript("verifyLegacyStatsGenerated()", tab));
}

std::vector<std::string> WebRtcTestBase::VerifyStatsGeneratedPromise(
    content::WebContents* tab) const {
  std::string result = ExecuteJavascript("verifyStatsGeneratedPromise()", tab);
  EXPECT_TRUE(base::StartsWith(result, "ok-", base::CompareCase::SENSITIVE));
  return JsonArrayToVectorOfStrings(result.substr(3));
}

double WebRtcTestBase::MeasureGetStatsCallbackPerformance(
    content::WebContents* tab) const {
  std::string result = ExecuteJavascript(
      "measureGetStatsCallbackPerformance()", tab);
  EXPECT_TRUE(base::StartsWith(result, "ok-", base::CompareCase::SENSITIVE));
  double ms;
  if (!base::StringToDouble(result.substr(3), &ms))
    return std::numeric_limits<double>::infinity();
  return ms;
}

scoped_refptr<content::TestStatsReportDictionary>
WebRtcTestBase::GetStatsReportDictionary(content::WebContents* tab) const {
  std::string result = ExecuteJavascript("getStatsReportDictionary()", tab);
  EXPECT_TRUE(base::StartsWith(result, "ok-", base::CompareCase::SENSITIVE));
  std::unique_ptr<base::Value> parsed_json =
      base::JSONReader::ReadDeprecated(result.substr(3));
  base::DictionaryValue* dictionary;
  CHECK(parsed_json);
  CHECK(parsed_json->GetAsDictionary(&dictionary));
  ignore_result(parsed_json.release());
  return scoped_refptr<content::TestStatsReportDictionary>(
      new content::TestStatsReportDictionary(
          std::unique_ptr<base::DictionaryValue>(dictionary)));
}

double WebRtcTestBase::MeasureGetStatsPerformance(
    content::WebContents* tab) const {
  std::string result = ExecuteJavascript("measureGetStatsPerformance()", tab);
  EXPECT_TRUE(base::StartsWith(result, "ok-", base::CompareCase::SENSITIVE));
  double ms;
  if (!base::StringToDouble(result.substr(3), &ms))
    return std::numeric_limits<double>::infinity();
  return ms;
}

std::vector<std::string> WebRtcTestBase::GetMandatoryStatsTypes(
    content::WebContents* tab) const {
  return JsonArrayToVectorOfStrings(
      ExecuteJavascript("getMandatoryStatsTypes()", tab));
}

void WebRtcTestBase::SetDefaultAudioCodec(
    content::WebContents* tab,
    const std::string& audio_codec) const {
  EXPECT_EQ("ok", ExecuteJavascript(
      "setDefaultAudioCodec('" + audio_codec + "')", tab));
}

void WebRtcTestBase::SetDefaultVideoCodec(content::WebContents* tab,
                                          const std::string& video_codec,
                                          bool prefer_hw_codec,
                                          const std::string& profile) const {
  std::string codec_profile = profile;
  // When no |profile| is given, we default VP9 to Profile 0.
  if (video_codec.compare("VP9") == 0 && codec_profile.empty())
    codec_profile = kVP9Profile0Specifier;

  EXPECT_EQ("ok", ExecuteJavascript(
                      "setDefaultVideoCodec('" + video_codec + "'," +
                          (prefer_hw_codec ? "true" : "false") + "," +
                          (codec_profile.empty() ? "null"
                                                 : "'" + codec_profile + "'") +
                          ")",
                      tab));
}

void WebRtcTestBase::EnableOpusDtx(content::WebContents* tab) const {
  EXPECT_EQ("ok-forced", ExecuteJavascript("forceOpusDtx()", tab));
}

std::string WebRtcTestBase::GetDesktopMediaStream(content::WebContents* tab) {
  DCHECK(static_cast<bool>(LoadDesktopCaptureExtension()));

  // Post a task to the extension, opening a desktop media stream.
  return ExecuteJavascript("openDesktopMediaStream()", tab);
}

base::Optional<std::string> WebRtcTestBase::LoadDesktopCaptureExtension() {
  base::Optional<std::string> extension_id;
  if (!desktop_capture_extension_.get()) {
    extensions::ChromeTestExtensionLoader loader(browser()->profile());
    base::FilePath extension_path;
    EXPECT_TRUE(base::PathService::Get(chrome::DIR_TEST_DATA, &extension_path));
    extension_path = extension_path.AppendASCII("extensions/desktop_capture");
    desktop_capture_extension_ = loader.LoadExtension(extension_path);
    LOG(INFO) << "Loaded desktop capture extension, id = "
              << desktop_capture_extension_->id();

    extensions::ExtensionRegistry* registry =
        extensions::ExtensionRegistry::Get(browser()->profile());

    EXPECT_TRUE(registry->enabled_extensions().GetByID(
        desktop_capture_extension_->id()));
  }
  if (desktop_capture_extension_)
    extension_id.emplace(desktop_capture_extension_->id());
  return extension_id;
}


text:
// Type definitions for minimist 1.2.0
// Project: https://github.com/substack/minimist
// Definitions by: Bart van der Schoor <https://github.com/Bartvds>, Necroskillz <https://github.com/Necroskillz>, kamranayub <https://github.com/kamranayub>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

/**
 * Return an argument object populated with the array arguments from args
 *
 * @param args An optional argument array (typically `process.argv.slice(2)`)
 * @param opts An optional options object to customize the parsing
 */
declare function minimist(args?: string[], opts?: minimist.Opts): minimist.ParsedArgs;

/**
 * Return an argument object populated with the array arguments from args. Strongly-typed
 * to be the intersect of type T with minimist.ParsedArgs.
 *
 * @type T The type that will be intersected with minimist.ParsedArgs to represent the argument object
 * @param args An optional argument array (typically `process.argv.slice(2)`)
 * @param opts An optional options object to customize the parsing
 */
declare function minimist<T>(args?: string[], opts?: minimist.Opts): T & minimist.ParsedArgs;

/**
 * Return an argument object populated with the array arguments from args. Strongly-typed
 * to be the the type T which should extend minimist.ParsedArgs
 *
 * @type T The type that extends minimist.ParsedArgs and represents the argument object
 * @param args An optional argument array (typically `process.argv.slice(2)`)
 * @param opts An optional options object to customize the parsing
 */
declare function minimist<T extends minimist.ParsedArgs>(args?: string[], opts?: minimist.Opts): T;

declare namespace minimist {
    export interface Opts {
        /**
         * A string or array of strings argument names to always treat as strings
         */
        string?: string | string[];

        /**
         * A boolean, string or array of strings to always treat as booleans. If true will treat
         * all double hyphenated arguments without equals signs as boolean (e.g. affects `--foo`, not `-f` or `--foo=bar`)
         */
        boolean?: boolean | string | string[];

        /**
         * An object mapping string names to strings or arrays of string argument names to use as aliases
         */
        alias?: { [key: string]: string | string[] };

        /**
         * An object mapping string argument names to default values
         */
        default?: { [key: string]: any };

        /**
         * When true, populate argv._ with everything after the first non-option
         */
        stopEarly?: boolean;

        /**
         * A function which is invoked with a command line parameter not defined in the opts
         * configuration object. If the function returns false, the unknown option is not added to argv
         */
        unknown?: (arg: string) => boolean;

        /**
         * When true, populate argv._ with everything before the -- and argv['--'] with everything after the --.
         * Note that with -- set, parsing for arguments still stops after the `--`.
         */
        '--'?: boolean;
    }

    export interface ParsedArgs {
        [arg: string]: any;

        /**
         * If opts['--'] is true, populated with everything after the --
         */
        '--'?: string[];

        /**
         * Contains all the arguments that didn't have an option associated with them
         */
        _: string[];
    }
}