text:
package com.phaller.rasync
package test
package opal

import java.net.URL

import com.phaller.rasync.cell._
import com.phaller.rasync.lattice.Updater
import com.phaller.rasync.pool.{ HandlerPool, SchedulingStrategy }
import scala.concurrent.Await
import scala.concurrent.duration._

import org.opalj.Success

import org.opalj.br.{ ClassFile, Method }
import org.opalj.br.analyses.{ BasicReport, Project, ProjectAnalysisApplication }
import org.opalj.br.instructions.GETFIELD
import org.opalj.br.instructions.GETSTATIC
import org.opalj.br.instructions.PUTFIELD
import org.opalj.br.instructions.PUTSTATIC
import org.opalj.br.instructions.MONITORENTER
import org.opalj.br.instructions.MONITOREXIT
import org.opalj.br.instructions.NEW
import org.opalj.br.instructions.NEWARRAY
import org.opalj.br.instructions.MULTIANEWARRAY
import org.opalj.br.instructions.ANEWARRAY
import org.opalj.br.instructions.AALOAD
import org.opalj.br.instructions.AASTORE
import org.opalj.br.instructions.ARRAYLENGTH
import org.opalj.br.instructions.LALOAD
import org.opalj.br.instructions.IALOAD
import org.opalj.br.instructions.CALOAD
import org.opalj.br.instructions.BALOAD
import org.opalj.br.instructions.BASTORE
import org.opalj.br.instructions.CASTORE
import org.opalj.br.instructions.IASTORE
import org.opalj.br.instructions.LASTORE
import org.opalj.br.instructions.SASTORE
import org.opalj.br.instructions.SALOAD
import org.opalj.br.instructions.DALOAD
import org.opalj.br.instructions.FALOAD
import org.opalj.br.instructions.FASTORE
import org.opalj.br.instructions.DASTORE
import org.opalj.br.instructions.INVOKEDYNAMIC
import org.opalj.br.instructions.INVOKESTATIC
import org.opalj.br.instructions.INVOKESPECIAL
import org.opalj.br.instructions.INVOKEVIRTUAL
import org.opalj.br.instructions.INVOKEINTERFACE
import org.opalj.br.instructions.MethodInvocationInstruction
import org.opalj.br.instructions.NonVirtualMethodInvocationInstruction
import org.opalj.bytecode.JRELibraryFolder
import scala.util.Try

import com.phaller.rasync.pool.DefaultScheduling
import com.phaller.rasync.pool.SourcesWithManySourcesFirst
import com.phaller.rasync.pool.SourcesWithManySourcesLast
import com.phaller.rasync.pool.SourcesWithManyTargetsFirst
import com.phaller.rasync.pool.SourcesWithManyTargetsLast
import com.phaller.rasync.pool.TargetsWithManySourcesFirst
import com.phaller.rasync.pool.TargetsWithManySourcesLast
import com.phaller.rasync.pool.TargetsWithManyTargetsFirst
import com.phaller.rasync.pool.TargetsWithManyTargetsLast
import com.phaller.rasync.test.opal.ifds.Fact
import com.phaller.rasync.test.opal.ifds.IFDSProperty

import org.opalj.util.PerformanceEvaluation
import org.opalj.br.DeclaredMethod

// A strategy tailored to PurityAnalysis
object PurityStrategy extends SchedulingStrategy[Purity, Null] {
  override def calcPriority(dependentCell: Cell[Purity, Null], other: Cell[Purity, Null], value: Try[ValueOutcome[Purity]]): Int = value match {
    case scala.util.Success(FinalOutcome(Impure)) => -1
    case _ => 1
  }

  override def calcPriority(dependentCell: Cell[Purity, Null], value: Try[Purity]): Int = value match {
    case scala.util.Success(Pure) => 0
    case _ => -1
  }
}

object PurityAnalysis extends ProjectAnalysisApplication {

  override def main(args: Array[String]): Unit = {
    val lib = Project(new java.io.File(args(args.length - 1))) //JRELibraryFolder.getAbsolutePath))

    println("Heap size: " + Runtime.getRuntime().maxMemory())

    schedulingStrategy = new DefaultScheduling[Purity, Null]
    PerformanceEvaluation.time {
      val report = PurityAnalysis.doAnalyze(lib.recreate(), List.empty, () => false)
    } { t ⇒ println(s"DefaultScheduling(Warmup),${t.timeSpan}") }

    for {
      scheduling ← List(
        new DefaultScheduling[Purity, Null],
        new SourcesWithManyTargetsFirst[Purity, Null],
        new SourcesWithManyTargetsLast[Purity, Null],
        new TargetsWithManySourcesFirst[Purity, Null],
        new TargetsWithManySourcesLast[Purity, Null],
        new TargetsWithManyTargetsFirst[Purity, Null],
        new TargetsWithManyTargetsLast[Purity, Null],
        new SourcesWithManySourcesFirst[Purity, Null],
        new SourcesWithManySourcesLast[Purity, Null],
        PurityStrategy)
      i ← (0 until 5)
    } {
      val p = lib.recreate()
      schedulingStrategy = scheduling
      PerformanceEvaluation.time {
        val report = PurityAnalysis.doAnalyze(p, List.empty, () => false)
      } { t ⇒ println(s"$scheduling,${t.timeSpan}") }
      //println(report.toConsoleString.split("\n").slice(0, 2).mkString("\n"))
    }
  }

  var schedulingStrategy: SchedulingStrategy[Purity, Null] = null

  override def doAnalyze(
    project: Project[URL],
    parameters: Seq[String] = List.empty,
    isInterrupted: () ⇒ Boolean): BasicReport = {

    val startTime = System.currentTimeMillis // Used for measuring execution time
    // 1. Initialization of key data structures (one cell(completer) per method)
    implicit val pool: HandlerPool[Purity, Null] = new HandlerPool(key = PurityKey, parallelism = 10, schedulingStrategy = schedulingStrategy)
    var methodToCell = Map.empty[Method, Cell[Purity, Null]]
    for {
      classFile <- project.allProjectClassFiles
      method <- classFile.methods
    } {
      val cell = pool.mkCell(_ => {
        analyze(project, methodToCell, classFile, method)
      })(Updater.partialOrderingToUpdater)
      methodToCell = methodToCell + ((method, cell))
    }

    val middleTime = System.currentTimeMillis

    // 2. trigger analyses
    for {
      classFile <- project.allProjectClassFiles.par
      method <- classFile.methods
    } {
      methodToCell(method).trigger()
    }
    val fut = pool.quiescentResolveCell
    Await.ready(fut, 30.minutes)
    pool.shutdown()

    val endTime = System.currentTimeMillis

    val setupTime = middleTime - startTime
    val analysisTime = endTime - middleTime
    val combinedTime = endTime - startTime

    val pureMethods = methodToCell.filter(_._2.getResult() match {
      case Pure => true
      case _ => false
    }).keys

    val pureMethodsInfo = pureMethods.map(m => m.toJava).toList.sorted

    BasicReport(s"pure methods analysis:\nPURE=${pureMethods.size}\n\n" + pureMethodsInfo.mkString("\n") +
      s"\nSETUP TIME: $setupTime" +
      s"\nANALYIS TIME: $analysisTime" +
      s"\nCOMBINED TIME: $combinedTime")
  }

  /**
   * Determines the purity of the given method.
   */
  def analyze(
    project: Project[URL],
    methodToCell: Map[Method, Cell[Purity, Null]],
    classFile: ClassFile,
    method: Method): Outcome[Purity] = {
    import project.nonVirtualCall

    val cell = methodToCell(method)

    if ( // Due to a lack of knowledge, we classify all native methods or methods that
    // belong to a library (and hence lack the body) as impure...
    method.body.isEmpty /*HERE: method.isNative || "isLibraryMethod(method)"*/ ||
      // for simplicity we are just focusing on methods that do not take objects as parameters
      method.parameterTypes.exists(!_.isBaseType)) {
      return FinalOutcome(Impure)
    }

    val dependencies = scala.collection.mutable.Set.empty[Method]
    val declaringClassType = classFile.thisType
    val methodDescriptor = method.descriptor
    val methodName = method.name
    val body = method.body.get
    val instructions = body.instructions
    val maxPC = instructions.size

    var currentPC = 0
    while (currentPC < maxPC) {
      val instruction = instructions(currentPC)

      (instruction.opcode: @scala.annotation.switch) match {
        case GETSTATIC.opcode ⇒
          val GETSTATIC(declaringClass, fieldName, fieldType) = instruction
          import project.resolveFieldReference
          resolveFieldReference(declaringClass, fieldName, fieldType) match {

            case Some(field) if field.isFinal ⇒ NoOutcome
            /* Nothing to do; constants do not impede purity! */

            // case Some(field) if field.isPrivate /*&& field.isNonFinal*/ ⇒
            // check if the field is effectively final

            case _ ⇒
              return FinalOutcome(Impure);
          }

        case INVOKESPECIAL.opcode | INVOKESTATIC.opcode ⇒ instruction match {

          case MethodInvocationInstruction(`declaringClassType`, _, `methodName`, `methodDescriptor`) ⇒
          // We have a self-recursive call; such calls do not influence
          // the computation of the method's purity and are ignored.
          // Let's continue with the evaluation of the next instruction.

          case mii: NonVirtualMethodInvocationInstruction ⇒

            nonVirtualCall(method.classFile.thisType, mii) match {

              case Success(callee) ⇒
                /* Recall that self-recursive calls are handled earlier! */
                dependencies.add(callee)

              case _ /* Empty or Failure */ ⇒

                // We know nothing about the target method (it is not
                // found in the scope of the current project).
                return FinalOutcome(Impure)
            }

        }

        case NEW.opcode |
          GETFIELD.opcode |
          PUTFIELD.opcode | PUTSTATIC.opcode |
          NEWARRAY.opcode | MULTIANEWARRAY.opcode | ANEWARRAY.opcode |
          AALOAD.opcode | AASTORE.opcode |
          BALOAD.opcode | BASTORE.opcode |
          CALOAD.opcode | CASTORE.opcode |
          SALOAD.opcode | SASTORE.opcode |
          IALOAD.opcode | IASTORE.opcode |
          LALOAD.opcode | LASTORE.opcode |
          DALOAD.opcode | DASTORE.opcode |
          FALOAD.opcode | FASTORE.opcode |
          ARRAYLENGTH.opcode |
          MONITORENTER.opcode | MONITOREXIT.opcode |
          INVOKEDYNAMIC.opcode | INVOKEVIRTUAL.opcode | INVOKEINTERFACE.opcode ⇒
          return FinalOutcome(Impure)

        case _ ⇒
        /* All other instructions (IFs, Load/Stores, Arith., etc.) are pure. */
      }
      currentPC = body.pcOfNextInstruction(currentPC)
    }

    // Every method that is not identified as being impure is (conditionally) pure.
    if (dependencies.isEmpty) {
      FinalOutcome(Pure)
    } else {
      cell.when(dependencies.map(methodToCell))(c)
      NextOutcome(UnknownPurity) // == NoOutcome
    }
  }

  def c(v: Iterable[(Cell[Purity, Null], Try[ValueOutcome[Purity]])]): Outcome[Purity] = {
    // If any dependee is Impure, the dependent Cell is impure.
    // Otherwise, we do not know anything new.
    // Exception will be rethrown.
    if (v.collectFirst({
      case (_, scala.util.Success(FinalOutcome(Impure))) => true
      case (_, scala.util.Failure(_)) => true
    }).isDefined)
      FinalOutcome(Impure)
    else NoOutcome
  }
}


text:
package org.apache.lucene.index;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.codecs.lucene41.Lucene41PostingsFormat;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.FieldCache;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.NoSuchDirectoryException;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util._TestUtil;
import org.junit.Assume;

