text:
#pragma once
#include <memory>
#include "kernel/environment.h"
#include "library/io_state.h"
#include "library/reducible.h"
#include "library/tmp_type_context.h"

namespace lean {
class app_builder_exception : public exception {
public:
    // We may provide more information in the future.
    app_builder_exception():exception("app_builder_exception") {}
};

/** \brief Helper for creating simple applications where some arguments are inferred using
    type inference.

    Example, given
        rel.{l_1 l_2} : Pi (A : Type.{l_1}) (B : A -> Type.{l_2}), (Pi x : A, B x) -> (Pi x : A, B x) -> , Prop
        nat     : Type.{1}
        real    : Type.{1}
        vec.{l} : Pi (A : Type.{l}) (n : nat), Type.{l1}
        f g     : Pi (n : nat), vec real n
    then
        builder.mk_app(rel, f, g)
    returns the application
        rel.{1 2} nat (fun n : nat, vec real n) f g
*/
class app_builder {
    struct imp;
    std::unique_ptr<imp> m_ptr;
public:
    app_builder(environment const & env, options const & o, reducible_behavior b = UnfoldReducible);
    app_builder(environment const & env, reducible_behavior b = UnfoldReducible);
    app_builder(tmp_type_context & ctx);
    ~app_builder();
    /** \brief Create an application (d.{_ ... _} _ ... _ args[0] ... args[nargs-1]).
        The missing arguments and universes levels are inferred using type inference.

        \remark The method throwns an app_builder_exception if: not all arguments can be inferred;
        or constraints are created during type inference; or an exception is thrown
        during type inference.

        \remark This methods uses just higher-order pattern matching.
    */
    expr mk_app(name const & c, unsigned nargs, expr const * args);

    expr mk_app(name const & c, std::initializer_list<expr> const & args) {
        return mk_app(c, args.size(), args.begin());
    }

    expr mk_app(name const & c, expr const & a1) {
        return mk_app(c, {a1});
    }

    expr mk_app(name const & c, expr const & a1, expr const & a2) {
        return mk_app(c, {a1, a2});
    }

    expr mk_app(name const & c, expr const & a1, expr const & a2, expr const & a3) {
        return mk_app(c, {a1, a2, a3});
    }

    expr mk_app(name const & c, unsigned mask_sz, bool const * mask, expr const * args);

    /** \brief Shortcut for mk_app(c, total_nargs, mask, expl_nargs), where
        \c mask starts with total_nargs - expl_nargs false's followed by expl_nargs true's
        \pre total_nargs >= expl_nargs */
    expr mk_app(name const & c, unsigned total_nargs, unsigned expl_nargs, expr const * expl_args);

    expr mk_app(name const & c, unsigned total_nargs, std::initializer_list<expr> const & args) {
        return mk_app(c, total_nargs, args.size(), args.begin());
    }

    expr mk_app(name const & c, unsigned total_nargs, expr const & a1) {
        return mk_app(c, total_nargs, {a1});
    }

    expr mk_app(name const & c, unsigned total_nargs, expr const & a1, expr const & a2) {
        return mk_app(c, total_nargs, {a1, a2});
    }

    expr mk_app(name const & c, unsigned total_nargs, expr const & a1, expr const & a2, expr const & a3) {
        return mk_app(c, total_nargs, {a1, a2, a3});
    }

    /** \brief Similar to mk_app(n, lhs, rhs), but handles eq and iff more efficiently. */
    expr mk_rel(name const & n, expr const & lhs, expr const & rhs);
    expr mk_eq(expr const & lhs, expr const & rhs);
    expr mk_iff(expr const & lhs, expr const & rhs);
    expr mk_heq(expr const & lhs, expr const & rhs);

    /** \brief Similar a reflexivity proof for the given relation */
    expr mk_refl(name const & relname, expr const & a);
    expr mk_eq_refl(expr const & a);
    expr mk_iff_refl(expr const & a);
    expr mk_heq_refl(expr const & a);

