text:
module Test.QuickCheck.Arbitrary
  (
  -- * Arbitrary and CoArbitrary classes
    Arbitrary(..)
  , CoArbitrary(..)

  -- ** Helper functions for implementing arbitrary
  , arbitrarySizedIntegral        -- :: Num a => Gen a
  , arbitraryBoundedIntegral      -- :: (Bounded a, Integral a) => Gen a
  , arbitrarySizedBoundedIntegral -- :: (Bounded a, Integral a) => Gen a
  , arbitrarySizedFractional      -- :: Fractional a => Gen a
  , arbitraryBoundedRandom        -- :: (Bounded a, Random a) => Gen a
  , arbitraryBoundedEnum          -- :: (Bounded a, Enum a) => Gen a
  -- ** Helper functions for implementing shrink
  , shrinkNothing            -- :: a -> [a]
  , shrinkList               -- :: (a -> [a]) -> [a] -> [[a]]
  , shrinkIntegral           -- :: Integral a => a -> [a]
  , shrinkRealFrac           -- :: RealFrac a => a -> [a]
  -- ** Helper functions for implementing coarbitrary
  , (><)
  , coarbitraryIntegral      -- :: Integral a => a -> Gen b -> Gen b
  , coarbitraryReal          -- :: Real a => a -> Gen b -> Gen b
  , coarbitraryShow          -- :: Show a => a -> Gen b -> Gen b
  , coarbitraryEnum          -- :: Enum a => a -> Gen b -> Gen b

  -- ** Generators which use arbitrary
  , vector      -- :: Arbitrary a => Int -> Gen [a]
  , orderedList -- :: (Ord a, Arbitrary a) => Gen [a]
  )
 where

--------------------------------------------------------------------------
-- imports

import Test.QuickCheck.Gen

{-
import Data.Generics
  ( (:*:)(..)
  , (:+:)(..)
  , Unit(..)
  )
-}

import Data.Char
  ( chr
  , ord
  , isLower
  , isUpper
  , toLower
  , isDigit
  , isSpace
  )

import Data.Fixed
  ( Fixed
  , HasResolution
  )

import Data.Ratio
  ( Ratio
  , (%)
  , numerator
  , denominator
  )

import Data.Complex
  ( Complex((:+)) )

import System.Random
  ( Random
  )

import Data.List
  ( sort
  , nub
  )

import Control.Monad
  ( liftM
  , liftM2
  , liftM3
  , liftM4
  , liftM5
  )

import Data.Int(Int8, Int16, Int32, Int64)
import Data.Word(Word, Word8, Word16, Word32, Word64)

--------------------------------------------------------------------------
-- ** class Arbitrary

-- | Random generation and shrinking of values.
class Arbitrary a where
  -- | A generator for values of the given type.
  arbitrary :: Gen a
  arbitrary = error "no default generator"

  -- | Produces a (possibly) empty list of all the possible
  -- immediate shrinks of the given value.
  shrink :: a -> [a]
  shrink _ = []

-- instances

instance (CoArbitrary a, Arbitrary b) => Arbitrary (a -> b) where
  arbitrary = promote (`coarbitrary` arbitrary)

instance Arbitrary () where
  arbitrary = return ()

instance Arbitrary Bool where
  arbitrary = choose (False,True)
  shrink True = [False]
  shrink False = []

instance Arbitrary Ordering where
  arbitrary = arbitraryBoundedEnum
  shrink GT = [EQ, LT]
  shrink LT = [EQ]
  shrink EQ = []

instance Arbitrary a => Arbitrary (Maybe a) where
  arbitrary = frequency [(1, return Nothing), (3, liftM Just arbitrary)]

  shrink (Just x) = Nothing : [ Just x' | x' <- shrink x ]
  shrink _        = []

instance (Arbitrary a, Arbitrary b) => Arbitrary (Either a b) where
  arbitrary = oneof [liftM Left arbitrary, liftM Right arbitrary]

  shrink (Left x)  = [ Left  x' | x' <- shrink x ]
  shrink (Right y) = [ Right y' | y' <- shrink y ]

instance Arbitrary a => Arbitrary [a] where
  arbitrary = sized $ \n ->
    do k <- choose (0,n)
       sequence [ arbitrary | _ <- [1..k] ]

  shrink xs = shrinkList shrink xs

shrinkList :: (a -> [a]) -> [a] -> [[a]]
shrinkList shr xs = concat [ removes k n xs | k <- takeWhile (>0) (iterate (`div`2) n) ]
                 ++ shrinkOne xs
 where
  n = length xs

  shrinkOne []     = []
  shrinkOne (x:xs) = [ x':xs | x'  <- shr x ]
                  ++ [ x:xs' | xs' <- shrinkOne xs ]

  removes k n xs
    | k > n     = []
    | null xs2  = [[]]
    | otherwise = xs2 : map (xs1 ++) (removes k (n-k) xs2)
   where
    xs1 = take k xs
    xs2 = drop k xs

{-
  -- "standard" definition for lists:
  shrink []     = []
  shrink (x:xs) = [ xs ]
               ++ [ x:xs' | xs' <- shrink xs ]
               ++ [ x':xs | x'  <- shrink x ]
-}

instance (Integral a, Arbitrary a) => Arbitrary (Ratio a) where
  arbitrary = arbitrarySizedFractional
  shrink    = shrinkRealFrac

instance (RealFloat a, Arbitrary a) => Arbitrary (Complex a) where
  arbitrary = liftM2 (:+) arbitrary arbitrary
  shrink (x :+ y) = [ x' :+ y | x' <- shrink x ] ++
                    [ x :+ y' | y' <- shrink y ]

instance HasResolution a => Arbitrary (Fixed a) where
    arbitrary = arbitrarySizedFractional
    shrink    = shrinkRealFrac