public class TestDirectoryReader extends LuceneTestCase {

  public void testDocument() throws IOException {
    SegmentReader [] readers = new SegmentReader[2];
    Directory dir = newDirectory();
    Document doc1 = new Document();
    Document doc2 = new Document();
    DocHelper.setupDoc(doc1);
    DocHelper.setupDoc(doc2);
    DocHelper.writeDoc(random(), dir, doc1);
    DocHelper.writeDoc(random(), dir, doc2);
    DirectoryReader reader = DirectoryReader.open(dir);
    assertTrue(reader != null);
    assertTrue(reader instanceof StandardDirectoryReader);

    Document newDoc1 = reader.document(0);
    assertTrue(newDoc1 != null);
    assertTrue(DocHelper.numFields(newDoc1) == DocHelper.numFields(doc1) - DocHelper.unstored.size());
    Document newDoc2 = reader.document(1);
    assertTrue(newDoc2 != null);
    assertTrue(DocHelper.numFields(newDoc2) == DocHelper.numFields(doc2) - DocHelper.unstored.size());
    Terms vector = reader.getTermVectors(0).terms(DocHelper.TEXT_FIELD_2_KEY);
    assertNotNull(vector);

    reader.close();
    if (readers[0] != null) readers[0].close();
    if (readers[1] != null) readers[1].close();
    dir.close();
  }

  public void testMultiTermDocs() throws IOException {
    Directory ramDir1=newDirectory();
    addDoc(random(), ramDir1, "test foo", true);
    Directory ramDir2=newDirectory();
    addDoc(random(), ramDir2, "test blah", true);
    Directory ramDir3=newDirectory();
    addDoc(random(), ramDir3, "test wow", true);

    IndexReader[] readers1 = new IndexReader[]{DirectoryReader.open(ramDir1), DirectoryReader.open(ramDir3)};
    IndexReader[] readers2 = new IndexReader[]{DirectoryReader.open(ramDir1), DirectoryReader.open(ramDir2), DirectoryReader.open(ramDir3)};
    MultiReader mr2 = new MultiReader(readers1);
    MultiReader mr3 = new MultiReader(readers2);

    // test mixing up TermDocs and TermEnums from different readers.
    TermsEnum te2 = MultiFields.getTerms(mr2, "body").iterator(null);
    te2.seekCeil(new BytesRef("wow"));
    DocsEnum td = _TestUtil.docs(random(), mr2,
                                 "body",
                                 te2.term(),
                                 MultiFields.getLiveDocs(mr2),
                                 null,
                                 0);

    TermsEnum te3 = MultiFields.getTerms(mr3, "body").iterator(null);
    te3.seekCeil(new BytesRef("wow"));
    td = _TestUtil.docs(random(), te3, MultiFields.getLiveDocs(mr3),
                        td,
                        0);

    int ret = 0;

    // This should blow up if we forget to check that the TermEnum is from the same
    // reader as the TermDocs.
    while (td.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) ret += td.docID();

    // really a dummy assert to ensure that we got some docs and to ensure that
    // nothing is eliminated by hotspot
    assertTrue(ret > 0);
    readers1[0].close();
    readers1[1].close();
    readers2[0].close();
    readers2[1].close();
    readers2[2].close();
    ramDir1.close();
    ramDir2.close();
    ramDir3.close();
  }

  private void addDoc(Random random, Directory ramDir1, String s, boolean create) throws IOException {
    IndexWriter iw = new IndexWriter(ramDir1, newIndexWriterConfig(
        TEST_VERSION_CURRENT,
        new MockAnalyzer(random)).setOpenMode(
        create ? OpenMode.CREATE : OpenMode.APPEND));
    Document doc = new Document();
    doc.add(newTextField("body", s, Field.Store.NO));
    iw.addDocument(doc);
    iw.close();
  }

  public void testIsCurrent() throws Exception {
    Directory d = newDirectory();
    IndexWriter writer = new IndexWriter(d, newIndexWriterConfig(
      TEST_VERSION_CURRENT, new MockAnalyzer(random())));
    addDocumentWithFields(writer);
    writer.close();
    // set up reader:
    DirectoryReader reader = DirectoryReader.open(d);
    assertTrue(reader.isCurrent());
    // modify index by adding another document:
    writer = new IndexWriter(d, newIndexWriterConfig(TEST_VERSION_CURRENT,
        new MockAnalyzer(random())).setOpenMode(OpenMode.APPEND));
    addDocumentWithFields(writer);
    writer.close();
    assertFalse(reader.isCurrent());
    // re-create index:
    writer = new IndexWriter(d, newIndexWriterConfig(TEST_VERSION_CURRENT,
        new MockAnalyzer(random())).setOpenMode(OpenMode.CREATE));
    addDocumentWithFields(writer);
    writer.close();
    assertFalse(reader.isCurrent());
    reader.close();
    d.close();
  }

  /**
   * Tests the IndexReader.getFieldNames implementation
   * @throws Exception on error
   */
  public void testGetFieldNames() throws Exception {
      Directory d = newDirectory();
      // set up writer
      IndexWriter writer = new IndexWriter(
          d,
          newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random()))
      );

      Document doc = new Document();

      FieldType customType3 = new FieldType();
      customType3.setStored(true);

      doc.add(new StringField("keyword", "test1", Field.Store.YES));
      doc.add(new TextField("text", "test1", Field.Store.YES));
      doc.add(new Field("unindexed", "test1", customType3));
      doc.add(new TextField("unstored","test1", Field.Store.NO));
      writer.addDocument(doc);

      writer.close();
      // set up reader
      DirectoryReader reader = DirectoryReader.open(d);
      FieldInfos fieldInfos = MultiFields.getMergedFieldInfos(reader);
      assertNotNull(fieldInfos.fieldInfo("keyword"));
      assertNotNull(fieldInfos.fieldInfo("text"));
      assertNotNull(fieldInfos.fieldInfo("unindexed"));
      assertNotNull(fieldInfos.fieldInfo("unstored"));
      reader.close();
      // add more documents
      writer = new IndexWriter(
          d,
          newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).
              setOpenMode(OpenMode.APPEND).
              setMergePolicy(newLogMergePolicy())
      );
      // want to get some more segments here
      int mergeFactor = ((LogMergePolicy) writer.getConfig().getMergePolicy()).getMergeFactor();
      for (int i = 0; i < 5*mergeFactor; i++) {
        doc = new Document();
        doc.add(new StringField("keyword", "test1", Field.Store.YES));
        doc.add(new TextField("text", "test1", Field.Store.YES));
        doc.add(new Field("unindexed", "test1", customType3));
        doc.add(new TextField("unstored","test1", Field.Store.NO));
        writer.addDocument(doc);
      }
      // new fields are in some different segments (we hope)
      for (int i = 0; i < 5*mergeFactor; i++) {
        doc = new Document();
        doc.add(new StringField("keyword2", "test1", Field.Store.YES));
        doc.add(new TextField("text2", "test1", Field.Store.YES));
        doc.add(new Field("unindexed2", "test1", customType3));
        doc.add(new TextField("unstored2","test1", Field.Store.NO));
        writer.addDocument(doc);
      }
      // new termvector fields

      FieldType customType5 = new FieldType(TextField.TYPE_STORED);
      customType5.setStoreTermVectors(true);
      FieldType customType6 = new FieldType(TextField.TYPE_STORED);
      customType6.setStoreTermVectors(true);
      customType6.setStoreTermVectorOffsets(true);
      FieldType customType7 = new FieldType(TextField.TYPE_STORED);
      customType7.setStoreTermVectors(true);
      customType7.setStoreTermVectorPositions(true);
      FieldType customType8 = new FieldType(TextField.TYPE_STORED);
      customType8.setStoreTermVectors(true);
      customType8.setStoreTermVectorOffsets(true);
      customType8.setStoreTermVectorPositions(true);

      for (int i = 0; i < 5*mergeFactor; i++) {
        doc = new Document();
        doc.add(new TextField("tvnot", "tvnot", Field.Store.YES));
        doc.add(new Field("termvector", "termvector", customType5));
        doc.add(new Field("tvoffset", "tvoffset", customType6));
        doc.add(new Field("tvposition", "tvposition", customType7));
        doc.add(new Field("tvpositionoffset", "tvpositionoffset", customType8));
        writer.addDocument(doc);
      }

      writer.close();

      // verify fields again
      reader = DirectoryReader.open(d);
      fieldInfos = MultiFields.getMergedFieldInfos(reader);

      Collection<String> allFieldNames = new HashSet<String>();
      Collection<String> indexedFieldNames = new HashSet<String>();
      Collection<String> notIndexedFieldNames = new HashSet<String>();
      Collection<String> tvFieldNames = new HashSet<String>();

      for(FieldInfo fieldInfo : fieldInfos) {
        final String name = fieldInfo.name;
        allFieldNames.add(name);
        if (fieldInfo.isIndexed()) {
          indexedFieldNames.add(name);
        } else {
          notIndexedFieldNames.add(name);
        }
        if (fieldInfo.hasVectors()) {
          tvFieldNames.add(name);
        }
      }

      assertTrue(allFieldNames.contains("keyword"));
      assertTrue(allFieldNames.contains("text"));
      assertTrue(allFieldNames.contains("unindexed"));
      assertTrue(allFieldNames.contains("unstored"));
      assertTrue(allFieldNames.contains("keyword2"));
      assertTrue(allFieldNames.contains("text2"));
      assertTrue(allFieldNames.contains("unindexed2"));
      assertTrue(allFieldNames.contains("unstored2"));
      assertTrue(allFieldNames.contains("tvnot"));
      assertTrue(allFieldNames.contains("termvector"));
      assertTrue(allFieldNames.contains("tvposition"));
      assertTrue(allFieldNames.contains("tvoffset"));
      assertTrue(allFieldNames.contains("tvpositionoffset"));

      // verify that only indexed fields were returned
      assertEquals(11, indexedFieldNames.size());    // 6 original + the 5 termvector fields
      assertTrue(indexedFieldNames.contains("keyword"));
      assertTrue(indexedFieldNames.contains("text"));
      assertTrue(indexedFieldNames.contains("unstored"));
      assertTrue(indexedFieldNames.contains("keyword2"));
      assertTrue(indexedFieldNames.contains("text2"));
      assertTrue(indexedFieldNames.contains("unstored2"));
      assertTrue(indexedFieldNames.contains("tvnot"));
      assertTrue(indexedFieldNames.contains("termvector"));
      assertTrue(indexedFieldNames.contains("tvposition"));
      assertTrue(indexedFieldNames.contains("tvoffset"));
      assertTrue(indexedFieldNames.contains("tvpositionoffset"));

      // verify that only unindexed fields were returned
      assertEquals(2, notIndexedFieldNames.size());    // the following fields
      assertTrue(notIndexedFieldNames.contains("unindexed"));
      assertTrue(notIndexedFieldNames.contains("unindexed2"));

      // verify index term vector fields
      assertEquals(tvFieldNames.toString(), 4, tvFieldNames.size());    // 4 field has term vector only
      assertTrue(tvFieldNames.contains("termvector"));

      reader.close();
      d.close();
  }