    /** \brief Similar a symmetry proof for the given relation */
    expr mk_symm(name const & relname, expr const & H);
    expr mk_eq_symm(expr const & H);
    expr mk_iff_symm(expr const & H);
    expr mk_heq_symm(expr const & H);

    /** \brief Similar a transitivity proof for the given relation */
    expr mk_trans(name const & relname, expr const & H1, expr const & H2);
    expr mk_eq_trans(expr const & H1, expr const & H2);
    expr mk_iff_trans(expr const & H1, expr const & H2);
    expr mk_heq_trans(expr const & H1, expr const & H2);

    /** \brief Create a (non-dependent) eq.rec application.
        C is the motive. The expected types for C, H1 and H2 are
             C : A -> Type
             H1 : C a
             H2 : a = b
        The resultant application is
             @eq.rec A a C H1 b H2
        In the HoTT library, we actually create an eq.nrec application
        since eq.rec is a dependent eliminator in HoTT. */
    expr mk_eq_rec(expr const & C, expr const & H1, expr const & H2);

    /** \brief Create a (dependent) eq.drec application.
        C is the motive. The expected types for C, H1 and H2 are
             C : Pi (x : A), a = x -> Type
             H1 : C a (eq.refl a)
             H2 : a = b
        The resultant application is
             @eq.drec A a C H1 b H2
        In the HoTT library, we actually create an eq.rec application
        because eq.rec is a dependent eliminator in HoTT. */
    expr mk_eq_drec(expr const & C, expr const & H1, expr const & H2);

    expr mk_eq_of_heq(expr const & H);
    expr mk_heq_of_eq(expr const & H);

    expr mk_congr_arg(expr const & f, expr const & H);
    expr mk_congr_fun(expr const & H, expr const & a);
    expr mk_congr(expr const & H1, expr const & H2);

    /** \brief Given a reflexive relation R, and a proof H : a = b,
        build a proof for (R a b) */
    expr lift_from_eq(name const & R, expr const & H);

    /** \brief not p -> (p <-> false) */
    expr mk_iff_false_intro(expr const & H);
    /** \brief p -> (p <-> true) */
    expr mk_iff_true_intro(expr const & H);
    /** \brief (p <-> false) -> not p */
    expr mk_not_of_iff_false(expr const & H);
    /** \brief (p <-> true) -> p */
    expr mk_of_iff_true(expr const & H);
    /** \brief (true <-> false) -> false */
    expr mk_false_of_true_iff_false(expr const & H);

    expr mk_not(expr const & H);

    expr mk_partial_add(expr const & A);
    expr mk_partial_mul(expr const & A);
    expr mk_zero(expr const & A);
    expr mk_one(expr const & A);
    expr mk_partial_left_distrib(expr const & A);
    expr mk_partial_right_distrib(expr const & A);

    /** \brief Create (@sorry type) */
    expr mk_sorry(expr const & type);

    /** \brief False elimination */
    expr mk_false_rec(expr const & c, expr const & H);

    /** \brief Set the local instances. This method is relevant when we want to expose local class instances
        to the app_builder.

        \remark When the constructor app_builder(tmp_type_context & ctx) is used
        the initialization can be performed outside. */
    void set_local_instances(list<expr> const & insts);
};
void initialize_app_builder();
void finalize_app_builder();
}


text:
cChestEntity::cChestEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World):
	Super(a_BlockType, a_BlockMeta, a_Pos, ContentsWidth, ContentsHeight, a_World),
	m_NumActivePlayers(0),
	m_Neighbour(nullptr)
{
}

cChestEntity & cChestEntity::GetPrimaryChest()
{
	if (m_Neighbour == nullptr)
	{
		return *this;
	}

	// The primary chest should be the one with lesser X or Z coord:
	return (
		(m_Neighbour->GetPosX() < GetPosX()) ||
		(m_Neighbour->GetPosZ() < GetPosZ())
	) ? *m_Neighbour : *this;
}

cChestEntity * cChestEntity::GetSecondaryChest()
{
	// If we're the primary, then our neighbour is the secondary, and vice versa:
	return (&GetPrimaryChest() == this) ? m_Neighbour : this;
}