instance (Arbitrary a, Arbitrary b)
      => Arbitrary (a,b)
 where
  arbitrary = liftM2 (,) arbitrary arbitrary

  shrink (x,y) = [ (x',y) | x' <- shrink x ]
              ++ [ (x,y') | y' <- shrink y ]

instance (Arbitrary a, Arbitrary b, Arbitrary c)
      => Arbitrary (a,b,c)
 where
  arbitrary = liftM3 (,,) arbitrary arbitrary arbitrary

  shrink (x,y,z) = [ (x',y,z) | x' <- shrink x ]
                ++ [ (x,y',z) | y' <- shrink y ]
                ++ [ (x,y,z') | z' <- shrink z ]

instance (Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d)
      => Arbitrary (a,b,c,d)
 where
  arbitrary = liftM4 (,,,) arbitrary arbitrary arbitrary arbitrary

  shrink (w,x,y,z) = [ (w',x,y,z) | w' <- shrink w ]
                  ++ [ (w,x',y,z) | x' <- shrink x ]
                  ++ [ (w,x,y',z) | y' <- shrink y ]
                  ++ [ (w,x,y,z') | z' <- shrink z ]

instance (Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d, Arbitrary e)
      => Arbitrary (a,b,c,d,e)
 where
  arbitrary = liftM5 (,,,,) arbitrary arbitrary arbitrary arbitrary arbitrary

  shrink (v,w,x,y,z) = [ (v',w,x,y,z) | v' <- shrink v ]
                    ++ [ (v,w',x,y,z) | w' <- shrink w ]
                    ++ [ (v,w,x',y,z) | x' <- shrink x ]
                    ++ [ (v,w,x,y',z) | y' <- shrink y ]
                    ++ [ (v,w,x,y,z') | z' <- shrink z ]

-- typical instance for primitive (numerical) types

instance Arbitrary Integer where
  arbitrary = arbitrarySizedIntegral
  shrink    = shrinkIntegral

instance Arbitrary Int where
  arbitrary = arbitrarySizedBoundedIntegral
  shrink    = shrinkIntegral

instance Arbitrary Int8 where
  arbitrary = arbitrarySizedBoundedIntegral
  shrink    = shrinkIntegral

instance Arbitrary Int16 where
  arbitrary = arbitrarySizedBoundedIntegral
  shrink    = shrinkIntegral

instance Arbitrary Int32 where
  arbitrary = arbitrarySizedBoundedIntegral
  shrink    = shrinkIntegral

instance Arbitrary Int64 where
  arbitrary = arbitrarySizedBoundedIntegral
  shrink    = shrinkIntegral

instance Arbitrary Word where
  arbitrary = arbitrarySizedBoundedIntegral
  shrink    = shrinkIntegral

instance Arbitrary Word8 where
  arbitrary = arbitrarySizedBoundedIntegral
  shrink    = shrinkIntegral

instance Arbitrary Word16 where
  arbitrary = arbitrarySizedBoundedIntegral
  shrink    = shrinkIntegral

instance Arbitrary Word32 where
  arbitrary = arbitrarySizedBoundedIntegral
  shrink    = shrinkIntegral

instance Arbitrary Word64 where
  arbitrary = arbitrarySizedBoundedIntegral
  shrink    = shrinkIntegral

instance Arbitrary Char where
  arbitrary = chr `fmap` oneof [choose (0,127), choose (0,255)]
  shrink c  = filter (<. c) $ nub
            $ ['a','b','c']
           ++ [ toLower c | isUpper c ]
           ++ ['A','B','C']
           ++ ['1','2','3']
           ++ [' ','\n']
   where
    a <. b  = stamp a < stamp b
    stamp a = ( (not (isLower a)
              , not (isUpper a)
              , not (isDigit a))
              , (not (a==' ')
              , not (isSpace a)
              , a)
              )

instance Arbitrary Float where
  arbitrary = arbitrarySizedFractional
  shrink    = shrinkRealFrac

instance Arbitrary Double where
  arbitrary = arbitrarySizedFractional
  shrink    = shrinkRealFrac

-- ** Helper functions for implementing arbitrary

-- | Generates an integral number. The number can be positive or negative
-- and its maximum absolute value depends on the size parameter.
arbitrarySizedIntegral :: Num a => Gen a
arbitrarySizedIntegral =
  sized $ \n ->
    let n' = toInteger n in
      fmap fromInteger (choose (-n', n'))

-- | Generates a fractional number. The number can be positive or negative
-- and its maximum absolute value depends on the size parameter.
arbitrarySizedFractional :: Fractional a => Gen a
arbitrarySizedFractional =
  sized $ \n ->
    let n' = toInteger n in
      do a <- choose ((-n') * precision, n' * precision)
         b <- choose (1, precision)
         return (fromRational (a % b))
 where
  precision = 9999999999999 :: Integer

-- | Generates an integral number. The number is chosen uniformly from
-- the entire range of the type. You may want to use
-- 'arbitrarySizedBoundedIntegral' instead.
arbitraryBoundedIntegral :: (Bounded a, Integral a) => Gen a
arbitraryBoundedIntegral =
  do let mn = minBound
         mx = maxBound `asTypeOf` mn
     n <- choose (toInteger mn, toInteger mx)
     return (fromInteger n `asTypeOf` mn)

-- | Generates an element of a bounded type. The element is
-- chosen from the entire range of the type.
arbitraryBoundedRandom :: (Bounded a, Random a) => Gen a
arbitraryBoundedRandom = choose (minBound,maxBound)

-- | Generates an element of a bounded enumeration.
arbitraryBoundedEnum :: (Bounded a, Enum a) => Gen a
arbitraryBoundedEnum =
  do let mn = minBound
         mx = maxBound `asTypeOf` mn
     n <- choose (fromEnum mn, fromEnum mx)
     return (toEnum n `asTypeOf` mn)

-- | Generates an integral number from a bounded domain. The number is
-- chosen from the entire range of the type, but small numbers are
-- generated more often than big numbers. Inspired by demands from
-- Phil Wadler.
arbitrarySizedBoundedIntegral :: (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral =
  sized $ \s ->
    do let mn = minBound
           mx = maxBound `asTypeOf` mn
           bits n | n `quot` 2 == 0 = 0
                  | otherwise = 1 + bits (n `quot` 2)
           k  = 2^(s*(bits mn `max` bits mx `max` 40) `div` 100)
       n <- choose (toInteger mn `max` (-k), toInteger mx `min` k)
       return (fromInteger n `asTypeOf` mn)

-- ** Helper functions for implementing shrink

-- | Returns no shrinking alternatives.
shrinkNothing :: a -> [a]
shrinkNothing _ = []

-- | Shrink an integral number.
shrinkIntegral :: Integral a => a -> [a]
shrinkIntegral x =
  nub $
  [ -x
  | x < 0, -x > x
  ] ++
  [ x'
  | x' <- takeWhile (<< x) (0:[ x - i | i <- tail (iterate (`quot` 2) x) ])
  ]
 where
   -- a << b is "morally" abs a < abs b, but taking care of overflow.
   a << b = case (a >= 0, b >= 0) of
            (True,  True)  -> a < b
            (False, False) -> a > b
            (True,  False) -> a + b < 0
            (False, True)  -> a + b > 0

-- | Shrink a fraction.
shrinkRealFrac :: RealFrac a => a -> [a]
shrinkRealFrac x =
  nub $
  [ -x
  | x < 0
  ] ++
  [ x'
  | x' <- [fromInteger (truncate x)]
  , x' << x
  ]
 where
  a << b = abs a < abs b

--------------------------------------------------------------------------
-- ** CoArbitrary

-- | Used for random generation of functions.
class CoArbitrary a where
  -- | Used to generate a function of type @a -> c@. The implementation
  -- should use the first argument to perturb the random generator
  -- given as the second argument. the returned generator
  -- is then used to generate the function result.
  -- You can often use 'variant' and '><' to implement
  -- 'coarbitrary'.
  coarbitrary :: a -> Gen c -> Gen c

{-
  -- GHC definition:
  coarbitrary{| Unit |}    Unit      = id
  coarbitrary{| a :*: b |} (x :*: y) = coarbitrary x >< coarbitrary y
  coarbitrary{| a :+: b |} (Inl x)   = variant 0    . coarbitrary x
  coarbitrary{| a :+: b |} (Inr y)   = variant (-1) . coarbitrary y
-}

-- | Combine two generator perturbing functions, for example the
-- results of calls to 'variant' or 'coarbitrary'.
(><) :: (Gen a -> Gen a) -> (Gen a -> Gen a) -> (Gen a -> Gen a)
(><) f g gen =
  do n <- arbitrary
     (g . variant (n :: Int) . f) gen

-- for the sake of non-GHC compilers, I have added definitions
-- for coarbitrary here.

instance (Arbitrary a, CoArbitrary b) => CoArbitrary (a -> b) where
  coarbitrary f gen =
    do xs <- arbitrary
       coarbitrary (map f xs) gen

instance CoArbitrary () where
  coarbitrary _ = id

instance CoArbitrary Bool where
  coarbitrary False = variant 0
  coarbitrary True  = variant (-1)

instance CoArbitrary Ordering where
  coarbitrary GT = variant 1
  coarbitrary EQ = variant 0
  coarbitrary LT = variant (-1)

instance CoArbitrary a => CoArbitrary (Maybe a) where
  coarbitrary Nothing  = variant 0
  coarbitrary (Just x) = variant (-1) . coarbitrary x

instance (CoArbitrary a, CoArbitrary b) => CoArbitrary (Either a b) where
  coarbitrary (Left x)  = variant 0    . coarbitrary x
  coarbitrary (Right y) = variant (-1) . coarbitrary y

instance CoArbitrary a => CoArbitrary [a] where
  coarbitrary []     = variant 0
  coarbitrary (x:xs) = variant (-1) . coarbitrary (x,xs)

instance (Integral a, CoArbitrary a) => CoArbitrary (Ratio a) where
  coarbitrary r = coarbitrary (numerator r,denominator r)

instance HasResolution a => CoArbitrary (Fixed a) where
  coarbitrary = coarbitraryReal

instance (RealFloat a, CoArbitrary a) => CoArbitrary (Complex a) where
  coarbitrary (x :+ y) = coarbitrary x >< coarbitrary y

instance (CoArbitrary a, CoArbitrary b)
      => CoArbitrary (a,b)
 where
  coarbitrary (x,y) = coarbitrary x
                   >< coarbitrary y

instance (CoArbitrary a, CoArbitrary b, CoArbitrary c)
      => CoArbitrary (a,b,c)
 where
  coarbitrary (x,y,z) = coarbitrary x
                     >< coarbitrary y
                     >< coarbitrary z

instance (CoArbitrary a, CoArbitrary b, CoArbitrary c, CoArbitrary d)
      => CoArbitrary (a,b,c,d)
 where
  coarbitrary (x,y,z,v) = coarbitrary x
                       >< coarbitrary y
                       >< coarbitrary z
                       >< coarbitrary v

instance (CoArbitrary a, CoArbitrary b, CoArbitrary c, CoArbitrary d, CoArbitrary e)
      => CoArbitrary (a,b,c,d,e)
 where
  coarbitrary (x,y,z,v,w) = coarbitrary x
                         >< coarbitrary y
                         >< coarbitrary z
                         >< coarbitrary v
                         >< coarbitrary w

-- typical instance for primitive (numerical) types

instance CoArbitrary Integer where
  coarbitrary = coarbitraryIntegral

instance CoArbitrary Int where
  coarbitrary = coarbitraryIntegral

instance CoArbitrary Int8 where
  coarbitrary = coarbitraryIntegral

instance CoArbitrary Int16 where
  coarbitrary = coarbitraryIntegral

instance CoArbitrary Int32 where
  coarbitrary = coarbitraryIntegral

instance CoArbitrary Int64 where
  coarbitrary = coarbitraryIntegral

instance CoArbitrary Word where
  coarbitrary = coarbitraryIntegral

instance CoArbitrary Word8 where
  coarbitrary = coarbitraryIntegral

instance CoArbitrary Word16 where
  coarbitrary = coarbitraryIntegral

instance CoArbitrary Word32 where
  coarbitrary = coarbitraryIntegral

instance CoArbitrary Word64 where
  coarbitrary = coarbitraryIntegral

instance CoArbitrary Char where
  coarbitrary = coarbitrary . ord

instance CoArbitrary Float where
  coarbitrary = coarbitraryReal

instance CoArbitrary Double where
  coarbitrary = coarbitraryReal

-- ** Helpers for implementing coarbitrary

-- | A 'coarbitrary' implementation for integral numbers.
coarbitraryIntegral :: Integral a => a -> Gen b -> Gen b
coarbitraryIntegral = variant

-- | A 'coarbitrary' implementation for real numbers.
coarbitraryReal :: Real a => a -> Gen b -> Gen b
coarbitraryReal x = coarbitrary (toRational x)

-- | 'coarbitrary' helper for lazy people :-).
coarbitraryShow :: Show a => a -> Gen b -> Gen b
coarbitraryShow x = coarbitrary (show x)

-- | A 'coarbitrary' implementation for enums.
coarbitraryEnum :: Enum a => a -> Gen b -> Gen b
coarbitraryEnum = variant . fromEnum

--------------------------------------------------------------------------
-- ** arbitrary generators

-- these are here and not in Gen because of the Arbitrary class constraint

-- | Generates a list of a given length.
vector :: Arbitrary a => Int -> Gen [a]
vector k = vectorOf k arbitrary

-- | Generates an ordered list of a given length.
orderedList :: (Ord a, Arbitrary a) => Gen [a]
orderedList = sort `fmap` arbitrary

--------------------------------------------------------------------------
-- the end.


text:
    <title>Java to XML serialization, and back again</title>
    <meta name="short" content="About XStream"/>

XStream is a simple library to serialize objects to XML and back again.

Features

Ease of use. A high level facade is supplied that simplifies common use cases.

No mappings required. Most objects can be serialized without need for specifying mappings.

Performance. Speed and low memory footprint are a crucial part of the design, making it suitable for
        large object graphs or systems with high message throughput.

Clean XML. No information is duplicated that can be obtained via reflection. This results
        in XML that is easier to read for humans and more compact than native Java serialization.

Requires no modifications to objects. Serializes internal fields, including private and
        final. Supports non-public and inner classes. Classes are not required to have default constructor.

Full object graph support. Duplicate references encountered in the object-model will
        be maintained. Supports circular references.

Integrates with other XML APIs. By implementing an interface, XStream can serialize
        directly to/from any tree structure (not just XML).

Customizable conversion strategies. Strategies can be registered allowing customization of how
        particular types are represented as XML.

Security framework. Fine-control about the unmarshalled types to prevent security issues with
        manipulated input.

Error messages. When an exception occurs due to malformed XML, detailed diagnostics are provided
        to help isolate and fix the problem.

Alternative output format. The modular design allows other output formats. XStream ships currently
        with JSON support and morphing.

Typical Uses

Transport

Persistence

Configuration

Unit Tests

Known Limitations

If using the enhanced mode, XStream can re-instantiate classes that do not have a default constructor.
    However, if using a different JVM like an old JRockit version, a JDK 1.4 or you have restrictions because of a
    SecurityManager, a default constructor is required.

The enhanced mode is also necessary to restore final fields for any JDK < 1.5. This implies deserialization of
    instances of an inner class.

Auto-detection of annotations may cause race conditions. Preprocessing annotations is safe though.

Getting Started

Download it.

Use it.

Latest News

February 18, 2015 XStream 1.4.8 released

Maintenance release 1.4.8 of XStream with bug fixes and improvements running with Java 8.

XStream supports now serializable lambda types for a Java 9 runtime.

View the complete change log and download.

Note, the next major release 1.5 will require Java 6.

Thanks to this impressive list of contributors.

redpajama_set_name: RedPajamaGithub


text:
require 'spec_helper'
require 'whois/record/parser/whois.educause.edu.rb'

describe Whois::Record::Parser::WhoisEducauseEdu, "property_contact_registrant_without_zip.expected" do

  subject do
    file = fixture("responses", "whois.educause.edu/property_contact_registrant_without_zip.txt")
    part = Whois::Record::Part.new(body: File.read(file))
    described_class.new(part)
  end

  describe "#registrant_contacts" do
    it do
      expect(subject.registrant_contacts).to be_a(Array)
      expect(subject.registrant_contacts).to have(1).items
      expect(subject.registrant_contacts[0]).to be_a(Whois::Record::Contact)
      expect(subject.registrant_contacts[0].type).to eq(Whois::Record::Contact::TYPE_REGISTRANT)
      expect(subject.registrant_contacts[0].id).to eq(nil)
      expect(subject.registrant_contacts[0].name).to eq(nil)
      expect(subject.registrant_contacts[0].organization).to eq("The American University of the Caribbean School of Medicine")
      expect(subject.registrant_contacts[0].address).to eq("c/o Campbell Corporate Services, Ltd.\nScotiabank Building, P. O. Box 268")
      expect(subject.registrant_contacts[0].city).to eq("Grand Cayman")
      expect(subject.registrant_contacts[0].zip).to eq(nil)
      expect(subject.registrant_contacts[0].state).to eq(nil)
      expect(subject.registrant_contacts[0].country).to eq("CAYMAN ISLANDS")
      expect(subject.registrant_contacts[0].country_code).to eq(nil)
      expect(subject.registrant_contacts[0].phone).to eq(nil)
      expect(subject.registrant_contacts[0].fax).to eq(nil)
      expect(subject.registrant_contacts[0].email).to eq(nil)
    end
  end
end


text:
<template name="taskSee">

        {{> menuProject project=project showActivities=1 showChat=1 }}

            {{> menuEchelon project=project.title echelon=echelon.title child=task.title }}

                        {{#if project.isAdmin}}

                                    Add a new task

                                            <input name="title" type="text" placeholder="New task" />

Priority

                                                        <input type="hidden" name="priority" value="0" />

                                                        None

                                                        <input type="hidden" name="priority" value="1" />

                                                        Low

                                                        <input type="hidden" name="priority" value="2" />

                                                        Middle

                                                        <input type="hidden" name="priority" value="3" />

                                                        High

                                                        <input type="hidden" name="priority" value="4" />

                                                        Urgent

Assigned to

                                                    {{#each project.users}}

                                                            <input type="hidden" name="assignId" value="{{_id}}" />

                                                            {{prettyFindUser _id}}

                                                    {{/each}}

                        {{/if}}
                        {{#each tasks}}
                            {{> taskMessage }}
                        {{else}}

                            You don't have any task in this echelon.

                        {{/each}}

                        {{priorityToShortText task.priority}}

{{task.title}}

                        {{#if task.description}}

{{markdown task.description}}

                        {{/if}}
                        {{#if task.assignId}}
                            <input type="hidden" name="assignId" value="{{task.assignId}}" />

                            {{prettyFindUser task.assignId}}
                        {{/if}}

                        {{#if project.isAdmin}}

                                <button class="ui primary edit" data-id="{{task._id}}">Edit</button>

                        {{/if}}

Comments

                            {{> commentBloc comments=comments }}

    {{#if project.isAdmin}}

                Error

You must provide a sentence for the task to do.

                    OK

    {{/if}}
</template>


text:
require File.dirname(__FILE__) + '/../spec_helper'

describe Muck::UserSessionsController do

  render_views

  it { should filter_param(:password) }

  before(:each) do
    @login = 'quentin'
    @good_password = 'test'
    @user = Factory(:user, :login => @login, :password => @good_password, :password_confirmation => @good_password)
  end

  describe "get new" do
    before(:each) do
      get :new
    end
    it { should respond_with :success }
    it { should render_template :new }
  end

  describe "login and redirect" do
    before(:each) do
      post :create, :user_session => { :login => @login, :password => @good_password }
    end
    it "should create a user session" do
      assert user_session = UserSession.find
      @user.should == user_session.user
    end
    it {should redirect_to(user_path(@user))}
  end

  describe "login callbacks" do
    it "should call before_create_user_session" do
      controller.should_receive(:before_create_user_session)
      post :create, :user_session => { :login => @login, :password => @good_password }
    end
    it "should call after_create_user_session" do
      controller.should_receive(:after_create_user_session).with(true)
      post :create, :user_session => { :login => @login, :password => @good_password }
    end
    it "should call after_create_user_session with false" do
      controller.should_receive(:after_create_user_session).with(false)
      post :create, :user_session => { :login => @login, :password => 'bad password' }
    end
    it "should call after_create_user_session with false because before_create_user_session fails" do
      controller.stub!(:before_create_user_session).and_return(false)
      controller.should_receive(:after_create_user_session).with(false)
      post :create, :user_session => { :login => @login, :password => 'bad password' }
    end
  end

  describe "login success and render json" do
    before(:each) do
      post :create, :user_session => { :login => @login, :password => @good_password }, :format => 'json'
    end
    it "should create a user session" do
      assert user_session = UserSession.find
      @user.should == user_session.user
    end
    it "should render true" do
      @response.body.should include('true')
    end
  end

  describe "fail login" do
    before(:each) do
      post :create, :user_session => { :login => @login, :password => 'bad password' }
    end
    it "should not create a user session" do
      UserSession.find.should be_nil
    end
    it { should respond_with :success }
    it { should render_template :new }
  end

  describe "fail login json" do
    before(:each) do
      post :create, :user_session => { :login => @login, :password => 'bad password' }, :format => 'json'
    end
    it "should not create a user session" do
      UserSession.find.should be_nil
    end
    it "should render false" do
      @response.body.should include('false')
    end
  end

  describe "authlogic enabled" do
    before(:each) do
      @user = Factory(:user)
      activate_authlogic
    end
    describe "logout" do
      before(:each) do
        login_as(@user)
      end
      describe "without params" do
        before(:each) do
          delete :destroy
        end
        it "should logout by destroying the user session" do
          UserSession.find.should be_nil
        end
        it {should redirect_to(logout_complete_path)}
      end
      describe "login" do
        it "should call before_create_user_session" do
          controller.should_receive(:before_destroy_user_session)
          delete :destroy
        end
        it "should call after_destroy_user_session" do
          controller.should_receive(:after_destroy_user_session)
          delete :destroy
        end
      end
      describe "with reset api key param" do
        it "should reset the user's single_access_token" do
          controller.stub!(:current_user).and_return(@user)
          @user.should_receive(:reset_single_access_token!)
          delete :destroy, :reset_api_key => true
        end
      end
    end
  end

  describe "GET login_check" do
    before do
      @user = Factory(:user)
      activate_authlogic
    end
    describe "logged in" do
      before do
       login_as(@user)
        get :login_check
      end
      it "should render json with logged in true" do
        @response.body.should include('true')
      end
    end
    describe "not logged in" do
      before do
        get :login_check
      end
      it "should render json with logged in false" do
        @response.body.should include('false')
      end
    end
  end

end


text:
#include "objectmemory.hpp"
#include "gc/immix.hpp"

#include "instruments/stats.hpp"

#include "capi/handle.hpp"
#include "capi/tag.hpp"
#include "object_watch.hpp"

#include "configuration.hpp"

namespace rubinius {
  void ImmixGC::ObjectDescriber::added_chunk(int count) {
#ifdef IMMIX_DEBUG
    std::cout << "[GC IMMIX: Added a chunk: " << count << "]\n";
#endif

    if(object_memory_) {
      if(gc_->dec_chunks_left() <= 0) {
        // object_memory_->collect_mature_now = true;
        gc_->reset_chunks_left();
      }
    }
  }

  /**
   * This means we're getting low on memory! Time to schedule a garbage
   * collection.
   */
  void ImmixGC::ObjectDescriber::last_block() {
    if(object_memory_) {
      object_memory_->collect_mature_now = true;
    }
  }

  void ImmixGC::ObjectDescriber::set_forwarding_pointer(memory::Address from, memory::Address to) {
    from.as<Object>()->set_forward(to.as<Object>());
  }

  ImmixGC::ImmixGC(ObjectMemory* om)
    : GarbageCollector(om)
    , allocator_(gc_.block_allocator())
    , chunks_before_collection_(10)
  {
    gc_.describer().set_object_memory(om, this);
    reset_chunks_left();
  }

  memory::Address ImmixGC::ObjectDescriber::copy(memory::Address original,
      immix::Allocator& alloc) {
    Object* orig = original.as<Object>();

    memory::Address copy_addr = alloc.allocate(
        orig->size_in_bytes(object_memory_->state()));

    Object* copy = copy_addr.as<Object>();

    copy->initialize_full_state(object_memory_->state(), orig, 0);

    copy->set_zone(MatureObjectZone);
    copy->set_in_immix();

    return copy_addr;
  }

  int ImmixGC::ObjectDescriber::size(memory::Address addr) {
    return addr.as<Object>()->size_in_bytes(object_memory_->state());
  }

  ImmixGC::~ImmixGC() {
    // @todo free data
  }

  Object* ImmixGC::allocate(int bytes) {
    if(bytes > immix::cMaxObjectSize) return 0;

    Object* obj = allocator_.allocate(bytes).as<Object>();
    obj->init_header(MatureObjectZone, InvalidType);
    obj->set_in_immix();

    return obj;
  }

  Object* ImmixGC::move_object(Object* orig, int bytes) {
    if(bytes > immix::cMaxObjectSize) return 0;

    Object* obj = allocator_.allocate(bytes).as<Object>();

    memcpy(obj, orig, bytes);

    // If the header is inflated, repoint it.
    if(obj->inflated_header_p()) {
      orig->deflate_header();
      obj->inflated_header()->set_object(obj);
    }

    obj->flags().zone = MatureObjectZone;
    obj->flags().age = 0;

    obj->set_in_immix();

    orig->set_forward(obj);

    return obj;
  }

  Object* ImmixGC::saw_object(Object* obj) {
#ifdef ENABLE_OBJECT_WATCH
    if(watched_p(obj)) {
      std::cout << "detected " << obj << " during immix scanning.\n";
    }
#endif

    if(!obj->reference_p()) return obj;

    memory::Address fwd = gc_.mark_address(memory::Address(obj), allocator_);
    Object* copy = fwd.as<Object>();

    // Check and update an inflated header
    if(copy && copy != obj && obj->inflated_header_p()) {
      InflatedHeader* ih = obj->deflate_header();
      ih->reset_object(copy);
      if(!copy->set_inflated_header(ih)) {
        rubinius::bug("Massive IMMIX inflated header screwup.");
      }
    }

    return copy;
  }

  ObjectPosition ImmixGC::validate_object(Object* obj) {
    if(gc_.allocated_address(memory::Address(obj))) {
      if(obj->in_immix_p()) {
        return cInImmix;
      } else {
        return cInImmixCorruptHeader;
      }
    }

    return cUnknown;
  }

  /**
   * Performs a garbage collection of the immix space.
   */
  void ImmixGC::collect(GCData& data) {
    Object* tmp;

    gc_.clear_lines();

    int via_handles_ = 0;
    int via_roots = 0;

    for(Roots::Iterator i(data.roots()); i.more(); i.advance()) {
      tmp = i->get();
      if(tmp->reference_p()) saw_object(tmp);
      via_roots++;
    }

    if(data.threads()) {
      for(std::list<ManagedThread*>::iterator i = data.threads()->begin();
          i != data.threads()->end();
          ++i) {
        scan(*i, false);
      }
    }

    for(capi::Handles::Iterator i(*data.handles()); i.more(); i.advance()) {
      if(i->in_use_p() && !i->weak_p()) {
        saw_object(i->object());
        via_handles_++;
      }
    }

    for(capi::Handles::Iterator i(*data.cached_handles()); i.more(); i.advance()) {
      if(i->in_use_p() && !i->weak_p()) {
        saw_object(i->object());
        via_handles_++;
      }
    }

    std::list<capi::Handle**>* gh = data.global_handle_locations();

    if(gh) {
      for(std::list<capi::Handle**>::iterator i = gh->begin();
          i != gh->end();
          ++i) {
        capi::Handle** loc = *i;
        if(capi::Handle* hdl = *loc) {
          if(!CAPI_REFERENCE_P(hdl)) continue;
          if(hdl->valid_p()) {
            Object* obj = hdl->object();
            if(obj && obj->reference_p()) {
              saw_object(obj);
              via_handles_++;
            }
          } else {
            std::cerr << "Detected bad handle checking global capi handles\n";
          }
        }
      }
    }

    gc_.process_mark_stack(allocator_);

    // We've now finished marking the entire object graph.

    check_finalize();

    // Finalize can cause more things to continue to live, so we must
    // check the mark_stack again.
    while(gc_.process_mark_stack(allocator_)) {
      check_finalize();
    }

    // Sweep up the garbage
    gc_.sweep_blocks();

    // This resets the allocator state to sync it up with the BlockAllocator
    // properly.
    allocator_.get_new_block();

    // Clear unreachable objects from the various remember sets
    int cleared = 0;
    unsigned int mark = object_memory_->mark();
    cleared = object_memory_->unremember_objects(mark);
    for(std::list<gc::WriteBarrier*>::iterator wbi = object_memory_->aux_barriers().begin();
        wbi != object_memory_->aux_barriers().end();
        ++wbi) {
      gc::WriteBarrier* wb = *wbi;
      cleared += wb->unremember_objects(mark);
    }

    // Now, calculate how much space we're still using.
    immix::Chunks& chunks = gc_.block_allocator().chunks();
    immix::AllBlockIterator iter(chunks);

    int live_bytes = 0;
    int total_bytes = 0;

    while(immix::Block* block = iter.next()) {
      total_bytes += immix::cBlockSize;
      live_bytes += block->bytes_from_lines();
    }

    double percentage_live = (double)live_bytes / (double)total_bytes;

    if(object_memory_->state()->shared.config.gc_immix_debug) {
      std::cerr << "[GC IMMIX: " << clear_marked_objects() << " marked"
                << ", "
                << via_roots << " roots "
                << via_handles_ << " handles "
                << (int)(percentage_live * 100) << "% live"
                << ", " << live_bytes << "/" << total_bytes
                << "]\n";
    }

    if(percentage_live >= 0.90) {
      if(object_memory_->state()->shared.config.gc_immix_debug) {
        std::cerr << "[GC IMMIX: expanding. "
                   << (int)(percentage_live * 100)
                   << "%]\n";
      }
      gc_.block_allocator().add_chunk();
    }

#ifdef IMMIX_DEBUG
    std::cout << "Immix: RS size cleared: " << cleared << "\n";

    immix::Chunks& chunks = gc_.block_allocator().chunks();
    std::cout << "chunks=" << chunks.size() << "\n";

    immix::AllBlockIterator iter(chunks);

    int blocks_seen = 0;
    int total_objects = 0;
    int total_object_bytes = 0;

    while(immix::Block* block = iter.next()) {
      blocks_seen++;
      std::cout << "block " << block << ", holes=" << block->holes() << " "
                << "objects=" << block->objects() << " "
                << "object_bytes=" << block->object_bytes() << " "
                << "frag=" << block->fragmentation_ratio()
                << "\n";

      total_objects += block->objects();
      total_object_bytes += block->object_bytes();
    }

    std::cout << blocks_seen << " blocks\n";
    std::cout << gc_.bytes_allocated() << " bytes allocated\n";
    std::cout << total_object_bytes << " object bytes / " << total_objects << " objects\n";

    int* holes = new int[10];
    for(int i = 0; i < 10; i++) {
      holes[i] = 0;
    }

    immix::AllBlockIterator iter2(chunks);

    while(immix::Block* block = iter2.next()) {
      int h = block->holes();
      if(h > 9) h = 9;

      holes[h]++;
    }

    std::cout << "== hole stats ==\n";
    for(int i = 0; i < 10; i++) {
      if(holes[i] > 0) {
        std::cout << i << ": " << holes[i] << "\n";
      }
    }

    delete[] holes;
    holes = NULL;
#endif
  }

  void ImmixGC::check_finalize() {
    // If finalizers are running right now, just fixup any finalizer references
    if(object_memory_->running_finalizers()) {
      for(std::list<FinalizeObject>::iterator i = object_memory_->finalize().begin();
          i != object_memory_->finalize().end();
          ++i) {
        if(i->object) {
          i->object = saw_object(i->object);
        }

        if(i->ruby_finalizer) {
          i->ruby_finalizer = saw_object(i->ruby_finalizer);
        }
      }
      return;
    }

    for(std::list<FinalizeObject>::iterator i = object_memory_->finalize().begin();
        i != object_memory_->finalize().end(); )
    {

      FinalizeObject& fi = *i;

      if(i->ruby_finalizer) {
        i->ruby_finalizer = saw_object(i->ruby_finalizer);
      }

      bool remove = false;

      switch(i->status) {
      case FinalizeObject::eLive:
        if(!i->object->marked_p(object_memory_->mark())) {
          // Run C finalizers now rather that queue them.
          if(i->finalizer) {
            (*i->finalizer)(state(), i->object);
            i->status = FinalizeObject::eFinalized;
            remove = true;
          } else {
            i->queued();
            object_memory_->add_to_finalize(&fi);

            // We have to still keep it alive though until we finish with it.
            i->object = saw_object(i->object);
          }
        } else {
          // Update the reference
          i->object = saw_object(i->object);
        }

        break;
      case FinalizeObject::eQueued:
        // Nothing, we haven't gotten to it yet.
        // Keep waiting and keep i->object updated.
        i->object = saw_object(i->object);
        i->queue_count++;
        break;
      case FinalizeObject::eFinalized:
        if(!i->object->marked_p(object_memory_->mark())) {
          // finalized and done with.
          remove = true;
        } else {
          // RESURRECTION!
          i->queued();
          i->object = saw_object(i->object);
        }
        break;
      }

      if(remove) {
        i = object_memory_->finalize().erase(i);
      } else {
        ++i;
      }
    }
  }

}


text:
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//! A safe wrapper for DOM nodes that prevents layout from mutating the DOM, from letting DOM nodes
//! escape, and from generally doing anything that it isn't supposed to. This is accomplished via
//! a simple whitelist of allowed operations, along with some lifetime magic to prevent nodes from
//! escaping.
//!
//! As a security wrapper is only as good as its whitelist, be careful when adding operations to
//! this list. The cardinal rules are:
//!
//! 1. Layout is not allowed to mutate the DOM.
//!
//! 2. Layout is not allowed to see anything with `LayoutDom` in the name, because it could hang
//!    onto these objects and cause use-after-free.
//!
//! When implementing wrapper functions, be careful that you do not touch the borrow flags, or you
//! will race and cause spurious thread failure. (Note that I do not believe these races are
//! exploitable, but they'll result in brokenness nonetheless.)
//!
//! Rules of the road for this file:
//!
//! * Do not call any methods on DOM nodes without checking to see whether they use borrow flags.
//!
//!   o Instead of `get_attr()`, use `.get_attr_val_for_layout()`.
//!
//!   o Instead of `html_element_in_html_document()`, use
//!     `html_element_in_html_document_for_layout()`.

#![allow(unsafe_code)]

use atomic_refcell::{AtomicRef, AtomicRefMut};
use data::{LayoutData, LayoutDataFlags, StyleAndLayoutData};
use script_layout_interface::wrapper_traits::{ThreadSafeLayoutElement, ThreadSafeLayoutNode};
use script_layout_interface::wrapper_traits::GetLayoutData;
use style::computed_values::content::{self, ContentItem};
use style::dom::{NodeInfo, TNode};
use style::selector_parser::RestyleDamage;

pub trait LayoutNodeLayoutData {
    /// Similar to borrow_data*, but returns the full PersistentLayoutData rather
    /// than only the style::data::ElementData.
    fn borrow_layout_data(&self) -> Option<AtomicRef<LayoutData>>;
    fn mutate_layout_data(&self) -> Option<AtomicRefMut<LayoutData>>;
    fn flow_debug_id(self) -> usize;
}

impl<T: GetLayoutData> LayoutNodeLayoutData for T {
    fn borrow_layout_data(&self) -> Option<AtomicRef<LayoutData>> {
        self.get_raw_data().map(|d| d.layout_data.borrow())
    }

    fn mutate_layout_data(&self) -> Option<AtomicRefMut<LayoutData>> {
        self.get_raw_data().map(|d| d.layout_data.borrow_mut())
    }

    fn flow_debug_id(self) -> usize {
        self.borrow_layout_data().map_or(0, |d| d.flow_construction_result.debug_id())
    }
}

pub trait GetRawData {
    fn get_raw_data(&self) -> Option<&StyleAndLayoutData>;
}

impl<T: GetLayoutData> GetRawData for T {
    fn get_raw_data(&self) -> Option<&StyleAndLayoutData> {
        self.get_style_and_layout_data().map(|opaque| {
            let container = opaque.ptr.get() as *mut StyleAndLayoutData;
            unsafe { &*container }
        })
    }
}

pub trait ThreadSafeLayoutNodeHelpers {
    /// Returns the layout data flags for this node.
    fn flags(self) -> LayoutDataFlags;

    /// Adds the given flags to this node.
    fn insert_flags(self, new_flags: LayoutDataFlags);

    /// Removes the given flags from this node.
    fn remove_flags(self, flags: LayoutDataFlags);

    /// If this is a text node, generated content, or a form element, copies out
    /// its content. Otherwise, panics.
    ///
    /// FIXME(pcwalton): This might have too much copying and/or allocation. Profile this.
    fn text_content(&self) -> TextContent;

    /// The RestyleDamage from any restyling, or RestyleDamage::rebuild_and_reflow() if this
    /// is the first time layout is visiting this node. We implement this here, rather than
    /// with the rest of the wrapper layer, because we need layout code to determine whether
    /// layout has visited the node.
    fn restyle_damage(self) -> RestyleDamage;
}

impl<T: ThreadSafeLayoutNode> ThreadSafeLayoutNodeHelpers for T {
    fn flags(self) -> LayoutDataFlags {
            self.borrow_layout_data().as_ref().unwrap().flags
    }

    fn insert_flags(self, new_flags: LayoutDataFlags) {
        self.mutate_layout_data().unwrap().flags.insert(new_flags);
    }

    fn remove_flags(self, flags: LayoutDataFlags) {
        self.mutate_layout_data().unwrap().flags.remove(flags);
    }

    fn text_content(&self) -> TextContent {
        if self.get_pseudo_element_type().is_replaced_content() {
            let style = self.as_element().unwrap().resolved_style();

            return match style.as_ref().get_counters().content {
                content::T::Items(ref value) if !value.is_empty() => {
                    TextContent::GeneratedContent((*value).clone())
                }
                _ => TextContent::GeneratedContent(vec![]),
            };
        }

        return TextContent::Text(self.node_text_content());
    }

    fn restyle_damage(self) -> RestyleDamage {
        // We need the underlying node to potentially access the parent in the
        // case of text nodes. This is safe as long as we don't let the parent
        // escape and never access its descendants.
        let mut node = unsafe { self.unsafe_get() };

        // If this is a text node, use the parent element, since that's what
        // controls our style.
        if node.is_text_node() {
            node = node.parent_node().unwrap();
            debug_assert!(node.is_element());
        }

        let damage = {
            let data = node.get_raw_data().unwrap();

            if !data.layout_data.borrow().flags.contains(::data::HAS_BEEN_TRAVERSED) {
                // We're reflowing a node that was styled for the first time and
                // has never been visited by layout. Return rebuild_and_reflow,
                // because that's what the code expects.
                RestyleDamage::rebuild_and_reflow()
            } else {
                data.style_data.element_data.borrow().damage
            }
        };

        damage
    }

}

pub enum TextContent {
    Text(String),
    GeneratedContent(Vec<ContentItem>),
}

impl TextContent {
    pub fn is_empty(&self) -> bool {
        match *self {
            TextContent::Text(_) => false,
            TextContent::GeneratedContent(ref content) => content.is_empty(),
        }
    }
}


text:
require('./sankey.css');
// JS
var px = window.px || require('../javascripts/modules/caravel.js');
var d3 = window.d3 || require('d3');
d3.sankey = require('d3-sankey').sankey;

function sankeyVis(slice) {
  var div = d3.select(slice.selector);

  var render = function () {
    var margin = {
      top: 5,
      right: 5,
      bottom: 5,
      left: 5
    };
    var width = slice.width() - margin.left - margin.right;
    var height = slice.height() - margin.top - margin.bottom;

    var formatNumber = d3.format(",.0f"),
      format = function (d) {
        return formatNumber(d) + " TWh";
      };

    var svg = div.append("svg")
      .attr("width", width + margin.left + margin.right)
      .attr("height", height + margin.top + margin.bottom)
      .append("g")
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

    var sankey = d3.sankey()
      .nodeWidth(15)
      .nodePadding(10)
      .size([width, height]);

    var path = sankey.link();

    d3.json(slice.jsonEndpoint(), function (error, json) {
      if (error !== null) {
        slice.error(error.responseText);
        return '';
      }
      var links = json.data;
      var nodes = {};
      // Compute the distinct nodes from the links.
      links.forEach(function (link) {
        link.source = nodes[link.source] || (nodes[link.source] = { name: link.source });
        link.target = nodes[link.target] || (nodes[link.target] = { name: link.target });
        link.value = Number(link.value);
      });
      nodes = d3.values(nodes);

      sankey
        .nodes(nodes)
        .links(links)
        .layout(32);

      var link = svg.append("g").selectAll(".link")
        .data(links)
        .enter().append("path")
        .attr("class", "link")
        .attr("d", path)
        .style("stroke-width", function (d) {
          return Math.max(1, d.dy);
        })
        .sort(function (a, b) {
          return b.dy - a.dy;
        });

      link.append("title")
        .text(function (d) {
          return d.source.name + " → " + d.target.name + "\n" + format(d.value);
        });

      var node = svg.append("g").selectAll(".node")
        .data(nodes)
        .enter().append("g")
        .attr("class", "node")
        .attr("transform", function (d) {
          return "translate(" + d.x + "," + d.y + ")";
        })
        .call(d3.behavior.drag()
          .origin(function (d) {
            return d;
          })
          .on("dragstart", function () {
            this.parentNode.appendChild(this);
          })
          .on("drag", dragmove));

      node.append("rect")
        .attr("height", function (d) {
          return d.dy;
        })
        .attr("width", sankey.nodeWidth())
        .style("fill", function (d) {
          d.color = px.color.category21(d.name.replace(/ .*/, ""));
          return d.color;
        })
        .style("stroke", function (d) {
          return d3.rgb(d.color).darker(2);
        })
        .append("title")
        .text(function (d) {
          return d.name + "\n" + format(d.value);
        });

      node.append("text")
        .attr("x", -6)
        .attr("y", function (d) {
          return d.dy / 2;
        })
        .attr("dy", ".35em")
        .attr("text-anchor", "end")
        .attr("transform", null)
        .text(function (d) {
          return d.name;
        })
        .filter(function (d) {
          return d.x < width / 2;
        })
        .attr("x", 6 + sankey.nodeWidth())
        .attr("text-anchor", "start");

      function dragmove(d) {
        d3.select(this).attr("transform", "translate(" + d.x + "," + (d.y = Math.max(0, Math.min(height - d.dy, d3.event.y))) + ")");
        sankey.relayout();
        link.attr("d", path);
      }
      slice.done(json);
    });
  };
  return {
    render: render,
    resize: render
  };
}

module.exports = sankeyVis;


text:
namespace mx
{
    namespace core
    {

        MX_FORWARD_DECLARE_ATTRIBUTES( InterchangeableAttributes )

        struct InterchangeableAttributes : public AttributesInterface
        {
        public:
            InterchangeableAttributes();
            virtual bool hasValues() const;
            virtual std::ostream& toStream( std::ostream& os ) const;
            TimeSymbol symbol;
            TimeSeparator separator;
            bool hasSymbol;
            bool hasSeparator;

            private:
            virtual bool fromXElementImpl( std::ostream& message, ::ezxml::XElement& xelement );
        };
    }
}


text:
/*
 * NgramStats.cc --
 *	N-gram counting
 *
 */

#ifndef _NgramStats_cc_
#define _NgramStats_cc_

#ifndef lint
static char NgramStats_Copyright[] = "Copyright (c) 1995-2010 SRI International.  All Rights Reserved.";
static char NgramStats_RcsId[] = "@(#)$Header: /home/srilm/devel/lm/src/RCS/NgramStats.cc,v 1.61 2010/06/02 06:22:48 stolcke Exp $";
#endif

#ifdef PRE_ISO_CXX
# include <iostream.h>
#else
# include <iostream>
using namespace std;
#endif
#include <string.h>
#include <stdio.h>
#include <errno.h>

const unsigned maxLineLength = 10000;
#ifdef INSTANTIATE_TEMPLATES
static
#endif
const char *NgramStats_BinaryFormatString = "SRILM_BINARY_COUNTS_001\n";

#include "NgramStats.h"

#include "Trie.cc"
#include "LHash.cc"
#include "Array.cc"
#include "SArray.h"	// for SArray_compareKey()
#include "zio.h"

#if defined(sgi) || defined(_MSC_VER) || defined(WIN32) || defined(linux) && defined(__INTEL_COMPILER) && __INTEL_COMPILER<=700
#define fseeko fseek
#define ftello ftell
#endif

#define INSTANTIATE_NGRAMCOUNTS(CountT) \
	INSTANTIATE_TRIE(VocabIndex,CountT); \
	template class NgramCounts<CountT>

/*
 * Debug levels used
 */
#define DEBUG_READ_GOOGLE	1

template <class CountT>
void
NgramCounts<CountT>::dump()
{
    cerr << "order = " << order << endl;
    counts.dump();
}

template <class CountT>
void
NgramCounts<CountT>::memStats(MemStats &stats)
{
    stats.total += sizeof(*this) - sizeof(counts) - sizeof(vocab);
    vocab.memStats(stats);
    counts.memStats(stats);
}

template <class CountT>
NgramCounts<CountT>::NgramCounts(Vocab &vocab, unsigned int maxOrder)
    : LMStats(vocab), intersect(false), order(maxOrder)
{
}

template <class CountT>
unsigned int
NgramCounts<CountT>::countSentence(const VocabString *words, const char *factor)
{
    CountT factorCount;

    /*
     * Parse the weight string as a count
     */
    if (!stringToCount(factor, factorCount)) {
	return 0;
    }

    return countSentence(words, factorCount);
}

template <class CountT>
unsigned int
NgramCounts<CountT>::countSentence(const VocabString *words, CountT factor)
{
    static VocabIndex wids[maxWordsPerLine + 3];
    unsigned int howmany;

    if (openVocab) {
	howmany = vocab.addWords(words, wids + 1, maxWordsPerLine + 1);
    } else {
	howmany = vocab.getIndices(words, wids + 1, maxWordsPerLine + 1,
					    vocab.unkIndex());
    }

    /*
     * Check for buffer overflow
     */
    if (howmany == maxWordsPerLine + 1) {
	return 0;
    }

    /*
     * update OOV count
     */
    if (!openVocab) {
	for (unsigned i = 1; i <= howmany; i++) {
	    if (wids[i] == vocab.unkIndex()) {
		stats.numOOVs ++;
	    }
	}
    }

    /*
     * Insert begin/end sentence tokens if necessary
     */
    VocabIndex *start;

    if (addSentStart && wids[1] != vocab.ssIndex()) {
	wids[0] = vocab.ssIndex();
	start = wids;
    } else {
	start = wids + 1;
    }

    if (addSentEnd && wids[howmany] != vocab.seIndex()) {
	wids[howmany + 1] = vocab.seIndex();
	wids[howmany + 2] = Vocab_None;
    }

    return countSentence(start, factor);
}

/*
 * Incrememt counts indexed by words, starting at node.
 */
template <class CountT>
void
NgramCounts<CountT>::incrementCounts(const VocabIndex *words,
					unsigned minOrder, CountT factor)
{
    NgramNode *node = &counts;

    for (unsigned i = 0; i < order; i++) {
	VocabIndex wid = words[i];

	/*
	 * check of end-of-sentence
	 */
        if (wid == Vocab_None) {
	    break;
	} else {
	    node = node->insertTrie(wid);
	    if (i + 1 >= minOrder) {
		node->value() += factor;
	    }
	}
    }
}

template <class CountT>
unsigned int
NgramCounts<CountT>::countSentence(const VocabIndex *words, CountT factor)
{
    unsigned int start;

    for (start = 0; words[start] != Vocab_None; start++) {
        incrementCounts(words + start, 1, factor);
    }

    /*
     * keep track of word and sentence counts
     */
    stats.numWords += start;
    if (words[0] == vocab.ssIndex()) {
	stats.numWords --;
    }
    if (start > 0 && words[start-1] == vocab.seIndex()) {
	stats.numWords --;
    }

    stats.numSentences ++;

    return start;
}

template <class CountT>
unsigned int
NgramCounts<CountT>::parseNgram(char *line,
		      VocabString *words,
		      unsigned int max,
		      CountT &count)
{
    unsigned howmany = Vocab::parseWords(line, words, max);

    if (howmany == max) {
	return 0;
    }

    /*
     * Parse the last word as a count
     */
    if (!stringToCount(words[howmany - 1], count)) {
	return 0;
    }

    howmany --;
    words[howmany] = 0;

    return howmany;
}

template <class CountT>
unsigned int
NgramCounts<CountT>::readNgram(File &file,
		      VocabString *words,
		      unsigned int max,
		      CountT &count)
{
    char *line;

    /*
     * Read next ngram count from file, skipping blank lines
     */
    line = file.getline();
    if (line == 0) {
	return 0;
    }

    unsigned howmany = parseNgram(line, words, max, count);

    if (howmany == 0) {
	file.position() << "malformed N-gram count or more than " << max - 1 << " words per line\n";
	return 0;
    }

    return howmany;
}

template <class CountT>
Boolean
NgramCounts<CountT>::read(File &file, unsigned int order, Boolean limitVocab)
{
    VocabString words[maxNgramOrder + 1];
    VocabIndex wids[maxNgramOrder + 1];
    CountT count;
    unsigned int howmany;

    /*
     * Check for binary format
     */
    char *firstLine = file.getline();

    if (!firstLine) {
    	return true;
    } else {
	if (strcmp(firstLine, NgramStats_BinaryFormatString) == 0) {
	    File binaryFile(file.name, "rb");
	    return readBinary(binaryFile, order, limitVocab);
	} else {
	    file.ungetline();
	}
    }

    while ((howmany = readNgram(file, words, maxNgramOrder + 1, count))) {
	/*
	 * Skip this entry if the length of the ngram exceeds our
	 * maximum order
	 */
	if (howmany > order) {
	    continue;
	}
	/*
	 * Map words to indices
	 */
	if (limitVocab) {
	    /*
	     * skip ngram if not in-vocabulary
	     */
	    if (!vocab.checkWords(words, wids, maxNgramOrder)) {
	    	continue;
	    }
	} else if (openVocab) {
	    vocab.addWords(words, wids, maxNgramOrder);
	} else {
	    vocab.getIndices(words, wids, maxNgramOrder, vocab.unkIndex());
	}

	/*
	 *  Update the count
	 */
        CountT *cnt = intersect ?
			counts.find(wids) :
			counts.insert(wids);

	if (cnt) {
	    *cnt += count;
	}
    }
    /*
     * XXX: always return true for now, should return false if there was
     * a format error in the input file.
     */
    return true;
}

/*
 * Binary count format:

 	magic string \n
	"maxorder" N \n
	index1 word1 \n
	index2 word2 \n
	...
	indexN wordN \n
	"." \n
	binary-count-trie
 */
template <class CountT>
Boolean
NgramCounts<CountT>::readBinary(File &file, unsigned order, Boolean limitVocab)
{
    char *firstLine = file.getline();

    if (!firstLine || strcmp(firstLine, NgramStats_BinaryFormatString) != 0) {
	file.position() << "bad binary format\n";
	return false;
    }

    /*
     * Maximal count order
     */
    unsigned maxOrder;
    if (fscanf(file, "maxorder %u", &maxOrder) != 1) {
    	file.position() << "could not read ngram order\n";
	return false;
    }

    /*
     * Vocabulary map
     */
    Array<VocabIndex> vocabMap;

    if (!vocab.readIndexMap(file, vocabMap, limitVocab)) {
	return false;
    }

    long long offset = ftello(file);

    // detect if file is not seekable
    if (offset < 0) {
	file.position() << strerror(errno) << endl;
	return false;
    }

    /*
     * Count data
     */
    return readBinaryNode(counts, order, maxOrder, file, offset, limitVocab, vocabMap);
}

/*
 * Binary count-trie format:

	length-of-binary-trie (unsigned long long)
	word1
	count1
	[subtrie1]
	word2
	count2
	[subtrie2]
	...
 */
template <class CountT>
Boolean
NgramCounts<CountT>::readBinaryNode(NgramNode &node,
					unsigned order, unsigned maxOrder,
					File &file, long long &offset,
					Boolean limitVocab,
					Array<VocabIndex> &vocabMap)
{
    if (maxOrder == 0) {
    	return true;
    } else {
	long long endOffset;
	unsigned long long trieLength;
	unsigned nbytes;

	nbytes = readBinaryCount(file, trieLength);
	if (!nbytes) {
	    return false;
	}
	endOffset = offset + trieLength;
	offset += nbytes;

	if (order == 0) {
	    if (fseeko(file, endOffset, SEEK_SET) < 0) {
		file.offset() << strerror(errno) << endl;
		return false;
	    }
	    offset = endOffset;
	} else {
	    while (offset < endOffset) {
		VocabIndex oldWid;

		nbytes = readBinaryCount(file, oldWid);
		if (!nbytes) {
		    return false;
		}
		offset += nbytes;

		if (oldWid >= vocabMap.size()) {
		    file.offset() << "word index " << oldWid
		                  << " out of range\n";
		    return false;
		}
		VocabIndex wid = vocabMap[oldWid];
		NgramNode *child = 0;

		if (wid != Vocab_None) {
		    child = intersect ?
				node.findTrie(wid) :
				node.insertTrie(wid);
		}

		if (child == 0) {
		    // skip count value and subtrie
		    CountT dummy;
		    nbytes = readBinaryCount(file, dummy);
		    if (!nbytes) {
		    	return false;
		    }
		    offset += nbytes;

		    if (!readBinaryNode(node, 0, maxOrder-1, file, offset,
							limitVocab, vocabMap)) {
			return false;
		    }
		} else {
		    // read count value and subtrie
		    CountT count;
		    nbytes = readBinaryCount(file, count);
		    if (!nbytes) {
			return false;
		    }
		    child->value() += count;
		    offset += nbytes;

		    if (!readBinaryNode(*child, order-1, maxOrder-1,
					file, offset, limitVocab, vocabMap)) {
			return false;
		    }
		}
	    }

	    if (offset != endOffset) {
	    	file.offset() << "data misaligned\n";
		return false;
	    }
	}

	return true;
    }
}

/*
 * Read counts stored in an indexed directory structure as proposed by
 * Thorsten Brandts at Google.  From the documentation:
 *
 * a) top-level directory
 *    doc: documentation
 *    data: data [this is the directory passed as argument]
 *    (the top-level structure is required by LDC)
 * b) data directory
 *    one sub-directory per n-gram order: 1gms, 2gms, 3gms, 4gms, 5gms
 *    (separating the orders makes it easier for people to use smaller orders)
 * c) contents of sub-directory 1gms
 *    - file 'vocab.gz' contains the vocabulary sorted by word in unix
 *      sort-order. Each word is on its own line:
 *      WORD <tab> COUNT
 *    - file 'vocab_cs.gz' contains the same data as 'vocab.gz' but
 *      sorted by count.
 *    (need to be 8+3 file names)
 * d) contents of sub-directories 2gms, 3gms, 4gms, 5gms:
 *    - files 'Ngm-KKKK.gz' where N is the order of the n-grams
 *      and KKKK is the zero-padded number of the file. Each file contains
 *      10 million n-gram entries. N-grams are unix-sorted. Each
 *      n-gram occupies one line:
 *      WORD1 <space> WORD2 <space> ... WORDN <tab> COUNT
 *    - file 'Ngm.idx' where N is the order of the n-grams, with one line for
 *      each n-gram file:
 *      FILENAME <tab> FIRST_NGRAM_IN_FILE
 */
template <class CountT>
Boolean
NgramCounts<CountT>::readGoogle(const char *dir, unsigned order,
							Boolean limitVocab)
{
    makeArray(char, filename, strlen(dir) + 20);

    {
	sprintf(filename, "%s/1gms/vocab%s", dir, GZIP_SUFFIX);

	File countFile(filename, "r", 0);

	if (countFile.error() && sizeof(GZIP_SUFFIX) > 1) {
	    // also try uncompressed vocab file
	    sprintf(filename, "%s/1gms/vocab", dir);

	    countFile.reopen(filename, "r");
	}

	if (countFile.error()) {
	    perror(filename);
	    return false;
	}

	if (debug(DEBUG_READ_GOOGLE)) {
	    dout() << "reading " << filename << endl;
	}

	if (!read(countFile, 1, limitVocab)) {
	    return false;
	}
    }

    for (unsigned i = 2; i <= order; i ++) {

	/*
	 * Read index file
	 */

	sprintf(filename, "%s/%dgms/%dgm.idx", dir, i, i);
    	File indexFile(filename, "r", 0);
	if (indexFile.error()) {
	    perror(filename);
	    return false;
	}

	unsigned numEntries = 0;
	Array<VocabString *> indexEntries;

	while (char *line = indexFile.getline()) {
	    char *savedLine = strdup(line);
	    assert(savedLine != 0);

	    VocabString *indexEntry = new VocabString[i + 3];
	    assert(indexEntry != 0);

	    indexEntry[0] = savedLine;
	    unsigned howmany =
	    		Vocab::parseWords(savedLine, &indexEntry[1], i + 2);

	    if (howmany != i + 1) {
		indexFile.position() << "malformed index entry\n";
		return false;
	    }
	    indexEntries[numEntries++] = indexEntry;
	}

	if (indexFile.error()) {
	    perror(filename);
	    return false;
	}

	/*
	 * Read N-gram counts selectively
	 */

	for (unsigned k = 0; k < numEntries; k ++) {
	    sprintf(filename, "%s/%dgms/%s", dir, i, indexEntries[k][1]);

	    File countFile(filename, "r", 0);
	    if (countFile.error()) {
		perror(filename);
		return false;
	    }

	    /*
	     * If limitVocab is in effect check if any in-vocabulary ngrams
	     * are in this count file
	     */
	    if (!limitVocab ||
	        vocab.ngramsInRange(&indexEntries[k][2],
			k == numEntries - 1 ? 0 : &indexEntries[k+1][2]))
	    {
		if (debug(DEBUG_READ_GOOGLE)) {
		    dout() << "reading " << filename << endl;
		}

		if (!read(countFile, i, limitVocab)) {
		    return false;
		}
	    }

	    if (countFile.error()) {
		perror(filename);
		return false;
	    }

	    free((char *)indexEntries[k][0]);
	    delete [] indexEntries[k];
	}
    }

    return true;
}

/*
 * Helper function to record a set of meta-counts at a given prefix into
 * the ngram tree
 */
template <class CountT>
void
NgramCounts<CountT>::addCounts(const VocabIndex *prefix,
				const LHash<VocabIndex, CountT> &set)
{
    NgramNode *node = intersect ?
			counts.findTrie(prefix) :
			counts.insertTrie(prefix);

    if (node) {
	LHashIter<VocabIndex, CountT> setIter(set);
	VocabIndex word;
	CountT *count;
	while ((count = setIter.next(word))) {
	    *node->insert(word) += *count;
	}
    }
}

/*
 * Read a counts file discarding counts below given minimum counts
 * Assumes that counts are merged, i.e., ngram order is generated by a
 * pre-order traversal of the ngram tree.
 */
template <class CountT>
Boolean
NgramCounts<CountT>::readMinCounts(File &file, unsigned order,
					Count *minCounts, Boolean limitVocab)
{
    /*
     * Check for binary format
     */
    char *firstLine = file.getline();

    if (!firstLine) {
    	return true;
    } else {
	if (strcmp(firstLine, NgramStats_BinaryFormatString) == 0) {
	    cerr << "binary format not yet support in readMinCounts\n";
	    return false;
	} else {
	    file.ungetline();
	}
    }

    VocabString words[maxNgramOrder + 1];
    VocabIndex prefix[maxNgramOrder + 1];

    /*
     * Data for tracking deletable prefixes
     */
    LHash<VocabIndex, CountT> *metaCounts;

    metaCounts = new LHash<VocabIndex, CountT>[order];
    assert(metaCounts != 0);

    makeArray(Boolean, haveCounts, order);
    makeArray(VocabIndex *, lastPrefix, order);
    for (unsigned i = 0; i < order; i ++) {
	lastPrefix[i] = new VocabIndex[order + 1];
	assert(lastPrefix[i] != 0);
	lastPrefix[i][0] = Vocab_None;
	haveCounts[i] = false;
    }

    Vocab::compareVocab = 0;		// do comparison by word index

    CountT count;
    unsigned int howmany;

    while ((howmany = readNgram(file, words, maxNgramOrder + 1, count))) {
	/*
	 * Skip this entry if the length of the ngram exceeds our
	 * maximum order
	 */
	if (howmany > order) {
	    continue;
	}

	VocabIndex metaTag = Vocab_None;
	Boolean haveRealCount = false;

	/*
	 * Discard counts below mincount threshold
	 */
	if (count < minCounts[howmany - 1]) {
	    if ((unsigned)count == 0) {
		continue;
	    } else {
		/*
		 * See if metatag is defined, and if so, keep going
		 */
	    	metaTag = vocab.metaTagOfType((unsigned)count);

		if (metaTag == Vocab_None) {
		    continue;
		}
	    }
	}

	/*
	 * Map words to indices (includes both N-gram prefix and last word)
	 */
	if (limitVocab) {
	    /*
	     * skip ngram if not in-vocabulary
	     */
	    if (!vocab.checkWords(words, prefix, maxNgramOrder)) {
	    	continue;
	    }
	} else if (openVocab) {
	    vocab.addWords(words, prefix, maxNgramOrder);
	} else {
	    vocab.getIndices(words, prefix, maxNgramOrder, vocab.unkIndex());
	}

	/*
	 * Truncate Ngram for prefix comparison
	 */
	VocabIndex lastWord = prefix[howmany - 1];
	prefix[howmany - 1] = Vocab_None;

	/*
	 * If current ngram prefix differs from previous one assume that
	 * we are done with all ngrams of that prefix (this assumption is
	 * true for counts in prefix order).
	 * In this case, output saved meta-counts for the previous prefix,
	 * but only if there also were some real counts -- this saves a lot
	 * of space in the counts tree.
	 */
	if (Vocab::compare(prefix, lastPrefix[howmany-1]) != 0) {
	    if (haveCounts[howmany-1]) {
		addCounts(lastPrefix[howmany-1], metaCounts[howmany-1]);
		haveCounts[howmany-1] = false;
	    }
	    metaCounts[howmany-1].clear();
	    Vocab::copy(lastPrefix[howmany-1], prefix);
	}

	/*
	 *  Update the count (either meta or real)
	 */
	if (metaTag != Vocab_None) {
	    *metaCounts[howmany-1].insert(metaTag) += 1;
	} else {
	    CountT *cnt = intersect ?
				findCount(prefix, lastWord) :
				insertCount(prefix, lastWord);
	    if (cnt) {
		*cnt += count;
	    }
	    haveCounts[howmany-1] = true;
	}
    }

    /*
     * Record final saved meta-counts and deallocate prefix buffers
     */
    for (unsigned i = order; i > 0; i --) {
	if (haveCounts[i-1]) {
	    addCounts(lastPrefix[i-1], metaCounts[i-1]);
	}
	delete [] lastPrefix[i-1];
    }

    delete [] metaCounts;

    /*
     * XXX: always return true for now, should return false if there was
     * a format error in the input file.
     */
    return true;
}

template <class CountT>
unsigned int
NgramCounts<CountT>::writeNgram(File &file,
		       const VocabString *words,
		       CountT count)
{
    unsigned int i;

    if (words[0]) {
	fprintf(file, "%s", words[0]);
	for (i = 1; words[i]; i++) {
	    fprintf(file, " %s", words[i]);
	}
    }
    fprintf(file, "\t%s\n", countToString(count));

    return i;
}

/*
 * For reasons of efficiency the write() method doesn't use
 * writeNgram()  (yet).  Instead, it fills up a string buffer
 * as it descends the tree recursively.  this avoid having to
 * lookup shared prefix words and buffer the corresponding strings
 * repeatedly.
 */
template <class CountT>
void
NgramCounts<CountT>::writeNode(
    NgramNode &node,		/* the trie node we're at */
    File &file,			/* output file */
    char *buffer,		/* output buffer */
    char *bptr,			/* pointer into output buffer */
    unsigned int level,		/* current trie level */
    unsigned int order,		/* target trie level */
    Boolean sorted)		/* produce sorted output */
{
    NgramNode *child;
    VocabIndex wid;

    TrieIter<VocabIndex,CountT> iter(node, sorted ? vocab.compareIndex() : 0);

    /*
     * Iterate over the child nodes at the current level,
     * appending their word strings to the buffer
     */
    while (!file.error() && (child = iter.next(wid))) {
	VocabString word = vocab.getWord(wid);

	if (word == 0) {
	   cerr << "undefined word index " << wid << "\n";
	   continue;
	}

	unsigned wordLen = strlen(word);

	if (bptr + wordLen + 1 > buffer + maxLineLength) {
	   *bptr = '0';
	   cerr << "ngram ["<< buffer << word
		<< "] exceeds write buffer\n";
	   continue;
	}

	strcpy(bptr, word);

	/*
	 * If this is the final level, print out the ngram and the count.
	 * Otherwise set up another level of recursion.
	 */
	if (order == 0 || level == order) {
	   fprintf(file, "%s\t%s\n", buffer, countToString(child->value()));
	}

	if (order == 0 || level < order) {
	   *(bptr + wordLen) = ' ';
	   writeNode(*child, file, buffer, bptr + wordLen + 1, level + 1,
			order, sorted);
	}
    }
}

template <class CountT>
void
NgramCounts<CountT>::write(File &file, unsigned int order, Boolean sorted)
{
    static char buffer[maxLineLength];
    writeNode(counts, file, buffer, buffer, 1, order, sorted);
}

template <class CountT>
Boolean
NgramCounts<CountT>::writeBinary(File &file, unsigned int order)
{
    /*
     * Magic string
     */
    fprintf(file, "%s", NgramStats_BinaryFormatString);

    /*
     * Maximal count order
     */
    fprintf(file, "maxorder %u\n", order > 0 ? order : this->order);

    /*
     * Vocabulary index
     */
    vocab.writeIndexMap(file);

    long long offset = ftello(file);

    // detect if file is not seekable
    if (offset < 0) {
	file.position() << strerror(errno) << endl;
	return false;
    }

    /*
     * Count data
     */
    return writeBinaryNode(counts, 1, order, file, offset);
}

template <class CountT>
Boolean
NgramCounts<CountT>::writeBinaryNode(NgramNode &node,
					unsigned level, unsigned order,
					File &file, long long &offset)
{
    unsigned effectiveOrder = order > 0 ? order : this->order;

    if (level > effectiveOrder) {
    	// when reaching the maximal order don't write an offset to save space
    	return true;
    } else {
	// guess number of bytes needed for storing subtrie rooted at node
	// based on its depth (if we guess wrong we need to redo the whole
	// subtrie later)
	unsigned subtrieDepth = effectiveOrder - level;
        unsigned offsetBytes = subtrieDepth == 0 ? 2 :
				subtrieDepth <= 3 ? 4 : 8;

	long long startOffset = offset;	// remember start offset

retry:
	// write placeholder value
	unsigned nbytes = writeBinaryCount(file, (unsigned long long)0,
								offsetBytes);
	if (!nbytes) return false;
	offset += nbytes;

	if (order == 0 || level <= order) {
	    NgramNode *child;
	    // write subtries -- always in index-sorted order to ensure fast
	    // reading regardless of data structure used
#ifdef USE_SARRAY_TRIE
	    TrieIter<VocabIndex,CountT> iter(node);
#else
	    TrieIter<VocabIndex,CountT> iter(node, SArray_compareKey);
#endif
	    VocabIndex wid;

	    while ((child = iter.next(wid))) {
		nbytes = writeBinaryCount(file, wid);
		if (!nbytes) return false;
		offset += nbytes;

		if (order > 0 && level < order) {
		    nbytes = writeBinaryCount(file, (CountT)0);
		} else {
		    nbytes = writeBinaryCount(file, child->value());
		}
		if (!nbytes) return false;
		offset += nbytes;

		if (!writeBinaryNode(*child, level + 1, order, file, offset)) {
		    return false;
		}
	    }
	}

	long long endOffset = offset;

	if (fseeko(file, startOffset, SEEK_SET) < 0) {
	    file.offset() << strerror(errno) << endl;
	    return false;
	}

	// don't update offset since we're skipping back in file
	nbytes = writeBinaryCount(file,
				  (unsigned long long)(endOffset-startOffset),
				  offsetBytes);
	if (!nbytes) return false;

	// now check that the number of bytes used for offset was actually ok
	if (nbytes > offsetBytes) {
	    file.offset() << "increasing offset bytes from " << offsetBytes
	                  << " to " << nbytes
			  << " (order " << effectiveOrder << ","
			  << " level " << level << ")\n";

	    offsetBytes = nbytes;

	    if (fseeko(file, startOffset, SEEK_SET) < 0) {
		file.offset() << strerror(errno) << endl;
		return false;
	    }
	    offset = startOffset;

	    goto retry;
	}

	if (fseeko(file, endOffset, SEEK_SET) < 0) {
	    file.offset() << strerror(errno) << endl;
	    return false;
	}

	return true;
    }
}

template <class CountT>
CountT
NgramCounts<CountT>::sumNode(NgramNode &node, unsigned level, unsigned order)
{
    /*
     * For leaf nodes, or nodes beyond the maximum level we are summing,
     * return their count, leaving it unchanged.
     * For nodes closer to the root, replace their counts with the
     * the sum of the counts of all the children.
     */
    if (level > order || node.numEntries() == 0) {
	return node.value();
    } else {
	NgramNode *child;
	TrieIter<VocabIndex,CountT> iter(node);
	VocabIndex wid;

	CountT sum = 0;

	while ((child = iter.next(wid))) {
	    sum += sumNode(*child, level + 1, order);
	}

	node.value() = sum;

	return sum;
    }
}

template <class CountT>
CountT
NgramCounts<CountT>::sumCounts(unsigned int order)
{
    return sumNode(counts, 1, order);
}

/*
 * Prune ngram counts
 */
template <class CountT>
unsigned
NgramCounts<CountT>::pruneCounts(CountT minCount)
{
    unsigned npruned = 0;
    makeArray(VocabIndex, ngram, order + 1);

    for (unsigned i = 1; i <= order; i++) {
	CountT *count;
	NgramCountsIter<CountT> countIter(*this, ngram, i);

	/*
	 * This enumerates all ngrams
	 */
	while ((count = countIter.next())) {
	    if (*count < minCount) {
		removeCount(ngram);
		npruned ++;
	    }
	}
    }
    return npruned;
}

/*
 * Set ngram counts to constant value (default zero)
 */
template <class CountT>
void
NgramCounts<CountT>::setCounts(CountT value)
{
    makeArray(VocabIndex, ngram, order + 1);

    for (unsigned i = 1; i <= order; i++) {
	CountT *count;
	NgramCountsIter<CountT> countIter(*this, ngram, i);

	/*
	 * This enumerates all ngrams
	 */
	while ((count = countIter.next())) {
	    *count = value;
	}
    }
}

#endif /* _NgramStats_cc_ */


text:
module Muffin
  module Rails
    SCOPE_ACCESSOR = :operation_auth_scope

    module ControllerAdditions
      def prepare(operation, params: nil, request: nil, scope: nil)
        if params.blank? && respond_to?(:params) && operation.respond_to?(:model_name)
          if self.params.key?(operation.model_name.param_key)
            params = parse_arrays self.params[operation.model_name.param_key].deep_dup.permit!.to_h
            params.deep_transform_keys! do |key|
              if key.to_s[/.+_attributes\Z/]
                new_key = key.to_s.sub(/_attributes\Z/, "")
                key.is_a?(Symbol) ? new_key.to_sym : new_key
              else
                key
              end
            end
          end
        end

        request ||= try(:request)

        scope ||= try(Muffin::Rails::SCOPE_ACCESSOR) || try(:current_user)

        operation.new(params: params, request: request, scope: scope)
      end

      private

      def parse_arrays(obj)
        obj.each do |key, value|
          if value.is_a? Hash
            if value.keys.find { |k, _| k =~ /\D/ }
              parse_arrays(value)
            else
              obj[key] = value.values
              value.each_value { |h| parse_arrays(h) }
            end
          end
        end
      end
    end
  end
end


text:
/*
 * Created on Sep 28, 2006
 */
package org.seqcode.gsebricks;

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

import org.seqcode.genome.Genome;

/**
 * @author tdanford
 *
 * GeneFactoryLoader is basically a Singleton.  It reads
 * a properties file in the classpath and creates GeneFactory objects.
 * Creating a GeneFactory with getFactory requires a
 * Genome and a type; the type generally indicates the set of gene
 * annotations to be accessed.
 *
 * The indirection through GeneFactoryLoader and GeneFactory allows
 * the properties file to specify more parameters than just the type
 */
public class GeneFactoryLoader {

    public static void main(String[] args) {
        GeneFactoryLoader loader = new GeneFactoryLoader();
        for(String genome : loader.getGenomes()) {
            System.out.println(genome);
            for(String type : loader.getTypes(genome)) {
                System.out.println("\t" + type);
            }
        }
    }

    private ResourceBundle gfRes;
    private Map<String,Map<String,GeneFactory>> factories;

    public GeneFactoryLoader() {
        gfRes = ResourceBundle.getBundle("org.seqcode.gseutils.properties.gene_factories");
        factories = new HashMap<String,Map<String,GeneFactory>>();
        Enumeration<String> keys = gfRes.getKeys();
        while(keys.hasMoreElements()) {
            String key = keys.nextElement();
            String[] keyArray = key.split(",");
            String genome = keyArray[0], type = keyArray[1];
            addFactory(genome, type, gfRes.getString(key));
        }
    }

    public GeneFactory getFactory(Genome g, String type) {
        if(!factories.containsKey(g.getVersion())) { return null; }
        if(!factories.get(g.getVersion()).containsKey(type)) { return null; }
        return factories.get(g.getVersion()).get(type);
    }

    public Set<String> getGenomes() { return factories.keySet(); }

    public Set<String> getTypes(Genome g) {
        if(!factories.containsKey(g.getVersion())) { return new HashSet<String>(); }
        return factories.get(g.getVersion()).keySet();
    }

    public Set<String> getTypes(String gversion) {
        if(!factories.containsKey(gversion)) { return new HashSet<String>(); }
        return factories.get(gversion).keySet();
    }

    private void addFactory(String genome, String type, String pathName) {
        ClassLoader loader = ClassLoader.getSystemClassLoader();
        try {
            Class gfClass = loader.loadClass(pathName);
            GeneFactory gf = (GeneFactory)gfClass.newInstance();
            if(!factories.containsKey(genome)) { factories.put(genome, new HashMap<String,GeneFactory>()); }
            Map<String,GeneFactory> genomeMap = factories.get(genome);
            genomeMap.put(type, gf);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}


text:
Puppet::Type.newtype(:cisco_vxlan_vtep) do
  @doc = "Manages VXLAN vtep nve interface configuration.

  ~~~puppet
  cisco_vxlan_vtep { <interface>:
    ..attributes..
  }
  ~~~

  Example:

  ~~~puppet
    cisco_vxlan_vtep { 'nve1':
      ensure                          => present,
      description                     => 'nve interface',
      host_reachability               => 'evpn',
      shutdown                        => false,
      source_interface                => 'loopback1',
      source_interface_hold_down_time => '50',
      global_ingress_replication_bgp  => 'true',
      global_mcast_group_l2           => '225.1.1.1',
      global_mcast_group_l3           => '225.1.1.2',
      global_suppress_arp             => 'true',
    }
  ~~~
  "

  def self.title_patterns
    identity = ->(x) { x }
    patterns = []

    # Below pattern matches both parts of the full composite name.
    patterns << [
      /^(\S+)/,
      [
        [:interface, identity]
      ],
    ]
    patterns
  end

  ##############
  # Parameters #
  ##############

  apply_to_all
  ensurable

  newparam(:interface, namevar: :true) do
    desc 'Name of the nve interface on the network element.
          Valid values are string.'

    validate do |value|
      fail("'Interface name must be a string") unless value.is_a? String
    end

    munge(&:downcase)
  end

  ##############
  # Properties #
  ##############

  newproperty(:description) do
    desc "Description of the NVE interface. Valid values are string,
          and keyword 'default'."

    munge { |value| value == 'default' ? :default : value }
  end

  newproperty(:host_reachability) do
    desc "Specify mechanism for host reachability advertisement. Valid values
          are 'evpn', 'flood', or 'default'"

    newvalues(:evpn, :flood, :default)
  end

  newproperty(:shutdown) do
    desc "Administratively shutdown the NVE interface. Valid values are true,
          false, or 'default'"

    newvalues(:true, :false, :default)
  end

  newproperty(:source_interface) do
    desc "Specify loopback interface whose IP address should be set as the
          IP address for the NVE interface. Valid values are string,
          and keyword 'default'."

    munge do |value|
      value == 'default' ? :default : value.gsub(/\s+/, '').downcase
    end
  end

  newproperty(:multisite_border_gateway_interface) do
    desc "Specify loopback interface to be used as VxLAN Multisite
          Border-gateway interface. Valid values are string,
          and keyword 'default'."

    munge do |value|
      value == 'default' ? :default : value.gsub(/\s+/, '').downcase
    end
  end

  newproperty(:source_interface_hold_down_time) do
    desc "Suppress advertisement of the NVE loopback address until the overlay
          has converged. Valid values are Integer, keyword 'default'."

    munge do |value|
      begin
        value = :default if value == 'default'
        value = Integer(value) unless value == :default
      rescue
        raise 'source_interface_hold_down_time must be an integer.'
      end
      value
    end
  end # source_interface_hold_down_time

  newproperty(:global_ingress_replication_bgp) do
    desc "Sets ingress replication protocol to bgp. Valid values are true,
          false, or 'default'"

    newvalues(:true, :false, :default)
  end

  newproperty(:global_suppress_arp) do
    desc "Enable ARP suppression. Valid values are true,
          false, or 'default'"

    newvalues(:true, :false, :default)
  end

  newproperty(:global_mcast_group_l2) do
    desc "NVE Multicast Group for L2 VNIs. Valid values are string,
          and keyword 'default'."

    munge do |value|
      value == 'default' ? false : value
    end
  end

  newproperty(:global_mcast_group_l3) do
    desc "NVE Multicast Group for L3 VNIs. Valid values are string,
          and keyword 'default'."

    munge do |value|
      value == 'default' ? false : value
    end
  end

  validate do
    # source_interface_hold_down_time can be set only if source_interface is set
    fail 'source_interface_hold_down_time can be used only when source_interface '\
         'is also used' if self[:source_interface_hold_down_time] &&
                           !self[:source_interface]
    fail 'Only one of global_ingress_replication_bgp or global_mcast_group_l2 '\
        'can be configured not both' if self[:global_ingress_replication_bgp] == :true &&
                                        self[:global_mcast_group_l2]
  end
end


text:
#include "20081222_0.h"

#define ASMNAME(cname)  ASMNAME2 (__USER_LABEL_PREFIX__, cname)
#define ASMNAME2(prefix, cname) STRING (prefix) cname
#define STRING(x)    #x

/* Actually, call "x" "INT_X", and make it hidden.  */
extern __typeof (x) x
	__asm__ (ASMNAME ("INT_x"))
	__attribute__ ((__visibility__ ("hidden")));

int x ()
{
  return 7;
}

/* Make an externally-visible symbol "X" that's an alias for INT_x.  */
extern __typeof (x) EXT_x
	__asm__ (ASMNAME ("x"))
	__attribute__ ((__alias__ ("INT_x")));


text:
export RUN_AS_USER="@some_system_user@"
export TERRACOTTA_HOME="@terracotta_home_path@"
export TERRACOTTA_CONFIG_XML="@terracotta_config_home_path@/tc-config.xml"
export REMOTE_JAVA_HOME="@path_to_java_on_remote@"

REMOTE_JAVA_OPTS="-server -Xms1G -Xmx1G -XX:+UseParallelOldGC -XX:+UseCompressedOops"
#REMOTE_JAVA_OPTS="${REMOTE_JAVA_OPTS} -verbose:gc -Xloggc:run.gc.log -XX:+PrintGCDetails -XX:+PrintTenuringDistribution -XX:+PrintGCTimeStamps"
#REMOTE_JAVA_OPTS="${REMOTE_JAVA_OPTS} -Dcom.sun.management.jmxremote.port=7070 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"

export REMOTE_JAVA_OPTS="${REMOTE_JAVA_OPTS}"


text:
#include "include/core/SkMatrix.h"
#include "include/pathops/SkPathOps.h"
#include "src/core/SkArenaAlloc.h"
#include "src/core/SkPathPriv.h"
#include "src/pathops/SkOpEdgeBuilder.h"
#include "src/pathops/SkPathOpsCommon.h"

static bool one_contour(const SkPath& path) {
    SkSTArenaAlloc<256> allocator;
    int verbCount = path.countVerbs();
    uint8_t* verbs = (uint8_t*) allocator.makeArrayDefault<uint8_t>(verbCount);
    (void) path.getVerbs(verbs, verbCount);
    for (int index = 1; index < verbCount; ++index) {
        if (verbs[index] == SkPath::kMove_Verb) {
            return false;
        }
    }
    return true;
}

void SkOpBuilder::ReversePath(SkPath* path) {
    SkPath temp;
    SkPoint lastPt;
    SkAssertResult(path->getLastPt(&lastPt));
    temp.moveTo(lastPt);
    temp.reversePathTo(*path);
    temp.close();
    *path = temp;
}

bool SkOpBuilder::FixWinding(SkPath* path) {
    SkPathFillType fillType = path->getFillType();
    if (fillType == SkPathFillType::kInverseEvenOdd) {
        fillType = SkPathFillType::kInverseWinding;
    } else if (fillType == SkPathFillType::kEvenOdd) {
        fillType = SkPathFillType::kWinding;
    }
    if (one_contour(*path)) {
        SkPathFirstDirection dir = SkPathPriv::ComputeFirstDirection(*path);
        if (dir != SkPathFirstDirection::kUnknown) {
            if (dir == SkPathFirstDirection::kCW) {
                ReversePath(path);
            }
            path->setFillType(fillType);
            return true;
        }
    }
    SkSTArenaAlloc<4096> allocator;
    SkOpContourHead contourHead;
    SkOpGlobalState globalState(&contourHead, &allocator  SkDEBUGPARAMS(false)
            SkDEBUGPARAMS(nullptr));
    SkOpEdgeBuilder builder(*path, &contourHead, &globalState);
    if (builder.unparseable() || !builder.finish()) {
        return false;
    }
    if (!contourHead.count()) {
        return true;
    }
    if (!contourHead.next()) {
        return false;
    }
    contourHead.joinAllSegments();
    contourHead.resetReverse();
    bool writePath = false;
    SkOpSpan* topSpan;
    globalState.setPhase(SkOpPhase::kFixWinding);
    while ((topSpan = FindSortableTop(&contourHead))) {
        SkOpSegment* topSegment = topSpan->segment();
        SkOpContour* topContour = topSegment->contour();
        SkASSERT(topContour->isCcw() >= 0);
#if DEBUG_WINDING
        SkDebugf("%s id=%d nested=%d ccw=%d\n",  __FUNCTION__,
                topSegment->debugID(), globalState.nested(), topContour->isCcw());
#endif
        if ((globalState.nested() & 1) != SkToBool(topContour->isCcw())) {
            topContour->setReverse();
            writePath = true;
        }
        topContour->markAllDone();
        globalState.clearNested();
    }
    if (!writePath) {
        path->setFillType(fillType);
        return true;
    }
    SkPath empty;
    SkPathWriter woundPath(empty);
    SkOpContour* test = &contourHead;
    do {
        if (!test->count()) {
            continue;
        }
        if (test->reversed()) {
            test->toReversePath(&woundPath);
        } else {
            test->toPath(&woundPath);
        }
    } while ((test = test->next()));
    *path = *woundPath.nativePath();
    path->setFillType(fillType);
    return true;
}

void SkOpBuilder::add(const SkPath& path, SkPathOp op) {
    if (0 == fOps.count() && op != kUnion_SkPathOp) {
        fPathRefs.push_back() = SkPath();
        *fOps.append() = kUnion_SkPathOp;
    }
    fPathRefs.push_back() = path;
    *fOps.append() = op;
}

void SkOpBuilder::reset() {
    fPathRefs.reset();
    fOps.reset();
}

/* OPTIMIZATION: Union doesn't need to be all-or-nothing. A run of three or more convex
   paths with union ops could be locally resolved and still improve over doing the
   ops one at a time. */
bool SkOpBuilder::resolve(SkPath* result) {
    SkPath original = *result;
    int count = fOps.count();
    bool allUnion = true;
    SkPathFirstDirection firstDir = SkPathFirstDirection::kUnknown;
    for (int index = 0; index < count; ++index) {
        SkPath* test = &fPathRefs[index];
        if (kUnion_SkPathOp != fOps[index] || test->isInverseFillType()) {
            allUnion = false;
            break;
        }
        // If all paths are convex, track direction, reversing as needed.
        if (test->isConvex()) {
            SkPathFirstDirection dir = SkPathPriv::ComputeFirstDirection(*test);
            if (dir == SkPathFirstDirection::kUnknown) {
                allUnion = false;
                break;
            }
            if (firstDir == SkPathFirstDirection::kUnknown) {
                firstDir = dir;
            } else if (firstDir != dir) {
                ReversePath(test);
            }
            continue;
        }
        // If the path is not convex but its bounds do not intersect the others, simplify is enough.
        const SkRect& testBounds = test->getBounds();
        for (int inner = 0; inner < index; ++inner) {
            // OPTIMIZE: check to see if the contour bounds do not intersect other contour bounds?
            if (SkRect::Intersects(fPathRefs[inner].getBounds(), testBounds)) {
                allUnion = false;
                break;
            }
        }
    }
    if (!allUnion) {
        *result = fPathRefs[0];
        for (int index = 1; index < count; ++index) {
            if (!Op(*result, fPathRefs[index], fOps[index], result)) {
                reset();
                *result = original;
                return false;
            }
        }
        reset();
        return true;
    }
    SkPath sum;
    for (int index = 0; index < count; ++index) {
        if (!Simplify(fPathRefs[index], &fPathRefs[index])) {
            reset();
            *result = original;
            return false;
        }
        if (!fPathRefs[index].isEmpty()) {
            // convert the even odd result back to winding form before accumulating it
            if (!FixWinding(&fPathRefs[index])) {
                *result = original;
                return false;
            }
            sum.addPath(fPathRefs[index]);
        }
    }
    reset();
    bool success = Simplify(sum, result);
    if (!success) {
        *result = original;
    }
    return success;
}


text:
package org.trie4j.patricia;

import org.trie4j.AbstractMutableMapTrieTest;

public class MapPatriciaTrieTest
extends AbstractMutableMapTrieTest<MapPatriciaTrie<Integer>>{
	@Override
	protected MapPatriciaTrie<Integer> createFirstTrie() {
		return new MapPatriciaTrie<Integer>();
	}
}


text:
<!DOCTYPE html>

        <title>HDMA-FoodEnvrionment</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.6.0/bootstrap-table.min.css">

Social Media API DEMO

Search Parameters

            <form class="form-horizontal" role="form">

					API:

                        <select class="form-control" id="apiList">
                            <option>Foursquare</option>
                            <option>Flickr</option>
                            <option>Instagram</option>
							<option>Google Places</option>
                        </select>

                    Keywords:

                        <input type="text" id="query" class="form-control" name="query" value="restaurant"/>

                    Latitude:

                        <input type="text" id="lat" class="form-control" name="lat" value="32.777355"/>

                    Longitude:

                        <input type="text" id="long" class="form-control" name="long" value="-117.074058"/>

                    Radius (miles):

                        <input type="text" id="radius" class="form-control" name="radius" value="1"/>

                        Pre-Defined Locations:
                        <input id="SDSU" class="btn btn-info" type="button" value="SDSU"/>
                        <input id="UCSD" class="btn btn-info" type="button" value="UCSD"/>
                        <input id="CityHeight" class="btn btn-info" type="button" value="City Height"/>
                        <input id="LittleItaly" class="btn btn-info" type="button" value="Little Italy"/>

                    <button id="SearchFQ_Button" type="button" class="btn btn-success btn-lg top-buffer center-block">Search by Locations and Keywords</button>

            </form>

Search Results


text:
using System;
using System.Diagnostics;
using System.Net;
using Flashback.Model;
using Flashback.Services.Forum;
using Xunit;

namespace Flashback.Test.Integration.Forum
{
    public class ForumMainListTest
    {
        [Fact]
        public void Ska_kunna_hämta_huvudlistan()
        {
            var forumService = new ForumService(new CookieContainer());

            Stopwatch sw = new Stopwatch();
            sw.Start();
            //for (int i = 0; i < 20; i++)
            //{
                var forum = forumService.GetMainForumlist().Result; // går ej med async i testprojekt?
            //}

            sw.Stop();

            Console.WriteLine(sw.ElapsedMilliseconds);

            //var forum = forumService.GetMainForumlist().Result; // går ej med async i testprojekt?

            Assert.True(forum.Items.Count > 5);

            foreach (var fbItem in forum.Items)
            {
                Assert.True(fbItem.Type == FbItemType.Forum);
                Assert.True(!string.IsNullOrWhiteSpace(fbItem.Id));
                Assert.True(!string.IsNullOrWhiteSpace(fbItem.Name));

                Console.WriteLine($"{fbItem.Name} - {fbItem.Id}");
            }

        }
    }
}


text:
try:
    from cStringIO import StringIO
except ImportError:
    from io import StringIO
import unittest

import jqp

class RunTest(unittest.TestCase):

    def test1(self):
        inputs = StringIO('''{"name": "Taro", "age": 10}
''')
        outputs = StringIO()
        jqp.run(inputs, outputs, 'j["name"]')
        self.assertEqual(outputs.getvalue(), '"Taro"\n')

    def test_import(self):
        inputs = StringIO('''{"name": "Taro", "age": 10}
''')
        outputs = StringIO()
        jqp.run(inputs, outputs, 're.sub("a", "A", j["name"])', imports=['re'])
        self.assertEqual(outputs.getvalue(), '"TAro"\n')

    def test_sort_keys(self):
        # This command ignores input
        inputs = StringIO('''1
''')
        outputs = StringIO()
        jqp.run(inputs, outputs, '{"a": 0, "b": 0, "c": 0}', sort_keys=True)
        self.assertEqual(outputs.getvalue(), '{"a": 0, "b": 0, "c": 0}\n')

    def test_raw_input(self):
        inputs = StringIO(' a \n')
        outputs = StringIO()
        jqp.run(inputs, outputs, 'j', raw_input_mode=True)
        self.assertEqual(outputs.getvalue(), '" a "\n')

    def test_raw_output(self):
        # This command ignores input
        inputs = StringIO('''1
''')
        outputs = StringIO()
        jqp.run(inputs, outputs, '"a"', raw_output=True)
        self.assertEqual(outputs.getvalue(), 'a\n')

    def test_join_output(self):
        inputs = StringIO('''1
2''')
        outputs = StringIO()
        jqp.run(inputs, outputs, 'j', join_output=True)
        self.assertEqual(outputs.getvalue(), '12')

    def test_ascii(self):
        # This command ignores input
        inputs = StringIO('''1
''')
        outputs = StringIO()
        # This is a Japanese character
        jqp.run(inputs, outputs, u'"\u3042"', ascii_output=True)
        self.assertEqual(outputs.getvalue(), '"\\u3042"\n')

    def test_indent(self):
        inputs = StringIO('''[1]
''')
        outputs = StringIO()
        jqp.run(inputs, outputs, 'j', indent=4)
        self.assertEqual(outputs.getvalue(), '[\n    1\n]\n')

    def test_parse_error(self):
        inputs = StringIO('invalid\n')
        outputs = StringIO()
        with self.assertRaises(SystemExit) as e:
            jqp.run(inputs, outputs, 'j')
        self.assertEqual(e.exception.code, 4)

    def test_execution_error(self):
        inputs = StringIO('1\n')
        outputs = StringIO()
        with self.assertRaises(SystemExit) as e:
            jqp.run(inputs, outputs, 'invalid')
        self.assertEqual(e.exception.code, 3)

    def test_dump_error(self):
        inputs = StringIO('1\n')
        outputs = StringIO()
        with self.assertRaises(SystemExit) as e:
            jqp.run(inputs, outputs, 'lambda: 0')
        self.assertEqual(e.exception.code, 3)

    def test_import_error(self):
        inputs = StringIO('1\n')
        outputs = StringIO()
        with self.assertRaises(SystemExit) as e:
            jqp.run(inputs, outputs, 'j', imports=['unknown_module'])
        self.assertEqual(e.exception.code, 5)


text:
class CreatePictures < ActiveRecord::Migration
  def change
    create_table :pictures do |t|
      t.string :url
      t.boolean :is_valid
      t.integer :user_id
    end
  end
end


text:
#import "ABI10_0_0RCTActivityIndicatorView.h"

@implementation ABI10_0_0RCTActivityIndicatorView {
}

- (void)setHidden:(BOOL)hidden
{
  if ([self hidesWhenStopped] && ![self isAnimating]) {
    [super setHidden: YES];
  } else {
    [super setHidden: hidden];
  }
}

@end