public void testTermVectors() throws Exception {
  Directory d = newDirectory();
  // set up writer
  IndexWriter writer = new IndexWriter(
      d,
      newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).
          setMergePolicy(newLogMergePolicy())
  );
  // want to get some more segments here
  // new termvector fields
  int mergeFactor = ((LogMergePolicy) writer.getConfig().getMergePolicy()).getMergeFactor();
  FieldType customType5 = new FieldType(TextField.TYPE_STORED);
  customType5.setStoreTermVectors(true);
  FieldType customType6 = new FieldType(TextField.TYPE_STORED);
  customType6.setStoreTermVectors(true);
  customType6.setStoreTermVectorOffsets(true);
  FieldType customType7 = new FieldType(TextField.TYPE_STORED);
  customType7.setStoreTermVectors(true);
  customType7.setStoreTermVectorPositions(true);
  FieldType customType8 = new FieldType(TextField.TYPE_STORED);
  customType8.setStoreTermVectors(true);
  customType8.setStoreTermVectorOffsets(true);
  customType8.setStoreTermVectorPositions(true);
  for (int i = 0; i < 5 * mergeFactor; i++) {
    Document doc = new Document();
      doc.add(new TextField("tvnot", "one two two three three three", Field.Store.YES));
      doc.add(new Field("termvector", "one two two three three three", customType5));
      doc.add(new Field("tvoffset", "one two two three three three", customType6));
      doc.add(new Field("tvposition", "one two two three three three", customType7));
      doc.add(new Field("tvpositionoffset", "one two two three three three", customType8));

      writer.addDocument(doc);
  }
  writer.close();
  d.close();
}

void assertTermDocsCount(String msg,
                                   IndexReader reader,
                                   Term term,
                                   int expected)
  throws IOException {
  DocsEnum tdocs = _TestUtil.docs(random(), reader,
                                  term.field(),
                                  new BytesRef(term.text()),
                                  MultiFields.getLiveDocs(reader),
                                  null,
                                  0);
  int count = 0;
  if (tdocs != null) {
    while(tdocs.nextDoc()!= DocIdSetIterator.NO_MORE_DOCS) {
      count++;
    }
  }
  assertEquals(msg + ", count mismatch", expected, count);
}

  public void testBinaryFields() throws IOException {
    Directory dir = newDirectory();
    byte[] bin = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).setMergePolicy(newLogMergePolicy()));

    for (int i = 0; i < 10; i++) {
      addDoc(writer, "document number " + (i + 1));
      addDocumentWithFields(writer);
      addDocumentWithDifferentFields(writer);
      addDocumentWithTermVectorFields(writer);
    }
    writer.close();
    writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).setOpenMode(OpenMode.APPEND).setMergePolicy(newLogMergePolicy()));
    Document doc = new Document();
    doc.add(new StoredField("bin1", bin));
    doc.add(new TextField("junk", "junk text", Field.Store.NO));
    writer.addDocument(doc);
    writer.close();
    DirectoryReader reader = DirectoryReader.open(dir);
    Document doc2 = reader.document(reader.maxDoc() - 1);
    IndexableField[] fields = doc2.getFields("bin1");
    assertNotNull(fields);
    assertEquals(1, fields.length);
    IndexableField b1 = fields[0];
    assertTrue(b1.binaryValue() != null);
    BytesRef bytesRef = b1.binaryValue();
    assertEquals(bin.length, bytesRef.length);
    for (int i = 0; i < bin.length; i++) {
      assertEquals(bin[i], bytesRef.bytes[i + bytesRef.offset]);
    }
    reader.close();
    // force merge

    writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).setOpenMode(OpenMode.APPEND).setMergePolicy(newLogMergePolicy()));
    writer.forceMerge(1);
    writer.close();
    reader = DirectoryReader.open(dir);
    doc2 = reader.document(reader.maxDoc() - 1);
    fields = doc2.getFields("bin1");
    assertNotNull(fields);
    assertEquals(1, fields.length);
    b1 = fields[0];
    assertTrue(b1.binaryValue() != null);
    bytesRef = b1.binaryValue();
    assertEquals(bin.length, bytesRef.length);
    for (int i = 0; i < bin.length; i++) {
      assertEquals(bin[i], bytesRef.bytes[i + bytesRef.offset]);
    }
    reader.close();
    dir.close();
  }

  /* ??? public void testOpenEmptyDirectory() throws IOException{
    String dirName = "test.empty";
    File fileDirName = new File(dirName);
    if (!fileDirName.exists()) {
      fileDirName.mkdir();
    }
    try {
      DirectoryReader.open(fileDirName);
      fail("opening DirectoryReader on empty directory failed to produce FileNotFoundException");
    } catch (FileNotFoundException e) {
      // GOOD
    }
    rmDir(fileDirName);
  }*/