bool cChestEntity::ScanNeighbour(cChunk & a_Chunk, Vector3i a_Position)
{
	const auto Chunk = a_Chunk.GetRelNeighborChunkAdjustCoords(a_Position);

	if ((Chunk == nullptr) || !Chunk->IsValid())
	{
		// If a chest was in fact there, they'll find us when their chunk loads.
		return false;
	}

	const auto BlockEntity = Chunk->GetBlockEntityRel(a_Position);

	if ((BlockEntity == nullptr) || (BlockEntity->GetBlockType() != m_BlockType))
	{
		// Neighbouring block is not the same type of chest:
		return false;
	}

	m_Neighbour = static_cast<cChestEntity *>(BlockEntity);
	return true;
}

void cChestEntity::DestroyWindow()
{
	const auto Window = GetWindow();
	if (Window != nullptr)
	{
		Window->OwnerDestroyed();
	}
}

bool cChestEntity::IsBlocked()
{
	return (
		(GetPosY() < cChunkDef::Height - 1) &&
		(
			!cBlockInfo::IsTransparent(GetWorld()->GetBlock(GetPos().addedY(1))) ||
			!cOcelot::IsCatSittingOnBlock(GetWorld(), Vector3d(GetPos()))
		)
	);
}

void cChestEntity::OpenNewWindow(void)
{
	if (m_Neighbour != nullptr)
	{
		ASSERT(&GetPrimaryChest() == this);  // Should only open windows for the primary chest.

		OpenWindow(new cChestWindow(this, m_Neighbour));
	}
	else
	{
		// There is no chest neighbour, open a single-chest window:
		OpenWindow(new cChestWindow(this));
	}
}

void cChestEntity::CopyFrom(const cBlockEntity & a_Src)
{
	Super::CopyFrom(a_Src);
	auto & src = static_cast<const cChestEntity &>(a_Src);
	m_Contents.CopyFrom(src.m_Contents);

	// Reset the neighbor and player count, there's no sense in copying these:
	m_Neighbour = nullptr;
	m_NumActivePlayers = 0;
}

void cChestEntity::OnAddToWorld(cWorld & a_World, cChunk & a_Chunk)
{
	Super::OnAddToWorld(a_World, a_Chunk);

	// Scan horizontally adjacent blocks for any neighbouring chest of the same type:
	if (
		const auto Position = GetRelPos();

		ScanNeighbour(a_Chunk, Position.addedX(-1)) ||
		ScanNeighbour(a_Chunk, Position.addedX(+1)) ||
		ScanNeighbour(a_Chunk, Position.addedZ(-1)) ||
		ScanNeighbour(a_Chunk, Position.addedZ(+1))
	)
	{
		m_Neighbour->m_Neighbour = this;
		m_Neighbour->DestroyWindow();  // Force neighbour's window shut. Does Mojang server do this or should a double window open?
	}
}

void cChestEntity::OnRemoveFromWorld()
{
	if (m_Neighbour != nullptr)
	{
		// Neighbour may share a window with us, force the window shut:
		m_Neighbour->DestroyWindow();
		m_Neighbour->m_Neighbour = nullptr;
	}

	DestroyWindow();
}

void cChestEntity::SendTo(cClientHandle & a_Client)
{
	a_Client.SendUpdateBlockEntity(*this);
}

bool cChestEntity::UsedBy(cPlayer * a_Player)
{
	if (IsBlocked())
	{
		// Obstruction, don't open
		return true;
	}

	if ((m_Neighbour != nullptr) && m_Neighbour->IsBlocked())
	{
		return true;
	}

	if (m_BlockType == E_BLOCK_CHEST)
	{
		a_Player->GetStatistics().Custom[CustomStatistic::OpenChest]++;
	}
	else  // E_BLOCK_TRAPPED_CHEST
	{
		a_Player->GetStatistics().Custom[CustomStatistic::TriggerTrappedChest]++;
	}

	auto & PrimaryChest = GetPrimaryChest();
	cWindow * Window = PrimaryChest.GetWindow();

	// If the window is not created, open it anew:
	if (Window == nullptr)
	{
		PrimaryChest.OpenNewWindow();
		Window = PrimaryChest.GetWindow();
	}

	// Open the window for the player:
	if (Window != nullptr)
	{
		if (a_Player->GetWindow() != Window)
		{
			a_Player->OpenWindow(*Window);
		}
	}

	return true;
}