public void testFilesOpenClose() throws IOException {
      // Create initial data set
      File dirFile = _TestUtil.getTempDir("TestIndexReader.testFilesOpenClose");
      Directory dir = newFSDirectory(dirFile);
      IndexWriter writer  = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())));
      addDoc(writer, "test");
      writer.close();
      dir.close();

      // Try to erase the data - this ensures that the writer closed all files
      _TestUtil.rmDir(dirFile);
      dir = newFSDirectory(dirFile);

      // Now create the data set again, just as before
      writer  = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).setOpenMode(OpenMode.CREATE));
      addDoc(writer, "test");
      writer.close();
      dir.close();

      // Now open existing directory and test that reader closes all files
      dir = newFSDirectory(dirFile);
      DirectoryReader reader1 = DirectoryReader.open(dir);
      reader1.close();
      dir.close();

      // The following will fail if reader did not close
      // all files
      _TestUtil.rmDir(dirFile);
  }

  public void testOpenReaderAfterDelete() throws IOException {
    File dirFile = _TestUtil.getTempDir("deletetest");
    Directory dir = newFSDirectory(dirFile);
    try {
      DirectoryReader.open(dir);
      fail("expected FileNotFoundException");
    } catch (FileNotFoundException e) {
      // expected
    }

    dirFile.delete();

    // Make sure we still get a CorruptIndexException (not NPE):
    try {
      DirectoryReader.open(dir);
      fail("expected FileNotFoundException");
    } catch (FileNotFoundException e) {
      // expected
    }

    dir.close();
  }

  static void addDocumentWithFields(IndexWriter writer) throws IOException
  {
      Document doc = new Document();

      FieldType customType3 = new FieldType();
      customType3.setStored(true);
      doc.add(newStringField("keyword", "test1", Field.Store.YES));
      doc.add(newTextField("text", "test1", Field.Store.YES));
      doc.add(newField("unindexed", "test1", customType3));
      doc.add(new TextField("unstored","test1", Field.Store.NO));
      writer.addDocument(doc);
  }

  static void addDocumentWithDifferentFields(IndexWriter writer) throws IOException
  {
    Document doc = new Document();

    FieldType customType3 = new FieldType();
    customType3.setStored(true);
    doc.add(newStringField("keyword2", "test1", Field.Store.YES));
    doc.add(newTextField("text2", "test1", Field.Store.YES));
    doc.add(newField("unindexed2", "test1", customType3));
    doc.add(new TextField("unstored2","test1", Field.Store.NO));
    writer.addDocument(doc);
  }

  static void addDocumentWithTermVectorFields(IndexWriter writer) throws IOException
  {
      Document doc = new Document();
      FieldType customType5 = new FieldType(TextField.TYPE_STORED);
      customType5.setStoreTermVectors(true);
      FieldType customType6 = new FieldType(TextField.TYPE_STORED);
      customType6.setStoreTermVectors(true);
      customType6.setStoreTermVectorOffsets(true);
      FieldType customType7 = new FieldType(TextField.TYPE_STORED);
      customType7.setStoreTermVectors(true);
      customType7.setStoreTermVectorPositions(true);
      FieldType customType8 = new FieldType(TextField.TYPE_STORED);
      customType8.setStoreTermVectors(true);
      customType8.setStoreTermVectorOffsets(true);
      customType8.setStoreTermVectorPositions(true);
      doc.add(newTextField("tvnot", "tvnot", Field.Store.YES));
      doc.add(newField("termvector","termvector",customType5));
      doc.add(newField("tvoffset","tvoffset", customType6));
      doc.add(newField("tvposition","tvposition", customType7));
      doc.add(newField("tvpositionoffset","tvpositionoffset", customType8));

      writer.addDocument(doc);
  }

  static void addDoc(IndexWriter writer, String value) throws IOException {
      Document doc = new Document();
      doc.add(newTextField("content", value, Field.Store.NO));
      writer.addDocument(doc);
  }

  // TODO: maybe this can reuse the logic of test dueling codecs?
  public static void assertIndexEquals(DirectoryReader index1, DirectoryReader index2) throws IOException {
    assertEquals("IndexReaders have different values for numDocs.", index1.numDocs(), index2.numDocs());
    assertEquals("IndexReaders have different values for maxDoc.", index1.maxDoc(), index2.maxDoc());
    assertEquals("Only one IndexReader has deletions.", index1.hasDeletions(), index2.hasDeletions());
    assertEquals("Single segment test differs.", index1.leaves().size() == 1, index2.leaves().size() == 1);

    // check field names
    FieldInfos fieldInfos1 = MultiFields.getMergedFieldInfos(index1);
    FieldInfos fieldInfos2 = MultiFields.getMergedFieldInfos(index2);
    assertEquals("IndexReaders have different numbers of fields.", fieldInfos1.size(), fieldInfos2.size());
    final int numFields = fieldInfos1.size();
    for(int fieldID=0;fieldID<numFields;fieldID++) {
      final FieldInfo fieldInfo1 = fieldInfos1.fieldInfo(fieldID);
      final FieldInfo fieldInfo2 = fieldInfos2.fieldInfo(fieldID);
      assertEquals("Different field names.", fieldInfo1.name, fieldInfo2.name);
    }

    // check norms
    for(FieldInfo fieldInfo : fieldInfos1) {
      String curField = fieldInfo.name;
      NumericDocValues norms1 = MultiDocValues.getNormValues(index1, curField);
      NumericDocValues norms2 = MultiDocValues.getNormValues(index2, curField);
      if (norms1 != null && norms2 != null) {
        // todo: generalize this (like TestDuelingCodecs assert)
        for (int i = 0; i < index1.maxDoc(); i++) {
          assertEquals("Norm different for doc " + i + " and field '" + curField + "'.", norms1.get(i), norms2.get(i));
        }
      } else {
        assertNull(norms1);
        assertNull(norms2);
      }
    }

    // check deletions
    final Bits liveDocs1 = MultiFields.getLiveDocs(index1);
    final Bits liveDocs2 = MultiFields.getLiveDocs(index2);
    for (int i = 0; i < index1.maxDoc(); i++) {
      assertEquals("Doc " + i + " only deleted in one index.",
                   liveDocs1 == null || !liveDocs1.get(i),
                   liveDocs2 == null || !liveDocs2.get(i));
    }

    // check stored fields
    for (int i = 0; i < index1.maxDoc(); i++) {
      if (liveDocs1 == null || liveDocs1.get(i)) {
        Document doc1 = index1.document(i);
        Document doc2 = index2.document(i);
        List<IndexableField> field1 = doc1.getFields();
        List<IndexableField> field2 = doc2.getFields();
        assertEquals("Different numbers of fields for doc " + i + ".", field1.size(), field2.size());
        Iterator<IndexableField> itField1 = field1.iterator();
        Iterator<IndexableField> itField2 = field2.iterator();
        while (itField1.hasNext()) {
          Field curField1 = (Field) itField1.next();
          Field curField2 = (Field) itField2.next();
          assertEquals("Different fields names for doc " + i + ".", curField1.name(), curField2.name());
          assertEquals("Different field values for doc " + i + ".", curField1.stringValue(), curField2.stringValue());
        }
      }
    }

    // check dictionary and posting lists
    Fields fields1 = MultiFields.getFields(index1);
    Fields fields2 = MultiFields.getFields(index2);
    Iterator<String> fenum2 = fields2.iterator();
    Bits liveDocs = MultiFields.getLiveDocs(index1);
    for (String field1 : fields1) {
      assertEquals("Different fields", field1, fenum2.next());
      Terms terms1 = fields1.terms(field1);
      if (terms1 == null) {
        assertNull(fields2.terms(field1));
        continue;
      }
      TermsEnum enum1 = terms1.iterator(null);

      Terms terms2 = fields2.terms(field1);
      assertNotNull(terms2);
      TermsEnum enum2 = terms2.iterator(null);

      while(enum1.next() != null) {
        assertEquals("Different terms", enum1.term(), enum2.next());
        DocsAndPositionsEnum tp1 = enum1.docsAndPositions(liveDocs, null);
        DocsAndPositionsEnum tp2 = enum2.docsAndPositions(liveDocs, null);

        while(tp1.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
          assertTrue(tp2.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
          assertEquals("Different doc id in postinglist of term " + enum1.term() + ".", tp1.docID(), tp2.docID());
          assertEquals("Different term frequence in postinglist of term " + enum1.term() + ".", tp1.freq(), tp2.freq());
          for (int i = 0; i < tp1.freq(); i++) {
            assertEquals("Different positions in postinglist of term " + enum1.term() + ".", tp1.nextPosition(), tp2.nextPosition());
          }
        }
      }
    }
    assertFalse(fenum2.hasNext());
  }

  public void testGetIndexCommit() throws IOException {

    Directory d = newDirectory();

    // set up writer
    IndexWriter writer = new IndexWriter(
        d,
        newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).
            setMaxBufferedDocs(2).
            setMergePolicy(newLogMergePolicy(10))
    );
    for(int i=0;i<27;i++)
      addDocumentWithFields(writer);
    writer.close();

    SegmentInfos sis = new SegmentInfos();
    sis.read(d);
    DirectoryReader r = DirectoryReader.open(d);
    IndexCommit c = r.getIndexCommit();

    assertEquals(sis.getSegmentsFileName(), c.getSegmentsFileName());

    assertTrue(c.equals(r.getIndexCommit()));

    // Change the index
    writer = new IndexWriter(
        d,
        newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).
            setOpenMode(OpenMode.APPEND).
            setMaxBufferedDocs(2).
            setMergePolicy(newLogMergePolicy(10))
    );
    for(int i=0;i<7;i++)
      addDocumentWithFields(writer);
    writer.close();

    DirectoryReader r2 = DirectoryReader.openIfChanged(r);
    assertNotNull(r2);
    assertFalse(c.equals(r2.getIndexCommit()));
    assertFalse(r2.getIndexCommit().getSegmentCount() == 1);
    r2.close();

    writer = new IndexWriter(d, newIndexWriterConfig(TEST_VERSION_CURRENT,
      new MockAnalyzer(random()))
      .setOpenMode(OpenMode.APPEND));
    writer.forceMerge(1);
    writer.close();

    r2 = DirectoryReader.openIfChanged(r);
    assertNotNull(r2);
    assertNull(DirectoryReader.openIfChanged(r2));
    assertEquals(1, r2.getIndexCommit().getSegmentCount());

    r.close();
    r2.close();
    d.close();
  }

  static Document createDocument(String id) {
    Document doc = new Document();
    FieldType customType = new FieldType(TextField.TYPE_STORED);
    customType.setTokenized(false);
    customType.setOmitNorms(true);

    doc.add(newField("id", id, customType));
    return doc;
  }

  // LUCENE-1468 -- make sure on attempting to open an
  // DirectoryReader on a non-existent directory, you get a
  // good exception
  public void testNoDir() throws Throwable {
    File tempDir = _TestUtil.getTempDir("doesnotexist");
    _TestUtil.rmDir(tempDir);
    Directory dir = newFSDirectory(tempDir);
    try {
      DirectoryReader.open(dir);
      fail("did not hit expected exception");
    } catch (NoSuchDirectoryException nsde) {
      // expected
    }
    dir.close();
  }

  // LUCENE-1509
  public void testNoDupCommitFileNames() throws Throwable {

    Directory dir = newDirectory();

    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
        TEST_VERSION_CURRENT, new MockAnalyzer(random()))
        .setMaxBufferedDocs(2));
    writer.addDocument(createDocument("a"));
    writer.addDocument(createDocument("a"));
    writer.addDocument(createDocument("a"));
    writer.close();

    Collection<IndexCommit> commits = DirectoryReader.listCommits(dir);
    for (final IndexCommit commit : commits) {
      Collection<String> files = commit.getFileNames();
      HashSet<String> seen = new HashSet<String>();
      for (final String fileName : files) {
        assertTrue("file " + fileName + " was duplicated", !seen.contains(fileName));
        seen.add(fileName);
      }
    }

    dir.close();
  }

  // LUCENE-1579: Ensure that on a reopened reader, that any
  // shared segments reuse the doc values arrays in
  // FieldCache
  public void testFieldCacheReuseAfterReopen() throws Exception {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(
        dir,
        newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).
            setMergePolicy(newLogMergePolicy(10))
    );
    Document doc = new Document();
    doc.add(newStringField("number", "17", Field.Store.NO));
    writer.addDocument(doc);
    writer.commit();

    // Open reader1
    DirectoryReader r = DirectoryReader.open(dir);
    AtomicReader r1 = getOnlySegmentReader(r);
    final FieldCache.Ints ints = FieldCache.DEFAULT.getInts(r1, "number", false);
    assertEquals(17, ints.get(0));

    // Add new segment
    writer.addDocument(doc);
    writer.commit();

    // Reopen reader1 --> reader2
    DirectoryReader r2 = DirectoryReader.openIfChanged(r);
    assertNotNull(r2);
    r.close();
    AtomicReader sub0 = r2.leaves().get(0).reader();
    final FieldCache.Ints ints2 = FieldCache.DEFAULT.getInts(sub0, "number", false);
    r2.close();
    assertTrue(ints == ints2);

    writer.close();
    dir.close();
  }

  // LUCENE-1586: getUniqueTermCount
  public void testUniqueTermCount() throws Exception {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())));
    Document doc = new Document();
    doc.add(newTextField("field", "a b c d e f g h i j k l m n o p q r s t u v w x y z", Field.Store.NO));
    doc.add(newTextField("number", "0 1 2 3 4 5 6 7 8 9", Field.Store.NO));
    writer.addDocument(doc);
    writer.addDocument(doc);
    writer.commit();

    DirectoryReader r = DirectoryReader.open(dir);
    AtomicReader r1 = getOnlySegmentReader(r);
    assertEquals(36, r1.fields().getUniqueTermCount());
    writer.addDocument(doc);
    writer.commit();
    DirectoryReader r2 = DirectoryReader.openIfChanged(r);
    assertNotNull(r2);
    r.close();

    for(AtomicReaderContext s : r2.leaves()) {
      assertEquals(36, s.reader().fields().getUniqueTermCount());
    }
    r2.close();
    writer.close();
    dir.close();
  }

  // LUCENE-1609: don't load terms index
  public void testNoTermsIndex() throws Throwable {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).setCodec(_TestUtil.alwaysPostingsFormat(new Lucene41PostingsFormat())));
    Document doc = new Document();
    doc.add(newTextField("field", "a b c d e f g h i j k l m n o p q r s t u v w x y z", Field.Store.NO));
    doc.add(newTextField("number", "0 1 2 3 4 5 6 7 8 9", Field.Store.NO));
    writer.addDocument(doc);
    writer.addDocument(doc);
    writer.close();

    DirectoryReader r = DirectoryReader.open(dir, -1);
    try {
      r.docFreq(new Term("field", "f"));
      fail("did not hit expected exception");
    } catch (IllegalStateException ise) {
      // expected
    }

    assertEquals(-1, ((SegmentReader) r.leaves().get(0).reader()).getTermInfosIndexDivisor());
    writer = new IndexWriter(
        dir,
        newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).
            setCodec(_TestUtil.alwaysPostingsFormat(new Lucene41PostingsFormat())).
            setMergePolicy(newLogMergePolicy(10))
    );
    writer.addDocument(doc);
    writer.close();

    // LUCENE-1718: ensure re-open carries over no terms index:
    DirectoryReader r2 = DirectoryReader.openIfChanged(r);
    assertNotNull(r2);
    assertNull(DirectoryReader.openIfChanged(r2));
    r.close();
    List<AtomicReaderContext> leaves = r2.leaves();
    assertEquals(2, leaves.size());
    for(AtomicReaderContext ctx : leaves) {
      try {
        ctx.reader().docFreq(new Term("field", "f"));
        fail("did not hit expected exception");
      } catch (IllegalStateException ise) {
        // expected
      }
    }
    r2.close();
    dir.close();
  }

  // LUCENE-2046
  public void testPrepareCommitIsCurrent() throws Throwable {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
        TEST_VERSION_CURRENT, new MockAnalyzer(random())));
    writer.commit();
    Document doc = new Document();
    writer.addDocument(doc);
    DirectoryReader r = DirectoryReader.open(dir);
    assertTrue(r.isCurrent());
    writer.addDocument(doc);
    writer.prepareCommit();
    assertTrue(r.isCurrent());
    DirectoryReader r2 = DirectoryReader.openIfChanged(r);
    assertNull(r2);
    writer.commit();
    assertFalse(r.isCurrent());
    writer.close();
    r.close();
    dir.close();
  }

  // LUCENE-2753
  public void testListCommits() throws Exception {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, null)
        .setIndexDeletionPolicy(new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy())));
    SnapshotDeletionPolicy sdp = (SnapshotDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();
    writer.addDocument(new Document());
    writer.commit();
    sdp.snapshot();
    writer.addDocument(new Document());
    writer.commit();
    sdp.snapshot();
    writer.addDocument(new Document());
    writer.commit();
    sdp.snapshot();
    writer.close();
    long currentGen = 0;
    for (IndexCommit ic : DirectoryReader.listCommits(dir)) {
      assertTrue("currentGen=" + currentGen + " commitGen=" + ic.getGeneration(), currentGen < ic.getGeneration());
      currentGen = ic.getGeneration();
    }
    dir.close();
  }

  // Make sure totalTermFreq works correctly in the terms
  // dict cache
  public void testTotalTermFreqCached() throws Exception {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())));
    Document d = new Document();
    d.add(newTextField("f", "a a b", Field.Store.NO));
    writer.addDocument(d);
    DirectoryReader r = writer.getReader();
    writer.close();
    try {
      // Make sure codec impls totalTermFreq (eg PreFlex doesn't)
      Assume.assumeTrue(r.totalTermFreq(new Term("f", new BytesRef("b"))) != -1);
      assertEquals(1, r.totalTermFreq(new Term("f", new BytesRef("b"))));
      assertEquals(2, r.totalTermFreq(new Term("f", new BytesRef("a"))));
      assertEquals(1, r.totalTermFreq(new Term("f", new BytesRef("b"))));
    } finally {
      r.close();
      dir.close();
    }
  }

  public void testGetSumDocFreq() throws Exception {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())));
    Document d = new Document();
    d.add(newTextField("f", "a", Field.Store.NO));
    writer.addDocument(d);
    d = new Document();
    d.add(newTextField("f", "b", Field.Store.NO));
    writer.addDocument(d);
    DirectoryReader r = writer.getReader();
    writer.close();
    try {
      // Make sure codec impls getSumDocFreq (eg PreFlex doesn't)
      Assume.assumeTrue(r.getSumDocFreq("f") != -1);
      assertEquals(2, r.getSumDocFreq("f"));
    } finally {
      r.close();
      dir.close();
    }
  }

  public void testGetDocCount() throws Exception {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())));
    Document d = new Document();
    d.add(newTextField("f", "a", Field.Store.NO));
    writer.addDocument(d);
    d = new Document();
    d.add(newTextField("f", "a", Field.Store.NO));
    writer.addDocument(d);
    DirectoryReader r = writer.getReader();
    writer.close();
    try {
      // Make sure codec impls getSumDocFreq (eg PreFlex doesn't)
      Assume.assumeTrue(r.getDocCount("f") != -1);
      assertEquals(2, r.getDocCount("f"));
    } finally {
      r.close();
      dir.close();
    }
  }

  public void testGetSumTotalTermFreq() throws Exception {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())));
    Document d = new Document();
    d.add(newTextField("f", "a b b", Field.Store.NO));
    writer.addDocument(d);
    d = new Document();
    d.add(newTextField("f", "a a b", Field.Store.NO));
    writer.addDocument(d);
    DirectoryReader r = writer.getReader();
    writer.close();
    try {
      // Make sure codec impls getSumDocFreq (eg PreFlex doesn't)
      Assume.assumeTrue(r.getSumTotalTermFreq("f") != -1);
      assertEquals(6, r.getSumTotalTermFreq("f"));
    } finally {
      r.close();
      dir.close();
    }
  }

  // LUCENE-2474
  public void testReaderFinishedListener() throws Exception {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).setMergePolicy(newLogMergePolicy()));
    ((LogMergePolicy) writer.getConfig().getMergePolicy()).setMergeFactor(3);
    writer.addDocument(new Document());
    writer.commit();
    writer.addDocument(new Document());
    writer.commit();
    final DirectoryReader reader = writer.getReader();
    final int[] closeCount = new int[1];
    final IndexReader.ReaderClosedListener listener = new IndexReader.ReaderClosedListener() {
      @Override
      public void onClose(IndexReader reader) {
        closeCount[0]++;
      }
    };

    reader.addReaderClosedListener(listener);

    reader.close();

    // Close the top reader, its the only one that should be closed
    assertEquals(1, closeCount[0]);
    writer.close();

    DirectoryReader reader2 = DirectoryReader.open(dir);
    reader2.addReaderClosedListener(listener);

    closeCount[0] = 0;
    reader2.close();
    assertEquals(1, closeCount[0]);
    dir.close();
  }

  public void testOOBDocID() throws Exception {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())));
    writer.addDocument(new Document());
    DirectoryReader r = writer.getReader();
    writer.close();
    r.document(0);
    try {
      r.document(1);
      fail("did not hit exception");
    } catch (IllegalArgumentException iae) {
      // expected
    }
    r.close();
    dir.close();
  }

  public void testTryIncRef() throws IOException {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())));
    writer.addDocument(new Document());
    writer.commit();
    DirectoryReader r = DirectoryReader.open(dir);
    assertTrue(r.tryIncRef());
    r.decRef();
    r.close();
    assertFalse(r.tryIncRef());
    writer.close();
    dir.close();
  }

  public void testStressTryIncRef() throws IOException, InterruptedException {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())));
    writer.addDocument(new Document());
    writer.commit();
    DirectoryReader r = DirectoryReader.open(dir);
    int numThreads = atLeast(2);

    IncThread[] threads = new IncThread[numThreads];
    for (int i = 0; i < threads.length; i++) {
      threads[i] = new IncThread(r, random());
      threads[i].start();
    }
    Thread.sleep(100);

    assertTrue(r.tryIncRef());
    r.decRef();
    r.close();

    for (int i = 0; i < threads.length; i++) {
      threads[i].join();
      assertNull(threads[i].failed);
    }
    assertFalse(r.tryIncRef());
    writer.close();
    dir.close();
  }

  static class IncThread extends Thread {
    final IndexReader toInc;
    final Random random;
    Throwable failed;

    IncThread(IndexReader toInc, Random random) {
      this.toInc = toInc;
      this.random = random;
    }

    @Override
    public void run() {
      try {
        while (toInc.tryIncRef()) {
          assertFalse(toInc.hasDeletions());
          toInc.decRef();
        }
        assertFalse(toInc.tryIncRef());
      } catch (Throwable e) {
        failed = e;
      }
    }
  }

  public void testLoadCertainFields() throws Exception {
    Directory dir = newDirectory();
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    doc.add(newStringField("field1", "foobar", Field.Store.YES));
    doc.add(newStringField("field2", "foobaz", Field.Store.YES));
    writer.addDocument(doc);
    DirectoryReader r = writer.getReader();
    writer.close();
    Set<String> fieldsToLoad = new HashSet<String>();
    assertEquals(0, r.document(0, fieldsToLoad).getFields().size());
    fieldsToLoad.add("field1");
    Document doc2 = r.document(0, fieldsToLoad);
    assertEquals(1, doc2.getFields().size());
    assertEquals("foobar", doc2.get("field1"));
    r.close();
    dir.close();
  }

  /**
   * @deprecated just to ensure IndexReader static methods work
   */
  @Deprecated
  public void testBackwards() throws Exception {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random())).setCodec(_TestUtil.alwaysPostingsFormat(new Lucene41PostingsFormat())));
    Document doc = new Document();
    doc.add(newTextField("field", "a b c d e f g h i j k l m n o p q r s t u v w x y z", Field.Store.NO));
    doc.add(newTextField("number", "0 1 2 3 4 5 6 7 8 9", Field.Store.NO));
    writer.addDocument(doc);

    // open(IndexWriter, boolean)
    DirectoryReader r = IndexReader.open(writer, true);
    assertEquals(1, r.docFreq(new Term("field", "f")));
    r.close();
    writer.addDocument(doc);
    writer.close();

    // open(Directory)
    r = IndexReader.open(dir);
    assertEquals(2, r.docFreq(new Term("field", "f")));
    r.close();

    // open(IndexCommit)
    List<IndexCommit> commits = DirectoryReader.listCommits(dir);
    assertEquals(1, commits.size());
    r = IndexReader.open(commits.get(0));
    assertEquals(2, r.docFreq(new Term("field", "f")));
    r.close();

    // open(Directory, int)
    r = IndexReader.open(dir, -1);
    try {
      r.docFreq(new Term("field", "f"));
      fail("did not hit expected exception");
    } catch (IllegalStateException ise) {
      // expected
    }
    assertEquals(-1, ((SegmentReader) r.leaves().get(0).reader()).getTermInfosIndexDivisor());
    r.close();

    // open(IndexCommit, int)
    r = IndexReader.open(commits.get(0), -1);
    try {
      r.docFreq(new Term("field", "f"));
      fail("did not hit expected exception");
    } catch (IllegalStateException ise) {
      // expected
    }
    assertEquals(-1, ((SegmentReader) r.leaves().get(0).reader()).getTermInfosIndexDivisor());
    r.close();
    dir.close();
  }

  public void testIndexExistsOnNonExistentDirectory() throws Exception {
    File tempDir = _TestUtil.getTempDir("testIndexExistsOnNonExistentDirectory");
    tempDir.delete();
    Directory dir = newFSDirectory(tempDir);
    System.out.println("dir=" + dir);
    assertFalse(DirectoryReader.indexExists(dir));
    dir.close();
  }
}

redpajama_set_name: RedPajamaGithub


text:
#include <aws/medialive/model/ArchiveOutputSettings.h>
#include <aws/core/utils/json/JsonSerializer.h>

#include <utility>

using namespace Aws::Utils::Json;
using namespace Aws::Utils;

namespace Aws
{
namespace MediaLive
{
namespace Model
{

ArchiveOutputSettings::ArchiveOutputSettings() :
    m_containerSettingsHasBeenSet(false),
    m_extensionHasBeenSet(false),
    m_nameModifierHasBeenSet(false)
{
}

ArchiveOutputSettings::ArchiveOutputSettings(JsonView jsonValue) :
    m_containerSettingsHasBeenSet(false),
    m_extensionHasBeenSet(false),
    m_nameModifierHasBeenSet(false)
{
  *this = jsonValue;
}

ArchiveOutputSettings& ArchiveOutputSettings::operator =(JsonView jsonValue)
{
  if(jsonValue.ValueExists("containerSettings"))
  {
    m_containerSettings = jsonValue.GetObject("containerSettings");

    m_containerSettingsHasBeenSet = true;
  }

  if(jsonValue.ValueExists("extension"))
  {
    m_extension = jsonValue.GetString("extension");

    m_extensionHasBeenSet = true;
  }

  if(jsonValue.ValueExists("nameModifier"))
  {
    m_nameModifier = jsonValue.GetString("nameModifier");

    m_nameModifierHasBeenSet = true;
  }

  return *this;
}

JsonValue ArchiveOutputSettings::Jsonize() const
{
  JsonValue payload;

  if(m_containerSettingsHasBeenSet)
  {
   payload.WithObject("containerSettings", m_containerSettings.Jsonize());

  }

  if(m_extensionHasBeenSet)
  {
   payload.WithString("extension", m_extension);

  }

  if(m_nameModifierHasBeenSet)
  {
   payload.WithString("nameModifier", m_nameModifier);

  }

  return payload;
}

} // namespace Model
} // namespace MediaLive
} // namespace Aws


text:
/*
 * angular-marked
 * (c) 2014 J. Harshbarger
 * Licensed MIT
 */

/* jshint undef: true, unused: true */
/* global angular:true */
/* global marked:true */