void cChestEntity::OnSlotChanged(cItemGrid * a_Grid, int a_SlotNum)
{
	ASSERT(a_Grid == &m_Contents);

	// Have cBlockEntityWithItems update redstone and try to broadcast our window:
	Super::OnSlotChanged(a_Grid, a_SlotNum);

	cWindow * Window = GetWindow();
	if ((Window == nullptr) && (m_Neighbour != nullptr))
	{
		// Window was null, Super will have failed.
		// Neighbour might own the window:
		Window = m_Neighbour->GetWindow();
	}

	if (Window != nullptr)
	{
		Window->BroadcastWholeWindow();
	}
}

redpajama_set_name: RedPajamaGithub


text:
package plugin

import (
	"fmt"
	"time"

	"github.com/docker/go-plugins-helpers/volume"
	"github.com/mcuadros/gce-docker/providers"
	"github.com/spf13/afero"
	"google.golang.org/api/compute/v1"
	. "gopkg.in/check.v1"
)

const TimeoutAfterUnmount = 15 * time.Second

type VolumeSuite struct {
	v  *Volume
	fs *MemFilesystem
	p  *DiskProviderFixture
}

var _ = Suite(&VolumeSuite{})

func (s *VolumeSuite) SetUpTest(c *C) {
	s.fs = NewMemFilesystem()
	s.p = NewDiskProviderFixture()
	s.v = &Volume{p: s.p, fs: s.fs, Root: "/mnt/"}
}

func (s *VolumeSuite) TestCreateDiskConfig(c *C) {
	config, err := s.v.createDiskConfig(volume.Request{Name: "foo"})
	c.Assert(err, IsNil)
	c.Assert(config.Name, Equals, "foo")

	config, err = s.v.createDiskConfig(volume.Request{
		Name:    "foo",
		Options: map[string]string{"SizeGb": "42"},
	})
	c.Assert(err, IsNil)
	c.Assert(config.SizeGb, Equals, int64(42))

	config, err = s.v.createDiskConfig(volume.Request{
		Name:    "foo",
		Options: map[string]string{"Type": "foo"},
	})
	c.Assert(err, IsNil)
	c.Assert(config.Type, Equals, "foo")

	config, err = s.v.createDiskConfig(volume.Request{
		Name:    "foo",
		Options: map[string]string{"SourceSnapshot": "foo"},
	})
	c.Assert(err, IsNil)
	c.Assert(config.SourceSnapshot, Equals, "foo")

	config, err = s.v.createDiskConfig(volume.Request{
		Name:    "foo",
		Options: map[string]string{"SourceImage": "foo"},
	})
	c.Assert(err, IsNil)
	c.Assert(config.SourceImage, Equals, "foo")
}

func (s *VolumeSuite) TestCreate(c *C) {
	r := s.v.Create(volume.Request{Name: "foo"})
	c.Assert(r.Err, HasLen, 0)

	c.Assert(s.p.disks, HasLen, 1)
	c.Assert(s.p.disks["foo"], Equals, true)
}

func (s *VolumeSuite) TestList(c *C) {
	r := s.v.Create(volume.Request{Name: "foo"})
	c.Assert(r.Err, HasLen, 0)

	r = s.v.List(volume.Request{})
	c.Assert(r.Err, HasLen, 0)
	c.Assert(r.Volumes, HasLen, 1)
	c.Assert(r.Volumes[0].Name, Equals, "foo")
}