(function () {
  'use strict';

  /**
   * @ngdoc overview
   * @name index
   *
   * @description
   * AngularJS Markdown using [marked](https://github.com/chjj/marked).
   *
   * ## Why?
   *
   * I wanted to use [marked](https://github.com/chjj/marked) instead of [showdown](https://github.com/coreyti/showdown) as used in [angular-markdown-directive](https://github.com/btford/angular-markdown-directive) as well as expose the option to globally set defaults.
   *
   * ## How?
   *
   * - {@link hc.marked.directive:marked As a directive}
   * - {@link hc.marked.service:marked As a service}
   * - {@link hc.marked.service:markedProvider Set default options}
   *
   * @example

      Convert markdown to html at run time.  For example:

      <example module="hc.marked">
        <file name=".html">
          <form ng-controller="MainController">
            Markdown:

            <textarea ng-model="my_markdown" cols="60" rows="5" class="span8" />

            Output:

          </form>
        </file>
        <file  name=".js">
          function MainController($scope) {
            $scope.my_markdown = "*This* **is** [markdown](https://daringfireball.net/projects/markdown/)";
          }
        </file>
      </example>

    *
    */

    /**
     * @ngdoc overview
     * @name hc.marked
     * @description # angular-marked (core module)
       # Installation
      First include angular-marked.js in your HTML:

      ```js
        <script src="angular-marked.js">
      ```

      Then load the module in your application by adding it as a dependency:

      ```js
      angular.module('yourApp', ['hc.marked']);
      ```

      With that you're ready to get started!
     */

  if (typeof module !== 'undefined' && typeof exports === 'object') {
    module.exports = 'hc.marked';
  };

  angular.module('hc.marked', [])

    /**
    * @ngdoc service
    * @name hc.marked.service:marked
    * @requires $window
    * @description
    * A reference to the [marked](https://github.com/chjj/marked) parser.
    *
    * @example
    <example module="hc.marked">
      <file name=".html">

          html: {{html}}

      </file>
      <file  name=".js">
        function MainController($scope, marked) {
          $scope.html = marked('#TEST');
        }
      </file>
    </example>
   **/

   /**
   * @ngdoc service
   * @name hc.marked.service:markedProvider
   * @description
   * Use `markedProvider` to change the default behavior of the {@link hc.marked.service:marked marked} service.
   *
   * @example

    ## Example using [google-code-prettify syntax highlighter](https://code.google.com/p/google-code-prettify/) (must include google-code-prettify.js script).  Also works with [highlight.js Javascript syntax highlighter](http://highlightjs.org/).

    <example module="myApp">
    <file name=".js">
    angular.module('myApp', ['hc.marked'])
      .config(['markedProvider', function(markedProvider) {
        markedProvider.setOptions({
          gfm: true,
          tables: true,
          highlight: function (code) {
            return prettyPrintOne(code);
          }
        });
      }]);
    </file>
    <file name=".html">
      <marked>
      ```js
      angular.module('myApp', ['hc.marked'])
        .config(['markedProvider', function(markedProvider) {
          markedProvider.setOptions({
            gfm: true,
            tables: true,
            highlight: function (code) {
              return prettyPrintOne(code);
            }
          });
        }]);
      ```
      </marked>
    </file>
    </example>
  **/

  .provider('marked', function () {

    var self = this;

    /**
     * @ngdoc method
     * @name markedProvider#setOptions
     * @methodOf hc.marked.service:markedProvider
     *
     * @param {object} opts Default options for [marked](https://github.com/chjj/marked#options-1).
     */

    self.setRenderer = function (opts) {
      this.renderer = opts;
    };

    self.setOptions = function(opts) {  // Store options for later
      this.defaults = opts;
    };

    self.$get = ['$window', '$log', function ($window, $log) {

      var m =  (function() {

        if (typeof module !== 'undefined' && typeof exports === 'object') {
          return require('marked');
        } else {
          return $window.marked || marked;
        }

      })();

      if (angular.isUndefined(m)) {
        $log.error('angular-marked Error: marked not loaded.  See installation instructions.');
        return;
      }

      // override rendered markdown html
      // with custom definitions if defined
      if (self.renderer) {
        var r = new m.Renderer();
        var o = Object.keys(self.renderer),
            l = o.length;

        while (l--) {
          r[o[l]] = self.renderer[o[l]];
        }

        // add the new renderer to the options if need be
        self.defaults = self.defaults ? self.defaults.renderer = r : self.defaults = {renderer: r };

      }

      m.setOptions(self.defaults);

      return m;
    }];

  })

  // TODO: filter tests */
  //app.filter('marked', ['marked', function(marked) {
  //  return marked;
  //}]);

  /**
   * @ngdoc directive
   * @name hc.marked.directive:marked
   * @restrict AE
   * @element any
   *
   * @description
   * Compiles source test into HTML.
   *
   * @param {expression} marked The source text to be compiled.  If blank uses content as the source.
   * @param {expression=} opts Hash of options that override defaults.
   *
   * @example

     ## A simple block of text

      <example module="hc.marked">
        <file name="exampleA.html">
         * <marked>
         * ### Markdown directive
         *
         * *It works!*
         *
         * *This* **is** [markdown](https://daringfireball.net/projects/markdown/) in the view.
         * </marked>
        </file>
      </example>

     ## Bind to a scope variable

      <example module="hc.marked">
        <file name="exampleB.html">
          <form ng-controller="MainController">
            Markdown:

            <textarea ng-model="my_markdown" class="span8" cols="60" rows="5"></textarea>

            Output:

          </form>
        </file>
        <file  name="exampleB.js">
          * function MainController($scope) {
          *   $scope.my_markdown = '*This* **is** [markdown](https://daringfireball.net/projects/markdown/)';
          *   $scope.my_markdown += ' in a scope variable';
          * }
        </file>
      </example>

      ## Include a markdown file:

       <example module="hc.marked">
         <file name="exampleC.html">

         </file>
         * <file name="include.html">
         * *This* **is** [markdown](https://daringfireball.net/projects/markdown/) in a include file.
         * </file>
       </example>
   */

  .directive('marked', ['marked', function (marked) {
    return {
      restrict: 'AE',
      replace: true,
      scope: {
        opts: '=',
        marked: '='
      },
      link: function (scope, element, attrs) {
        set(scope.marked || element.text() || '');

        function set(val) {
          element.html(marked(val || '', scope.opts || null));
        }

        if (attrs.marked) {
          scope.$watch('marked', set);
        }

      }
    };
  }]);

}());


text:
build
=====
Build a configured project.

Variables
---------
  - :code:`buildDir` (**required**) - The build directory to use.
  - :code:`buildArgs` - Extra arguments to pass to the build tool; the
    arguments will be passed without modification.
  - :code:`buildPrefix` - Something to prefix to the build command.  This
    isn't needed in most cases, but some tools (:code:`scan-build`) adjust the
    environment during a build so they can run.
  - :code:`buildTargets` - An array of targets that should be built during
    this step.  If not specified, the default target will be run.
  - :code:`countWarnings` - Include warning information in the build
    artifacts.  This will include both the raw warnings from the compiler and
    a sorted count of warnings.  The count assumes the flag to emit the
    warning was included in output (this is true for both :code:`gcc` and
    :code:`clang`).

Artifacts
---------
If :code:`countWarnings` is :code:`true`, warning information is captured.
The files will be archived even if no warnings are emitted.


text:
<?php

namespace Magento\Captcha\Test\Unit\Model\Customer\Plugin;

class AjaxLoginTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $sessionManagerMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $captchaHelperMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $jsonFactoryMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $captchaMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $resultJsonMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $requestMock;

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $loginControllerMock;

    /**
     * @var array
     */
    protected $formIds;

    /**
     * @var \Magento\Captcha\Model\Customer\Plugin\AjaxLogin
     */
    protected $model;

    protected function setUp()
    {
        $this->sessionManagerMock = $this->getMock('Magento\Checkout\Model\Session', ['setUsername'], [], '', false);
        $this->captchaHelperMock = $this->getMock('Magento\Captcha\Helper\Data', [], [], '', false);
        $this->captchaMock = $this->getMock('Magento\Captcha\Model\DefaultModel', [], [], '', false);
        $this->jsonFactoryMock = $this->getMock(
            'Magento\Framework\Controller\Result\JsonFactory',
            ['create'],
            [],
            '',
            false
        );
        $this->resultJsonMock = $this->getMock('Magento\Framework\Controller\Result\Json', [], [], '', false);
        $this->requestMock = $this->getMock('Magento\Framework\App\Request\Http', [], [], '', false);
        $this->loginControllerMock = $this->getMock('Magento\Customer\Controller\Ajax\Login', [], [], '', false);

        $this->loginControllerMock->expects($this->any())->method('getRequest')
            ->will($this->returnValue($this->requestMock));
        $this->captchaHelperMock->expects($this->once())->method('getCaptcha')
            ->with('user_login')->will($this->returnValue($this->captchaMock));
        $this->formIds = ['user_login'];

        $this->model = new \Magento\Captcha\Model\Customer\Plugin\AjaxLogin(
            $this->captchaHelperMock,
            $this->sessionManagerMock,
            $this->jsonFactoryMock,
            $this->formIds
        );
    }

    public function testAroundExecute()
    {
        $username = 'name';
        $captchaString = 'string';
        $requestContent = json_encode([
            'username' => $username,
            'captcha_string' => $captchaString,
            'captcha_form_id' => $this->formIds[0]
        ]);

        $this->requestMock->expects($this->once())->method('getContent')->will($this->returnValue($requestContent));
        $this->captchaMock->expects($this->once())->method('isRequired')->with($username)
            ->will($this->returnValue(true));
        $this->captchaMock->expects($this->once())->method('logAttempt')->with($username);
        $this->captchaMock->expects($this->once())->method('isCorrect')->with($captchaString)
            ->will($this->returnValue(true));

        $closure = function () {
            return 'result';
        };
        $this->assertEquals('result', $this->model->aroundExecute($this->loginControllerMock, $closure));
    }

    public function testAroundExecuteIncorrectCaptcha()
    {
        $username = 'name';
        $captchaString = 'string';
        $requestContent = json_encode([
            'username' => $username,
            'captcha_string' => $captchaString,
            'captcha_form_id' => $this->formIds[0]
        ]);

        $this->requestMock->expects($this->once())->method('getContent')->will($this->returnValue($requestContent));
        $this->captchaMock->expects($this->once())->method('isRequired')->with($username)
            ->will($this->returnValue(true));
        $this->captchaMock->expects($this->once())->method('logAttempt')->with($username);
        $this->captchaMock->expects($this->once())->method('isCorrect')
            ->with($captchaString)->will($this->returnValue(false));

        $this->sessionManagerMock->expects($this->once())->method('setUsername')->with($username);
        $this->jsonFactoryMock->expects($this->once())->method('create')
            ->will($this->returnValue($this->resultJsonMock));

        $this->resultJsonMock->expects($this->once())->method('setData')
            ->with(['errors' => true, 'message' => __('Incorrect CAPTCHA')])->will($this->returnValue('response'));

        $closure = function () {
        };
        $this->assertEquals('response', $this->model->aroundExecute($this->loginControllerMock, $closure));
    }

    /**
     * @dataProvider aroundExecuteCaptchaIsNotRequired
     * @param string $username
     * @param array $requestContent
     */
    public function testAroundExecuteCaptchaIsNotRequired($username, $requestContent)
    {
        $this->requestMock->expects($this->once())->method('getContent')->will($this->returnValue($requestContent));

        $this->captchaMock->expects($this->once())->method('isRequired')->with($username)
            ->will($this->returnValue(false));
        $this->captchaMock->expects($this->never())->method('logAttempt')->with($username);
        $this->captchaMock->expects($this->never())->method('isCorrect');

        $closure = function () {
            return 'result';
        };
        $this->assertEquals('result', $this->model->aroundExecute($this->loginControllerMock, $closure));
    }

    /**
     * @return array
     */
    public function aroundExecuteCaptchaIsNotRequired()
    {
        return [
            [
                'username' => 'name',
                'requestContent' => json_encode(['username' => 'name', 'captcha_string' => 'string']),
            ],
            [
                'username' => null,
                'requestContent' => json_encode(['captcha_string' => 'string']),
            ],
        ];
    }
}


text:
/*! \file version.h
 *  \brief Cusp version
 */

#pragma once

#include <cusp/detail/config.h>

//  This is the only cusp header that is guaranteed to
//  change with every cusp release.
//
//  CUSP_VERSION % 100 is the sub-minor version
//  CUSP_VERSION / 100 % 1000 is the minor version
//  CUSP_VERSION / 100000 is the major version

#define CUSP_VERSION 600
#define CUSP_MAJOR_VERSION     (CUSP_VERSION / 100000)
#define CUSP_MINOR_VERSION     (CUSP_VERSION / 100 % 1000)
#define CUSP_SUBMINOR_VERSION  (CUSP_VERSION % 100)


text:
void fam_bypass(mongoc_collection_t *collection)
{
   mongoc_find_and_modify_opts_t *opts;
   bson_t reply;
   bson_t *update;
   bson_error_t error;
   bson_t query = BSON_INITIALIZER;
   bool success;

   /* Find Zlatan Ibrahimovic, the striker */
   BSON_APPEND_UTF8 (&query, "firstname", "Zlatan");
   BSON_APPEND_UTF8 (&query, "lastname", "Ibrahimovic");
   BSON_APPEND_UTF8 (&query, "profession", "Football player");

   /* Bump his age */
   update = BCON_NEW ("$inc", "{",
      "age", BCON_INT32 (1),
   "}");

   opts = mongoc_find_and_modify_opts_new ();
   mongoc_find_and_modify_opts_set_update (opts, update);
   /* He can still play, even though he is pretty old. */
   mongoc_find_and_modify_opts_set_bypass_document_validation (opts, true);

   success = mongoc_collection_find_and_modify_with_opts (collection, &query, opts, &reply, &error);

   if (success) {
      char *str;

      str = bson_as_json (&reply, NULL);
      printf ("%s\n", str);
      bson_free (str);
   } else {
      fprintf(stderr, "Got error: \"%s\" on line %d\n", error.message, __LINE__);
   }

   bson_destroy (&reply);
   bson_destroy (update);
   bson_destroy (&query);
   mongoc_find_and_modify_opts_destroy (opts);
}


text:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="0; url=https://www.fullstackpython.com/flask-extensions-plug-ins-related-libraries.html">
</head>
<body>
</body>
</html>


text:
import type babylon from 'babylon'

import * as React from 'react'
import * as t from 'babel-types'

import * as literals from './literal-renderers.js'
import * as statements from './statement-renderers.js'
import * as expressions from './expression-renderers.js'
import * as declarations from './declaration-renderers.js'
import * as patterns from './pattern-renderers.js'
import * as modules from './module-renderers.js'
import * as classes from './class-renderers.js'
import * as reservedKeywords from './reserved-keywords.js'

type File = babylon.Node & {
  type: 'File',
}

export const NodeWrapper = ({ className='', inline=false, block=false, children }
  : { className?: string, inline?: boolean, block?: boolean, children: React.Node }) => {
  if (inline === block) return undefined

  const props = {
    className: className,
  }

  const Wrapper = inline ? 'span' : block ? 'div' : null
  if (Wrapper === null) return undefined

  return (
    <Wrapper {...props}>
      { children }
    </Wrapper>
  )
}

export const NodeRenderer = ({ node }: { node: babylon.Node }) => {
  if (t.isFile(node))       return <FileRenderer node={ node } />
  if (t.isIdentifier(node)) return <IdentifierRenderer node={ node } />
  // case 'PrivateName':
  // Literals [
  if (t.isRegExpLiteral(node))  return <literals.RegExpLiteralRenderer node={ node } />
  if (t.isNullLiteral(node))    return <literals.NullLiteralRenderer node={ node } />
  if (t.isStringLiteral(node))  return <literals.StringLiteralRenderer node={ node } />
  if (t.isBooleanLiteral(node)) return <literals.BooleanLiteralRenderer node={ node } />
  if (t.isNumericLiteral(node)) return <literals.NumericLiteralRenderer node={ node } />
  // Literals ]
  if (t.isProgram(node)) return <ProgramRenderer node={ node } />
  // Statements [
  if (t.isExpressionStatement(node)) return <statements.ExpressionStatementRenderer node={ node } />
  if (t.isBlockStatement(node))      return <statements.BlockStatementRenderer node={ node } />
  if (t.isEmptyStatement(node))      return <statements.EmptyStatementRenderer node={ node } />
  if (t.isDebuggerStatement(node))   return <statements.DebuggerStatementRenderer node={ node } />
  if (t.isWithStatement(node))       return <statements.WithStatementRenderer node={ node } />
  if (t.isReturnStatement(node))     return <statements.ReturnStatementRenderer node={ node } />
  if (t.isLabeledStatement(node))    return <statements.LabeledStatementRenderer node={ node } />
  if (t.isBreakStatement(node))      return <statements.BreakStatementRenderer node={ node } />
  if (t.isContinueStatement(node))   return <statements.ContinueStatementRenderer node={ node } />
  if (t.isIfStatement(node))         return <statements.IfStatementRenderer node={ node } />
  if (t.isSwitchStatement(node))     return <statements.SwitchStatementRenderer node={ node } />
  if (t.isSwitchCase(node))          return <statements.SwitchCaseRenderer node={ node } />
  if (t.isThrowStatement(node))      return <statements.ThrowStatementRenderer node={ node } />
  if (t.isTryStatement(node))        return <statements.TryStatementRenderer node={ node } />
  if (t.isCatchClause(node))         return <statements.CatchClauseRenderer node={ node } />
  if (t.isWhileStatement(node))      return <statements.WhileStatementRenderer node={ node } />
  if (t.isDoWhileStatement(node))    return <statements.DoWhileStatementRenderer node={ node } />
  if (t.isForStatement(node))        return <statements.ForStatementRenderer node={ node } />
  if (t.isForInStatement(node))      return <statements.ForInStatementRenderer node={ node } />
  if (t.isForOfStatement(node))      return <statements.ForOfStatementRenderer node={ node } />
  // Statements ]
  // Declarations [
  if (t.isFunctionDeclaration(node)) return <declarations.FunctionDeclarationRenderer node={ node } />
  if (t.isVariableDeclaration(node)) return <declarations.VariableDeclarationRenderer node={ node } />
  if (t.isVariableDeclarator(node))  return <declarations.VariableDeclaratorRenderer node={ node } />
  if (t.isClassDeclaration(node))    return <declarations.ClassDeclarationRenderer node={ node } />
  // Declarations ]
  if (t.isDecorator(node)) return <DecoratorRenderer node={ node } />
  //case 'Directive':
  //case 'DirectiveLiteral':
  // Expressions [
  if (t.isSuper(node))                   return <expressions.SuperRenderer node={ node } />
  if (t.isImport(node))                  return <expressions.ImportRenderer node={ node } />
  if (t.isThisExpression(node))          return <expressions.ThisExpressionRenderer node={ node } />
  if (t.isArrowFunctionExpression(node)) return <expressions.ArrowFunctionExpressionRenderer node={ node } />
  if (t.isYieldExpression(node))         return <expressions.YieldExpressionRenderer node={ node } />
  if (t.isAwaitExpression(node))         return <expressions.AwaitExpressionRenderer node={ node } />
  if (t.isArrayExpression(node))         return <expressions.ArrayExpressionRenderer node={ node } />
  if (t.isObjectExpression(node))        return <expressions.ObjectExpressionRenderer node={ node } />
  if (t.isObjectMethod(node))            return <expressions.ObjectMethodRenderer node={ node } />
  if (t.isObjectProperty(node))          return <expressions.ObjectPropertyRenderer node={ node } />
  if (t.isFunctionExpression(node))      return <expressions.FunctionExpressionRenderer node={ node } />
  if (t.isUnaryExpression(node))         return <expressions.UnaryExpressionRenderer node={ node } />
  if (t.isUpdateExpression(node))        return <expressions.UpdateExpressionRenderer node={ node } />
  if (t.isBinaryExpression(node))        return <expressions.BinaryExpressionRenderer node={ node } />
  if (t.isAssignmentExpression(node))    return <expressions.AssignmentExpressionRenderer node={ node } />
  if (t.isLogicalExpression(node))       return <expressions.LogicalExpressionRenderer node={ node } />
  if (t.isSpreadElement(node) || t.isSpreadProperty(node)) return <expressions.SpreadElementRenderer node={ node } />
  if (t.isMemberExpression(node))        return <expressions.MemberExpressionRenderer node={ node } />
  if (t.isBindExpression(node))          return <expressions.BindExpressionRenderer node={ node } />
  if (t.isConditionalExpression(node))   return <expressions.ConditionalExpressionRenderer node={ node } />
  if (t.isCallExpression(node))          return <expressions.CallExpressionRenderer node={ node } />
  if (t.isNewExpression(node))           return <expressions.NewExpressionRenderer node={ node } />
  if (t.isSequenceExpression(node))      return <expressions.SequenceExpressionRenderer node={ node } />
  if (t.isDoExpression(node))            return <expressions.DoExpressionRenderer node={ node } />
  if (t.isClassExpression(node))         return <expressions.ClassExpressionRenderer node={ node } />
  // case 'MetaProperty':
  // Expressions ]
  // Template Literals [
  // case 'TemplateLiteral':
  // case 'TaggedTemplateExpression':
  // Template Literals ]
  // Patterns [
  if (t.isObjectPattern(node))        return <patterns.ObjectPatternRenderer node={ node } />
  //if (t.isAssignmentProperty(node)) return <patterns.AssignmentPropertyRenderer node={ node } />
  if (t.isArrayPattern(node))         return <patterns.ArrayPatternRenderer node={ node } />
  if (t.isRestElement(node))          return <patterns.RestElementRenderer node={ node } />
  if (t.isAssignmentPattern(node))    return <patterns.AssignmentPatternRenderer node={ node } />
  // Patterns ]
  // Classes [
  if (t.isClassMethod(node))   return <classes.ClassMethodRenderer node={ node } />
  if (t.isClassProperty(node)) return <classes.ClassPropertyRenderer node={ node } />
  // Classes ]
  // Modules [
  if (t.isImportDeclaration(node))        return <modules.ImportDeclarationRenderer node={ node } />
  if (t.isImportSpecifier(node))          return <modules.ImportSpecifierRenderer node={ node } />
  if (t.isImportDefaultSpecifier(node))   return <modules.ImportDefaultSpecifierRenderer node={ node } />
  if (t.isImportNamespaceSpecifier(node)) return <modules.ImportNamespaceSpecifierRenderer node={ node } />
  if (t.isExportNamedDeclaration(node))   return <modules.ExportNamedDeclarationRenderer node={ node } />
  if (t.isExportDefaultDeclaration(node)) return <modules.ExportDefaultDeclarationRenderer node={ node } />
  if (t.isExportAllDeclaration(node))     return <modules.ExportAllDeclarationRenderer node={ node } />
  // Modules ]
  return node == null ? null : <UnknownNodeRenderer node={ node } />
}

const UnknownNodeRenderer = ({ node }: { node: babylon.Node }) => (
  <NodeWrapper inline className="ms-fontColor-neutralLight ms-bgColor-red">
    [UNKNOWN: { node.type }]
  </NodeWrapper>
)

const FileRenderer = ({ node }: { node: File }) => (
  <NodeWrapper block>
    <NodeRenderer node={ node.program } />
  </NodeWrapper>
)

const IdentifierRenderer = ({ node }: { node: babylon.Identifier }) => (
  <NodeWrapper inline className="identifier ms-fontColor-neutralLight">
    { node.name }
  </NodeWrapper>
)

const ProgramRenderer = ({ node }: { node: babylon.Program }) => (
  <NodeWrapper block>
    { node.directives.map((directive, i) => <NodeRenderer key={ i } node={ directive } />) }
    { node.body.map((child, i) => <NodeRenderer key={ i } node={ child } />) }
  </NodeWrapper>
)

export const FunctionRenderer = ({ node }: { node: babylon.Function }) => (
  <NodeWrapper inline>
    { node.async ? <reservedKeywords.Async /> : null }
    <reservedKeywords.Function />
    { node.generator ? <PunctuationRenderer punctuation="*" /> : null }
    <NodeRenderer node={ node.id } />
    <BracketRenderer bracket="(" />
    <CommaSeparatedList elements={ node.params } inline />
    <BracketRenderer bracket=")" />
    <NodeRenderer node={ node.body } />
  </NodeWrapper>
)

const DecoratorRenderer = ({ node }: { node: babylon.Decorator }) => (
  <NodeWrapper block>
    <PunctuationRenderer punctuation="@" />
    <NodeRenderer node={ node.expression } />
  </NodeWrapper>
)

//

export const CommaSeparatedList = ({ className='', elements, inline=false }: { className?: string, elements: babylon.Node[], inline?: boolean }) => {
  className += ' comma-separated-list' + (inline ? ' inline' : '')
  return (

      { elements.map((element, i) => <NodeRenderer key={i} node={ element } />) }

  )
}

export const PunctuationRenderer = ({ punctuation }: { punctuation: string }) => {
  let className = 'punctuation ms-fontColor-themeSecondary'
  if (punctuation === '.') {
    className += ' member'
  }

  return (

      { punctuation }

  )
}

export const BracketRenderer = ({ bracket }: { bracket: string }) => {
  switch (bracket) {
    case '(':
      return (

    case '[':
      return [

    case '{':
      return { '{' }

    case ')':
      return )

    case ']':
      return ]

    case '}':
      return { '}' }
  }
}

export default (ast: babylon.Node) => <NodeRenderer node={ ast } />
// vim: set ts=2 sw=2 et:


text:
=========
pygeobase
=========

The pygeobase package implements base class definitions for the I/O interface used in pytesmo_, pynetCF_, and other packages.

.. _pytesmo: https://github.com/TUW-GEO/pytesmo
.. _pynetCF: https://github.com/TUW-GEO/pynetCF