func (s *VolumeSuite) TestRemove(c *C) {
	r := s.v.Create(volume.Request{Name: "foo"})
	c.Assert(r.Err, HasLen, 0)

	r = s.v.Remove(volume.Request{Name: "foo"})
	c.Assert(r.Err, HasLen, 0)

	c.Assert(s.p.disks, HasLen, 0)
}

func (s *VolumeSuite) TestPath(c *C) {
	r := s.v.Path(volume.Request{Name: "foo"})
	c.Assert(r.Err, HasLen, 0)
	c.Assert(r.Mountpoint, Equals, "/mnt/foo")

	fs, err := s.fs.Stat(r.Mountpoint)
	c.Assert(err, IsNil)
	c.Assert(fs.IsDir(), Equals, true)
}

func (s *VolumeSuite) TestMount(c *C) {
	r := s.v.Create(volume.Request{Name: "foo"})
	c.Assert(r.Err, HasLen, 0)

	r = s.v.Mount(volume.Request{Name: "foo"})
	c.Assert(r.Err, HasLen, 0)
	c.Assert(r.Mountpoint, Equals, "/mnt/foo")

	fs, err := s.fs.Stat(r.Mountpoint)
	c.Assert(err, IsNil)
	c.Assert(fs.IsDir(), Equals, true)

	c.Assert(s.p.attached, HasLen, 1)
	c.Assert(s.p.attached["foo"], Equals, true)
	c.Assert(s.fs.Mounted["/mnt/foo"], Equals, "/dev/disk/by-id/google-docker-volume-foo")
}

func (s *VolumeSuite) TestUnmount(c *C) {
	r := s.v.Create(volume.Request{Name: "foo"})
	c.Assert(r.Err, HasLen, 0)

	r = s.v.Mount(volume.Request{Name: "foo"})
	c.Assert(r.Err, HasLen, 0)
	c.Assert(r.Mountpoint, Equals, "/mnt/foo")

	r = s.v.Unmount(volume.Request{Name: "foo"})
	c.Assert(r.Err, HasLen, 0)

	c.Assert(s.p.attached, HasLen, 0)
	c.Assert(s.fs.Mounted["/mnt/foo"], Equals, "")
}

type DiskProviderFixture struct {
	disks    map[string]bool
	attached map[string]bool
}

func NewDiskProviderFixture() *DiskProviderFixture {
	return &DiskProviderFixture{
		disks:    make(map[string]bool, 0),
		attached: make(map[string]bool, 0),
	}
}

func (d *DiskProviderFixture) Create(c *providers.DiskConfig) error {
	d.disks[c.Name] = true
	return nil
}

func (d *DiskProviderFixture) Attach(c *providers.DiskConfig) error {
	if _, ok := d.disks[c.Name]; !ok {
		return fmt.Errorf("unable to find disk %s", c.Name)
	}

	d.attached[c.Name] = true
	return nil
}

func (d *DiskProviderFixture) Detach(c *providers.DiskConfig) error {
	delete(d.attached, c.Name)
	return nil
}

func (d *DiskProviderFixture) Delete(c *providers.DiskConfig) error {
	delete(d.disks, c.Name)
	return nil
}

func (d *DiskProviderFixture) List() ([]*compute.Disk, error) {
	var l []*compute.Disk
	for name, _ := range d.disks {
		l = append(l, &compute.Disk{Name: name, Status: "READY"})
	}

	l = append(l, &compute.Disk{Name: "no-ready", Status: "PENDING"})
	return l, nil
}

type MemFilesystem struct {
	Mounted   map[string]string
	Formatted map[string]string
	afero.Fs
}

func NewMemFilesystem() *MemFilesystem {
	return &MemFilesystem{
		Mounted:   make(map[string]string, 0),
		Formatted: make(map[string]string, 0),

		Fs: afero.NewMemMapFs(),
	}
}

func (fs *MemFilesystem) Mount(source string, target string) error {
	fs.Mounted[target] = source
	return nil
}

func (fs *MemFilesystem) Unmount(target string) error {
	fs.Mounted[target] = ""
	return nil
}

func (fs *MemFilesystem) Format(source string) error {
	fs.Formatted[source] = "ext4"
	return nil
}