text:
int SC_initiate_station(new_STATION* my_station, new_INPUT* my_input)
{
	puts(" initiate station here !!!!!" );
fflush(stdout);
	int MAX = 10000;
	int count;
	int ista;

	for(ista = 0; ista < my_input->MAX_STA; ista++)
	{
	//printf("initiate station for %d \n", ista);
		my_station[ista].name = (char*)malloc(sizeof(char)*100);
		my_station[ista].dt_array = (double*)malloc(sizeof(double)*MAX);
		my_station[ista].neighbour_sta_dt_array = (double*)malloc(sizeof(double)*MAX);
		my_station[ista].BAZ = (double*)malloc(sizeof(double)*MAX);
		my_station[ista].incident = (double*)malloc(sizeof(double)*MAX);

		my_station[ista].neighbour_sta_array = (char**)malloc(sizeof(char*)*MAX);
		for(count = 0; count < MAX; count++)
			my_station[ista].neighbour_sta_array[count] = (char*)malloc(sizeof(char)*10);
	}

	return 0;
}


text:
import React, {
  PropTypes,
} from 'react';

import {
  StyleSheet,
  Text,
} from 'react-native';

import {
  xishengStyles,
} from '../styles';

const cs = StyleSheet.create(xishengStyles);

const propTypes = {
  children: PropTypes.oneOfType([
    PropTypes.arrayOf(PropTypes.node),
    PropTypes.node,
    PropTypes.string,
  ]),
  style: PropTypes.any,
};

const defaultProps = {};

export default function H3(props) {
  return (
    <Text
      {...props}
      style={[cs.h3, cs.marginBottom, props.style]}
    >
      {props.children}
    </Text>
  );
}

H3.propTypes = propTypes;
H3.defaultProps = defaultProps;


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <TITLE>
        CookieManager (Apache JMeter API)
    </TITLE>

    <META NAME="keywords" CONTENT="org.apache.jmeter.protocol.http.control.CookieManager class">

    <LINK REL="stylesheet" TYPE="text/css" HREF="../../../../../../stylesheet.css" TITLE="Style">

                    Overview
                    Package
                      Class

                    Tree

                    Deprecated
                    Index
                    Help

            Apache JMeter

             PREV CLASS
             NEXT CLASS

            FRAMES
             NO FRAMES

                All Classes

            SUMMARY: NESTED | FIELD | CONSTR | METHOD

            DETAIL: FIELD | CONSTR | METHOD

        org.apache.jmeter.protocol.http.control

    Class CookieManager

java.lang.Object
  org.apache.jmeter.testelement.AbstractTestElement
      org.apache.jmeter.config.ConfigTestElement
          org.apache.jmeter.protocol.http.control.CookieManager

All Implemented Interfaces:

Cloneable, ConfigElement, Serializable, TestElement, TestListener

public class CookieManager

extends ConfigTestElement

implements TestListener, Serializable

    This class provides an interface to the netscape cookies file to pass cookies
    along with a request.

    Now uses Commons HttpClient parsing and matching code (since 2.1.2)

See Also:

Serialized
        Form

            Field Summary

            static String
        DEFAULT_POLICY

        Fields inherited from class org.apache.jmeter.config.ConfigTestElement

        PASSWORD, USERNAME

        Fields inherited from interface org.apache.jmeter.testelement.TestElement

        COMMENTS, ENABLED, GUI_CLASS, NAME, TEST_CLASS

            Constructor Summary

        CookieManager()

        Method Summary

         void
    add(Cookie c)

                  Add a cookie.

         void
    addCookieFromHeader(String cookieHeader,
        URL url)

         void
    addFile(String cookieFile)

                  Add cookie data from a file.

         void
    clear()

                  Clear the TestElement of all data.

         Object
    clone()

         Cookie
    get(int i)

                  Return the cookie at index i.

         boolean
    getClearEachIteration()

         int
    getCookieCount()

         String
    getCookieHeaderForURL(URL url)

                  Find cookies applicable to the given URL and build
        the Cookie header from
        them.

         CollectionProperty
    getCookies()

         org.apache.commons.httpclient.Cookie[]
    getCookiesForUrl(URL url)

                  Get array of valid HttpClient cookies for the URL

         String
    getPolicy()

         void
    recoverRunningVersion()

                  Tells the test element to return to the state it was
        in when
        makeRunningVersion() was called.

         void
    remove(int index)

                  Remove a cookie.

         void
    save(String authFile)

                  Save the static cookie data to a file.

         void
    setClearEachIteration(boolean clear)

         void
    setCookiePolicy(String policy)

         void
    setRunningVersion(boolean running)

                  Sets the runningVersion.

         void
    testEnded()

        Called once for all threads after the end of a test.

         void
    testEnded(String host)

        Called once for all threads after the end of a test.

         void
    testIterationStart(LoopIterationEvent event)

                  Each time through a Thread Group's test script, an
        iteration event is
        fired for each thread.

         void
    testStarted()

        Called just before the start of the test from the main engine thread.

         void
    testStarted(String host)

        Called just before the start of the test from the main engine thread.

        Methods inherited from class org.apache.jmeter.config.ConfigTestElement

        addConfigElement,
            addTestElement,
            expectsModification

        Methods inherited from class org.apache.jmeter.testelement.AbstractTestElement

        addProperty,
            canRemove,
            clearTemporary,
            emptyTemporary,
            equals,
            getComment,
            getName, getProperty,
            getPropertyAsBoolean,
            getPropertyAsBoolean,
            getPropertyAsDouble,
            getPropertyAsFloat,
            getPropertyAsInt,
            getPropertyAsInt,
            getPropertyAsLong,
            getPropertyAsString,
            getPropertyAsString,
            getThreadContext,
            getThreadName,
            hashCode,
            isEnabled,
            isRunningVersion,
            isTemporary,
            logProperties,
            mergeIn,
            propertyIterator,
            removeProperty,
            setComment,
            setName,
            setProperty,
            setProperty,
            setProperty,
            setProperty,
            setProperty,
            setProperty,
            setProperty,
            setTemporary,
            setThreadContext,
            setThreadName,
            traverse,
            traverseCollection,
            traverseMap,
            traverseProperty

        Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, toString, wait, wait, wait

            Field Detail

    DEFAULT_POLICY

public static final String DEFAULT_POLICY

See Also:

            Constant
                Field Values

            Constructor Detail

    CookieManager

public CookieManager()

            Method Detail

    clone

public Object clone()

Specified by:

clone
                in interface ConfigElement

Overrides:

clone
                in class AbstractTestElement

    getPolicy

public String getPolicy()

    setCookiePolicy

public void setCookiePolicy(String policy)

    getCookies

public CollectionProperty getCookies()

    getCookieCount

public int getCookieCount()

    getClearEachIteration

public boolean getClearEachIteration()

    setClearEachIteration

public void setClearEachIteration(boolean clear)

    save

public void save(String authFile)
          throws IOException

Save the static cookie data to a file.
        Cookies are only taken from the GUI - runtime cookies are not included.

Throws:

IOException

    addFile

public void addFile(String cookieFile)
             throws IOException

Add cookie data from a file.

Throws:

IOException

    recoverRunningVersion

public void recoverRunningVersion()

Description copied from interface: TestElement

Tells the test element to return to the state it was in when
        makeRunningVersion() was called.

Specified by:

recoverRunningVersion
                in interface TestElement

Overrides:

recoverRunningVersion
                in class AbstractTestElement

    setRunningVersion

public void setRunningVersion(boolean running)

Description copied from class: AbstractTestElement

Sets the runningVersion.

Specified by:

setRunningVersion
                in interface TestElement

Overrides:

setRunningVersion
                in class AbstractTestElement

Parameters:

running - the runningVersion to set

    add

public void add(Cookie c)

Add a cookie.

    clear

public void clear()

Description copied from interface: TestElement

Clear the TestElement of all data.

Specified by:

clear in
                interface TestElement

Overrides:

clear
                in class AbstractTestElement

    remove

public void remove(int index)

Remove a cookie.

    get

public Cookie get(int i)

Return the cookie at index i.

    getCookiesForUrl

public org.apache.commons.httpclient.Cookie[] getCookiesForUrl(URL url)

Get array of valid HttpClient cookies for the URL

Parameters:

url - the target URL

Returns:

array of HttpClient cookies

    getCookieHeaderForURL

public String getCookieHeaderForURL(URL url)

Find cookies applicable to the given URL and build the Cookie header from
        them.

Parameters:

url - URL of the request to which the returned header will be added.

Returns:

the value string for the cookie header (goes after "Cookie: ").

    addCookieFromHeader

public void addCookieFromHeader(String cookieHeader,
                                URL url)

    testStarted

public void testStarted()

Description copied from interface: TestListener

        Called just before the start of the test from the main engine thread.

        This is before the test elements are cloned.

        Note that not all the test
        variables will have been set up at this point.

                N.B. testStarted() and testEnded() are called from different threads.

Specified by:

testStarted
                in interface TestListener

See Also:

StandardJMeterEngine.run()

    testEnded

public void testEnded()

Description copied from interface: TestListener

        Called once for all threads after the end of a test.

        This will use the same element instances as at the start of the test.

                N.B. testStarted() and testEnded() are called from different threads.

Specified by:

testEnded
                in interface TestListener

See Also:

StandardJMeterEngine.stopTest()

    testStarted

public void testStarted(String host)

Description copied from interface: TestListener

        Called just before the start of the test from the main engine thread.

        This is before the test elements are cloned.

        Note that not all the test
        variables will have been set up at this point.

                N.B. testStarted() and testEnded() are called from different threads.

Specified by:

testStarted
                in interface TestListener

See Also:

StandardJMeterEngine.run()

    testEnded

public void testEnded(String host)

Description copied from interface: TestListener

        Called once for all threads after the end of a test.

        This will use the same element instances as at the start of the test.

                N.B. testStarted() and testEnded() are called from different threads.

Specified by:

testEnded
                in interface TestListener

See Also:

StandardJMeterEngine.stopTest()

    testIterationStart

public void testIterationStart(LoopIterationEvent event)

Description copied from interface: TestListener

Each time through a Thread Group's test script, an iteration event is
        fired for each thread.

        This will be after the test elements have been cloned, so in general
        the instance will not be the same as the ones the start/end methods call.

Specified by:

testIterationStart
                in interface TestListener

Parameters:

event -

                    Overview
                    Package
                      Class

                    Tree

                    Deprecated
                    Index
                    Help

            Apache JMeter

             PREV CLASS
             NEXT CLASS

            FRAMES
             NO FRAMES

                All Classes

            SUMMARY: NESTED | FIELD | CONSTR | METHOD

            DETAIL: FIELD | CONSTR | METHOD

Copyright © 1998-2009 Apache Software Foundation. All Rights Reserved.


text:
ExtensionPrefValueMapFactory::ExtensionPrefValueMapFactory()
    : ProfileKeyedServiceFactory(
        "ExtensionPrefValueMap",
        ProfileDependencyManager::GetInstance()) {
}

ExtensionPrefValueMapFactory::~ExtensionPrefValueMapFactory() {
}

// static
ExtensionPrefValueMap* ExtensionPrefValueMapFactory::GetForProfile(
    Profile* profile) {
  return static_cast<ExtensionPrefValueMap*>(
      GetInstance()->GetServiceForProfile(profile, true));
}

// static
ExtensionPrefValueMapFactory* ExtensionPrefValueMapFactory::GetInstance() {
  return Singleton<ExtensionPrefValueMapFactory>::get();
}

ProfileKeyedService* ExtensionPrefValueMapFactory::BuildServiceInstanceFor(
    content::BrowserContext* profile) const {
  return new ExtensionPrefValueMap();
}