<commit_before>// //////////////////////////////////////////////////////////
// sha3.cpp
// Copyright (c) 2014 Stephan Brumme. All rights reserved.
// see http://create.stephan-brumme.com/disclaimer.html
//

#include "susi/util/sha3.h"

// big endian architectures need #define __BYTE_ORDER __BIG_ENDIAN
#ifndef _MSC_VER
#include <endian.h>
#endif

/// same as reset()
SHA3::SHA3( Bits bits )
    : m_blockSize( 200 - 2 * ( bits / 8 ) ),
      m_bits( bits )
{
    reset();
}

/// restart
void SHA3::reset()
{
    for( size_t i = 0; i < StateSize; i++ )
        m_hash[i] = 0;

    m_numBytes   = 0;
    m_bufferSize = 0;
}

/// constants and local helper functions
namespace
{
    const unsigned int Rounds = 24;
    const uint64_t XorMasks[Rounds] =
    {
        0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808aULL,
        0x8000000080008000ULL, 0x000000000000808bULL, 0x0000000080000001ULL,
        0x8000000080008081ULL, 0x8000000000008009ULL, 0x000000000000008aULL,
        0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000aULL,
        0x000000008000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL,
        0x8000000000008003ULL, 0x8000000000008002ULL, 0x8000000000000080ULL,
        0x000000000000800aULL, 0x800000008000000aULL, 0x8000000080008081ULL,
        0x8000000000008080ULL, 0x0000000080000001ULL, 0x8000000080008008ULL
    };

    /// rotate left and wrap around to the right
    inline uint64_t rotateLeft( uint64_t x, uint8_t numBits )
    {
        return ( x << numBits ) | ( x >> ( 64 - numBits ) );
    }

    /// convert litte vs big endian
    inline uint64_t swap( uint64_t x )
    {
#if defined(__GNUC__) || defined(__clang__)
        return __builtin_bswap64( x );
#endif
#ifdef MSC_VER
        return _byteswap_uint64( x );
#endif

        return ( x >> 56 ) |
               ( ( x >> 40 ) & 0x000000000000FF00ULL ) |
               ( ( x >> 24 ) & 0x0000000000FF0000ULL ) |
               ( ( x >>  8 ) & 0x00000000FF000000ULL ) |
               ( ( x <<  8 ) & 0x000000FF00000000ULL ) |
               ( ( x << 24 ) & 0x0000FF0000000000ULL ) |
               ( ( x << 40 ) & 0x00FF000000000000ULL ) |
               ( x << 56 );
    }

    /// return x % 5 for 0 <= x <= 9
    unsigned int mod5( unsigned int x )
    {
        if( x < 5 )
            return x;

        return x - 5;
    }
}

/// process a full block
void SHA3::processBlock( const void* data )
{
#if defined(__BYTE_ORDER) && (__BYTE_ORDER != 0) && (__BYTE_ORDER == __BIG_ENDIAN)
#define LITTLEENDIAN(x) swap(x)
#else
#define LITTLEENDIAN(x) (x)
#endif

    const uint64_t* data64 = ( const uint64_t* ) data;
    // mix data into state
    for( unsigned int i = 0; i < m_blockSize / 8; i++ )
        m_hash[i] ^= LITTLEENDIAN( data64[i] );

    // re-compute state
    for( unsigned int round = 0; round < Rounds; round++ )
    {
        // Theta
        uint64_t coefficients[5];
        for( unsigned int i = 0; i < 5; i++ )
            coefficients[i] = m_hash[i] ^ m_hash[i + 5] ^ m_hash[i + 10] ^ m_hash[i + 15] ^ m_hash[i + 20];

        for( unsigned int i = 0; i < 5; i++ )
        {
            uint64_t one = coefficients[mod5( i + 4 )] ^ rotateLeft( coefficients[mod5( i + 1 )], 1 );
            m_hash[i     ] ^= one;
            m_hash[i +  5] ^= one;
            m_hash[i + 10] ^= one;
            m_hash[i + 15] ^= one;
            m_hash[i + 20] ^= one;
        }

        // temporary
        uint64_t one;

        // Rho Pi
        uint64_t last = m_hash[1];
        one = m_hash[10];
        m_hash[10] = rotateLeft( last,  1 );
        last = one;
        one = m_hash[ 7];
        m_hash[ 7] = rotateLeft( last,  3 );
        last = one;
        one = m_hash[11];
        m_hash[11] = rotateLeft( last,  6 );
        last = one;
        one = m_hash[17];
        m_hash[17] = rotateLeft( last, 10 );
        last = one;
        one = m_hash[18];
        m_hash[18] = rotateLeft( last, 15 );
        last = one;
        one = m_hash[ 3];
        m_hash[ 3] = rotateLeft( last, 21 );
        last = one;
        one = m_hash[ 5];
        m_hash[ 5] = rotateLeft( last, 28 );
        last = one;
        one = m_hash[16];
        m_hash[16] = rotateLeft( last, 36 );
        last = one;
        one = m_hash[ 8];
        m_hash[ 8] = rotateLeft( last, 45 );
        last = one;
        one = m_hash[21];
        m_hash[21] = rotateLeft( last, 55 );
        last = one;
        one = m_hash[24];
        m_hash[24] = rotateLeft( last,  2 );
        last = one;
        one = m_hash[ 4];
        m_hash[ 4] = rotateLeft( last, 14 );
        last = one;
        one = m_hash[15];
        m_hash[15] = rotateLeft( last, 27 );
        last = one;
        one = m_hash[23];
        m_hash[23] = rotateLeft( last, 41 );
        last = one;
        one = m_hash[19];
        m_hash[19] = rotateLeft( last, 56 );
        last = one;
        one = m_hash[13];
        m_hash[13] = rotateLeft( last,  8 );
        last = one;
        one = m_hash[12];
        m_hash[12] = rotateLeft( last, 25 );
        last = one;
        one = m_hash[ 2];
        m_hash[ 2] = rotateLeft( last, 43 );
        last = one;
        one = m_hash[20];
        m_hash[20] = rotateLeft( last, 62 );
        last = one;
        one = m_hash[14];
        m_hash[14] = rotateLeft( last, 18 );
        last = one;
        one = m_hash[22];
        m_hash[22] = rotateLeft( last, 39 );
        last = one;
        one = m_hash[ 9];
        m_hash[ 9] = rotateLeft( last, 61 );
        last = one;
        one = m_hash[ 6];
        m_hash[ 6] = rotateLeft( last, 20 );
        last = one;
        m_hash[ 1] = rotateLeft( last, 44 );

        // Chi
        for( unsigned int j = 0; j < 25; j += 5 )
        {
            // temporaries
            uint64_t one = m_hash[j];
            uint64_t two = m_hash[j + 1];

            m_hash[j]     ^= m_hash[j + 2] & ~two;
            m_hash[j + 1] ^= m_hash[j + 3] & ~m_hash[j + 2];
            m_hash[j + 2] ^= m_hash[j + 4] & ~m_hash[j + 3];
            m_hash[j + 3] ^=      one      & ~m_hash[j + 4];
            m_hash[j + 4] ^=      two      & ~one;
        }

        // Iota
        m_hash[0] ^= XorMasks[round];
    }
}

/// add arbitrary number of bytes
void SHA3::add( const void* data, size_t numBytes )
{
    const uint8_t* current = ( const uint8_t* ) data;

    if( m_bufferSize > 0 )
    {
        while( numBytes > 0 && m_bufferSize < m_blockSize )
        {
            m_buffer[m_bufferSize++] = *current++;
            numBytes--;
        }
    }

    // full buffer
    if( m_bufferSize == m_blockSize )
    {
        processBlock( ( void* )m_buffer );
        m_numBytes  += m_blockSize;
        m_bufferSize = 0;
    }

    // no more data ?
    if( numBytes == 0 )
        return;

    // process full blocks
    while( numBytes >= m_blockSize )
    {
        processBlock( current );
        current    += m_blockSize;
        m_numBytes += m_blockSize;
        numBytes   -= m_blockSize;
    }

    // keep remaining bytes in buffer
    while( numBytes > 0 )
    {
        m_buffer[m_bufferSize++] = *current++;
        numBytes--;
    }
}

/// process everything left in the internal buffer
void SHA3::processBuffer()
{
    unsigned int blockSize = 200 - 2 * ( m_bits / 8 );

    // add padding
    size_t offset = m_bufferSize;
    // add a "1" byte
    m_buffer[offset++] = 0x06;
    // fill with zeros
    while( offset < blockSize - 1 )
        m_buffer[offset++] = 0;

    // and add a single set bit
    m_buffer[blockSize - 1] = 0x80;

    processBlock( m_buffer );
}

/// return latest hash as 16 hex characters
std::string SHA3::getHash()
{
    // process remaining bytes
    processBuffer();

    // convert hash to string
    static const char dec2hex[16 + 1] = "0123456789abcdef";

    // number of significant elements in hash (uint64_t)
    unsigned int hashLength = m_bits / 64;

    std::string result;
    for( unsigned int i = 0; i < hashLength; i++ )
        for( unsigned int j = 0; j < 8; j++ ) // 64 bits => 8 bytes
        {
            // convert a byte to hex
            unsigned char oneByte = ( unsigned char )( m_hash[i] >> ( 8 * j ) );
            result += dec2hex[oneByte >> 4];
            result += dec2hex[oneByte & 15];
        }

    return result;
}

/// compute SHA3 of a memory block
std::string SHA3::operator()( const void* data, size_t numBytes )
{
    reset();
    add( data, numBytes );
    return getHash();
}

/// compute SHA3 of a string, excluding final zero
std::string SHA3::operator()( const std::string& text )
{
    reset();
    add( text.c_str(), text.size() );
    return getHash();
}
<commit_msg>[susi-common] fixed wrong include path in sha3.cpp;<commit_after>// //////////////////////////////////////////////////////////
// sha3.cpp
// Copyright (c) 2014 Stephan Brumme. All rights reserved.
// see http://create.stephan-brumme.com/disclaimer.html
//

#include "susi/sha3.h"

// big endian architectures need #define __BYTE_ORDER __BIG_ENDIAN
#ifndef _MSC_VER
#include <endian.h>
#endif

/// same as reset()
SHA3::SHA3( Bits bits )
    : m_blockSize( 200 - 2 * ( bits / 8 ) ),
      m_bits( bits )
{
    reset();
}

/// restart
void SHA3::reset()
{
    for( size_t i = 0; i < StateSize; i++ )
        m_hash[i] = 0;

    m_numBytes   = 0;
    m_bufferSize = 0;
}

/// constants and local helper functions
namespace
{
    const unsigned int Rounds = 24;
    const uint64_t XorMasks[Rounds] =
    {
        0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808aULL,
        0x8000000080008000ULL, 0x000000000000808bULL, 0x0000000080000001ULL,
        0x8000000080008081ULL, 0x8000000000008009ULL, 0x000000000000008aULL,
        0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000aULL,
        0x000000008000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL,
        0x8000000000008003ULL, 0x8000000000008002ULL, 0x8000000000000080ULL,
        0x000000000000800aULL, 0x800000008000000aULL, 0x8000000080008081ULL,
        0x8000000000008080ULL, 0x0000000080000001ULL, 0x8000000080008008ULL
    };

    /// rotate left and wrap around to the right
    inline uint64_t rotateLeft( uint64_t x, uint8_t numBits )
    {
        return ( x << numBits ) | ( x >> ( 64 - numBits ) );
    }

    /// convert litte vs big endian
    inline uint64_t swap( uint64_t x )
    {
#if defined(__GNUC__) || defined(__clang__)
        return __builtin_bswap64( x );
#endif
#ifdef MSC_VER
        return _byteswap_uint64( x );
#endif

        return ( x >> 56 ) |
               ( ( x >> 40 ) & 0x000000000000FF00ULL ) |
               ( ( x >> 24 ) & 0x0000000000FF0000ULL ) |
               ( ( x >>  8 ) & 0x00000000FF000000ULL ) |
               ( ( x <<  8 ) & 0x000000FF00000000ULL ) |
               ( ( x << 24 ) & 0x0000FF0000000000ULL ) |
               ( ( x << 40 ) & 0x00FF000000000000ULL ) |
               ( x << 56 );
    }

    /// return x % 5 for 0 <= x <= 9
    unsigned int mod5( unsigned int x )
    {
        if( x < 5 )
            return x;

        return x - 5;
    }
}

/// process a full block
void SHA3::processBlock( const void* data )
{
#if defined(__BYTE_ORDER) && (__BYTE_ORDER != 0) && (__BYTE_ORDER == __BIG_ENDIAN)
#define LITTLEENDIAN(x) swap(x)
#else
#define LITTLEENDIAN(x) (x)
#endif

    const uint64_t* data64 = ( const uint64_t* ) data;
    // mix data into state
    for( unsigned int i = 0; i < m_blockSize / 8; i++ )
        m_hash[i] ^= LITTLEENDIAN( data64[i] );

    // re-compute state
    for( unsigned int round = 0; round < Rounds; round++ )
    {
        // Theta
        uint64_t coefficients[5];
        for( unsigned int i = 0; i < 5; i++ )
            coefficients[i] = m_hash[i] ^ m_hash[i + 5] ^ m_hash[i + 10] ^ m_hash[i + 15] ^ m_hash[i + 20];

        for( unsigned int i = 0; i < 5; i++ )
        {
            uint64_t one = coefficients[mod5( i + 4 )] ^ rotateLeft( coefficients[mod5( i + 1 )], 1 );
            m_hash[i     ] ^= one;
            m_hash[i +  5] ^= one;
            m_hash[i + 10] ^= one;
            m_hash[i + 15] ^= one;
            m_hash[i + 20] ^= one;
        }

        // temporary
        uint64_t one;

        // Rho Pi
        uint64_t last = m_hash[1];
        one = m_hash[10];
        m_hash[10] = rotateLeft( last,  1 );
        last = one;
        one = m_hash[ 7];
        m_hash[ 7] = rotateLeft( last,  3 );
        last = one;
        one = m_hash[11];
        m_hash[11] = rotateLeft( last,  6 );
        last = one;
        one = m_hash[17];
        m_hash[17] = rotateLeft( last, 10 );
        last = one;
        one = m_hash[18];
        m_hash[18] = rotateLeft( last, 15 );
        last = one;
        one = m_hash[ 3];
        m_hash[ 3] = rotateLeft( last, 21 );
        last = one;
        one = m_hash[ 5];
        m_hash[ 5] = rotateLeft( last, 28 );
        last = one;
        one = m_hash[16];
        m_hash[16] = rotateLeft( last, 36 );
        last = one;
        one = m_hash[ 8];
        m_hash[ 8] = rotateLeft( last, 45 );
        last = one;
        one = m_hash[21];
        m_hash[21] = rotateLeft( last, 55 );
        last = one;
        one = m_hash[24];
        m_hash[24] = rotateLeft( last,  2 );
        last = one;
        one = m_hash[ 4];
        m_hash[ 4] = rotateLeft( last, 14 );
        last = one;
        one = m_hash[15];
        m_hash[15] = rotateLeft( last, 27 );
        last = one;
        one = m_hash[23];
        m_hash[23] = rotateLeft( last, 41 );
        last = one;
        one = m_hash[19];
        m_hash[19] = rotateLeft( last, 56 );
        last = one;
        one = m_hash[13];
        m_hash[13] = rotateLeft( last,  8 );
        last = one;
        one = m_hash[12];
        m_hash[12] = rotateLeft( last, 25 );
        last = one;
        one = m_hash[ 2];
        m_hash[ 2] = rotateLeft( last, 43 );
        last = one;
        one = m_hash[20];
        m_hash[20] = rotateLeft( last, 62 );
        last = one;
        one = m_hash[14];
        m_hash[14] = rotateLeft( last, 18 );
        last = one;
        one = m_hash[22];
        m_hash[22] = rotateLeft( last, 39 );
        last = one;
        one = m_hash[ 9];
        m_hash[ 9] = rotateLeft( last, 61 );
        last = one;
        one = m_hash[ 6];
        m_hash[ 6] = rotateLeft( last, 20 );
        last = one;
        m_hash[ 1] = rotateLeft( last, 44 );

        // Chi
        for( unsigned int j = 0; j < 25; j += 5 )
        {
            // temporaries
            uint64_t one = m_hash[j];
            uint64_t two = m_hash[j + 1];

            m_hash[j]     ^= m_hash[j + 2] & ~two;
            m_hash[j + 1] ^= m_hash[j + 3] & ~m_hash[j + 2];
            m_hash[j + 2] ^= m_hash[j + 4] & ~m_hash[j + 3];
            m_hash[j + 3] ^=      one      & ~m_hash[j + 4];
            m_hash[j + 4] ^=      two      & ~one;
        }

        // Iota
        m_hash[0] ^= XorMasks[round];
    }
}

/// add arbitrary number of bytes
void SHA3::add( const void* data, size_t numBytes )
{
    const uint8_t* current = ( const uint8_t* ) data;

    if( m_bufferSize > 0 )
    {
        while( numBytes > 0 && m_bufferSize < m_blockSize )
        {
            m_buffer[m_bufferSize++] = *current++;
            numBytes--;
        }
    }

    // full buffer
    if( m_bufferSize == m_blockSize )
    {
        processBlock( ( void* )m_buffer );
        m_numBytes  += m_blockSize;
        m_bufferSize = 0;
    }

    // no more data ?
    if( numBytes == 0 )
        return;

    // process full blocks
    while( numBytes >= m_blockSize )
    {
        processBlock( current );
        current    += m_blockSize;
        m_numBytes += m_blockSize;
        numBytes   -= m_blockSize;
    }

    // keep remaining bytes in buffer
    while( numBytes > 0 )
    {
        m_buffer[m_bufferSize++] = *current++;
        numBytes--;
    }
}

/// process everything left in the internal buffer
void SHA3::processBuffer()
{
    unsigned int blockSize = 200 - 2 * ( m_bits / 8 );

    // add padding
    size_t offset = m_bufferSize;
    // add a "1" byte
    m_buffer[offset++] = 0x06;
    // fill with zeros
    while( offset < blockSize - 1 )
        m_buffer[offset++] = 0;

    // and add a single set bit
    m_buffer[blockSize - 1] = 0x80;

    processBlock( m_buffer );
}

/// return latest hash as 16 hex characters
std::string SHA3::getHash()
{
    // process remaining bytes
    processBuffer();

    // convert hash to string
    static const char dec2hex[16 + 1] = "0123456789abcdef";

    // number of significant elements in hash (uint64_t)
    unsigned int hashLength = m_bits / 64;

    std::string result;
    for( unsigned int i = 0; i < hashLength; i++ )
        for( unsigned int j = 0; j < 8; j++ ) // 64 bits => 8 bytes
        {
            // convert a byte to hex
            unsigned char oneByte = ( unsigned char )( m_hash[i] >> ( 8 * j ) );
            result += dec2hex[oneByte >> 4];
            result += dec2hex[oneByte & 15];
        }

    return result;
}

/// compute SHA3 of a memory block
std::string SHA3::operator()( const void* data, size_t numBytes )
{
    reset();
    add( data, numBytes );
    return getHash();
}

/// compute SHA3 of a string, excluding final zero
std::string SHA3::operator()( const std::string& text )
{
    reset();
    add( text.c_str(), text.size() );
    return getHash();
}
<|endoftext|>


<commit_before><commit_msg>coverity#1242630 Untrusted loop bound<commit_after><|endoftext|>


<commit_before><commit_msg>coverity#1158083 Unchecked dynamic_cast<commit_after><|endoftext|>


<commit_before>/*************************************************************************
 *
 *  $RCSfile: itemdef.hxx,v $
 *
 *  $Revision: 1.1.1.1 $
 *
 *  last change: $Author: hr $ $Date: 2000-09-18 17:14:40 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/
#ifndef ITEMDEF_HXX
#define ITEMDEF_HXX

#ifndef _SFXMSG_HXX //autogen
#include <sfx2/msg.hxx>
#endif

SFX_DECL_TYPE(10); //SwElemItem
SFX_DECL_TYPE(13); //SwAddPrinterItem
SFX_DECL_TYPE(16); //SwDocDisplayItem

#endif
<commit_msg>INTEGRATION: CWS ooo19126 (1.1.1.1.1462); FILE MERGED 2005/09/05 13:45:19 rt 1.1.1.1.1462.1: #i54170# Change license header: remove SISSL<commit_after>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: itemdef.hxx,v $
 *
 *  $Revision: 1.2 $
 *
 *  last change: $Author: rt $ $Date: 2005-09-09 09:23:16 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/
#ifndef ITEMDEF_HXX
#define ITEMDEF_HXX

#ifndef _SFXMSG_HXX //autogen
#include <sfx2/msg.hxx>
#endif

SFX_DECL_TYPE(10); //SwElemItem
SFX_DECL_TYPE(13); //SwAddPrinterItem
SFX_DECL_TYPE(16); //SwDocDisplayItem

#endif
<|endoftext|>


<commit_before>/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: outline.hxx,v $
 * $Revision: 1.10 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/
#ifndef _OUTLINE_HXX
#define _OUTLINE_HXX

#include <sfx2/tabdlg.hxx>

#include <vcl/menu.hxx>

#ifndef _BUTTON_HXX //autogen
#include <vcl/button.hxx>
#endif
#include <svtools/stdctrl.hxx>

#ifndef _FIXED_HXX //autogen
#include <vcl/fixed.hxx>
#endif

#ifndef _LSTBOX_HXX //autogen
#include <vcl/lstbox.hxx>
#endif

#ifndef _EDIT_HXX //autogen
#include <vcl/edit.hxx>
#endif

#ifndef _FIELD_HXX //autogen
#include <vcl/field.hxx>
#endif

#include "swtypes.hxx"      //fuer MAXLEVEL
#include <numprevw.hxx>
#include <numberingtypelistbox.hxx>

class SwWrtShell;
class SwNumRule;
class SwChapterNumRules;

/* -----------------07.07.98 13:38-------------------
 *
 * --------------------------------------------------*/
class SwOutlineTabDialog : public SfxTabDialog
{
    static     USHORT    nNumLevel;

    String              aNullStr;
    String              aCollNames[MAXLEVEL];
    PopupMenu           aFormMenu;

    SwWrtShell&         rWrtSh;
    SwNumRule*          pNumRule;
    SwChapterNumRules*  pChapterNumRules;

    BOOL                bModified : 1;

    protected:
    DECL_LINK( CancelHdl, Button * );
    DECL_LINK( FormHdl, Button * );
    DECL_LINK( MenuSelectHdl, Menu * );

        virtual void    PageCreated(USHORT nPageId, SfxTabPage& rPage);
        virtual short   Ok();

    public:
        SwOutlineTabDialog(Window* pParent,
                    const SfxItemSet* pSwItemSet,
                    SwWrtShell &);
        ~SwOutlineTabDialog();

    SwNumRule*          GetNumRule() {return pNumRule;}
    USHORT              GetLevel(const String &rFmtName) const;
    String*             GetCollNames() {return aCollNames;}

    static USHORT       GetActNumLevel() {return nNumLevel;}
    static void         SetActNumLevel(USHORT nSet) {nNumLevel = nSet;}
};
/* -----------------07.07.98 13:47-------------------
 *
 * --------------------------------------------------*/
class SwOutlineSettingsTabPage : public SfxTabPage
{
    ListBox         aLevelLB;
    FixedLine       aLevelFL;

    FixedText       aCollLbl;
    ListBox         aCollBox;
    FixedText       aNumberLbl;
    SwNumberingTypeListBox  aNumberBox;
    FixedText       aCharFmtFT;
    ListBox         aCharFmtLB;
    FixedText       aAllLevelFT;
    NumericField    aAllLevelNF;
    FixedText       aDelim;
    FixedText       aPrefixFT;
    Edit            aPrefixED;
    FixedText       aSuffixFT;
    Edit            aSuffixED;
    FixedText       aStartLbl;
    NumericField    aStartEdit;
    FixedLine        aNumberFL;
    NumberingPreview aPreviewWIN;

    String              aNoFmtName;
    String              aSaveCollNames[MAXLEVEL];
    SwWrtShell*         pSh;
    SwNumRule*          pNumRule;
    String*             pCollNames;
    USHORT              nActLevel;

    DECL_LINK( LevelHdl, ListBox * );
    DECL_LINK( ToggleComplete, NumericField * );
    DECL_LINK( CollSelect, ListBox * );
    DECL_LINK( CollSelectGetFocus, ListBox * );
    DECL_LINK( NumberSelect, SwNumberingTypeListBox * );
    DECL_LINK( DelimModify, Edit * );
    DECL_LINK( StartModified, NumericField * );
    DECL_LINK( CharFmtHdl, ListBox * );

    void    Update();

    void    SetModified(){aPreviewWIN.Invalidate();}
    void    CheckForStartValue_Impl(sal_uInt16 nNumberingType);

    using TabPage::ActivatePage;
    using TabPage::DeactivatePage;

public:
    SwOutlineSettingsTabPage(Window* pParent, const SfxItemSet& rSet);
    ~SwOutlineSettingsTabPage();

    void SetWrtShell(SwWrtShell* pShell);

    virtual void        ActivatePage(const SfxItemSet& rSet);
    virtual int         DeactivatePage(SfxItemSet *pSet);

    virtual BOOL        FillItemSet( SfxItemSet& rSet );
    virtual void        Reset( const SfxItemSet& rSet );
    static SfxTabPage*  Create( Window* pParent,
                                const SfxItemSet& rAttrSet);
};
#endif
<commit_msg>INTEGRATION: CWS lcwarnings2 (1.9.182); FILE MERGED 2008/04/17 12:35:09 tl 1.9.182.3: RESYNC: (1.9-1.10); FILE MERGED 2008/03/03 10:09:51 tl 1.9.182.2: #i83458# warning-free code wntmsci11 2008/02/29 14:55:47 tl 1.9.182.1: #i83458# warning free code for wntmsci11<commit_after>/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: outline.hxx,v $
 * $Revision: 1.11 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/
#ifndef _OUTLINE_HXX
#define _OUTLINE_HXX

#include <sfx2/tabdlg.hxx>

#include <vcl/menu.hxx>

#ifndef _BUTTON_HXX //autogen
#include <vcl/button.hxx>
#endif
#include <svtools/stdctrl.hxx>

#ifndef _FIXED_HXX //autogen
#include <vcl/fixed.hxx>
#endif

#ifndef _LSTBOX_HXX //autogen
#include <vcl/lstbox.hxx>
#endif

#ifndef _EDIT_HXX //autogen
#include <vcl/edit.hxx>
#endif

#ifndef _FIELD_HXX //autogen
#include <vcl/field.hxx>
#endif

#include "swtypes.hxx"      //fuer MAXLEVEL
#include <numprevw.hxx>
#include <numberingtypelistbox.hxx>

class SwWrtShell;
class SwNumRule;
class SwChapterNumRules;

/* -----------------07.07.98 13:38-------------------
 *
 * --------------------------------------------------*/
class SwOutlineTabDialog : public SfxTabDialog
{
    static     USHORT    nNumLevel;

    String              aNullStr;
    String              aCollNames[MAXLEVEL];
    PopupMenu           aFormMenu;

    SwWrtShell&         rWrtSh;
    SwNumRule*          pNumRule;
    SwChapterNumRules*  pChapterNumRules;

    BOOL                bModified : 1;

    protected:
    DECL_LINK( CancelHdl, Button * );
    DECL_LINK( FormHdl, Button * );
    DECL_LINK( MenuSelectHdl, Menu * );

        virtual void    PageCreated(USHORT nPageId, SfxTabPage& rPage);
        virtual short   Ok();

    public:
        SwOutlineTabDialog(Window* pParent,
                    const SfxItemSet* pSwItemSet,
                    SwWrtShell &);
        ~SwOutlineTabDialog();

    SwNumRule*          GetNumRule() {return pNumRule;}
    USHORT              GetLevel(const String &rFmtName) const;
    String*             GetCollNames() {return aCollNames;}

    static USHORT       GetActNumLevel() {return nNumLevel;}
    static void         SetActNumLevel(USHORT nSet) {nNumLevel = nSet;}
};
/* -----------------07.07.98 13:47-------------------
 *
 * --------------------------------------------------*/
class SwOutlineSettingsTabPage : public SfxTabPage
{
    ListBox         aLevelLB;
    FixedLine       aLevelFL;

    FixedText       aCollLbl;
    ListBox         aCollBox;
    FixedText       aNumberLbl;
    SwNumberingTypeListBox  aNumberBox;
    FixedText       aCharFmtFT;
    ListBox         aCharFmtLB;
    FixedText       aAllLevelFT;
    NumericField    aAllLevelNF;
    FixedText       aDelim;
    FixedText       aPrefixFT;
    Edit            aPrefixED;
    FixedText       aSuffixFT;
    Edit            aSuffixED;
    FixedText       aStartLbl;
    NumericField    aStartEdit;
    FixedLine        aNumberFL;
    NumberingPreview aPreviewWIN;

    String              aNoFmtName;
    String              aSaveCollNames[MAXLEVEL];
    SwWrtShell*         pSh;
    SwNumRule*          pNumRule;
    String*             pCollNames;
    USHORT              nActLevel;

    DECL_LINK( LevelHdl, ListBox * );
    DECL_LINK( ToggleComplete, NumericField * );
    DECL_LINK( CollSelect, ListBox * );
    DECL_LINK( CollSelectGetFocus, ListBox * );
    DECL_LINK( NumberSelect, SwNumberingTypeListBox * );
    DECL_LINK( DelimModify, Edit * );
    DECL_LINK( StartModified, NumericField * );
    DECL_LINK( CharFmtHdl, ListBox * );

    void    Update();

    void    SetModified(){aPreviewWIN.Invalidate();}
    void    CheckForStartValue_Impl(sal_uInt16 nNumberingType);

    using SfxTabPage::ActivatePage;
    using SfxTabPage::DeactivatePage;

public:
    SwOutlineSettingsTabPage(Window* pParent, const SfxItemSet& rSet);
    ~SwOutlineSettingsTabPage();

    void SetWrtShell(SwWrtShell* pShell);

    virtual void        ActivatePage(const SfxItemSet& rSet);
    virtual int         DeactivatePage(SfxItemSet *pSet);

    virtual BOOL        FillItemSet( SfxItemSet& rSet );
    virtual void        Reset( const SfxItemSet& rSet );
    static SfxTabPage*  Create( Window* pParent,
                                const SfxItemSet& rAttrSet);
};
#endif
<|endoftext|>


<commit_before>/*************************************************************************
 *
 *  $RCSfile: outline.hxx,v $
 *
 *  $Revision: 1.4 $
 *
 *  last change: $Author: os $ $Date: 2002-11-07 14:42:42 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/
#ifndef _OUTLINE_HXX
#define _OUTLINE_HXX

#ifndef _SFXTABDLG_HXX //autogen
#include <sfx2/tabdlg.hxx>
#endif

#ifndef _SV_MENU_HXX //autogen
#include <vcl/menu.hxx>
#endif

#ifndef _BUTTON_HXX //autogen
#include <vcl/button.hxx>
#endif

#ifndef _STDCTRL_HXX //autogen
#include <svtools/stdctrl.hxx>
#endif

#ifndef _FIXED_HXX //autogen
#include <vcl/fixed.hxx>
#endif

#ifndef _LSTBOX_HXX //autogen
#include <vcl/lstbox.hxx>
#endif

#ifndef _EDIT_HXX //autogen
#include <vcl/edit.hxx>
#endif

#ifndef _FIELD_HXX //autogen
#include <vcl/field.hxx>
#endif

#include "swtypes.hxx"      //fuer MAXLEVEL
#ifndef _NUMPREVW_HXX
#include <numprevw.hxx>
#endif
#ifndef _NUMBERINGTYPELISTBOX_HXX
#include <numberingtypelistbox.hxx>
#endif

class SwWrtShell;
class SwTxtFmtColl;
class SwNumRule;
class SwChapterNumRules;

/* -----------------07.07.98 13:38-------------------
 *
 * --------------------------------------------------*/
class SwOutlineTabDialog : public SfxTabDialog
{
    String              aNullStr;
    String              aCollNames[MAXLEVEL];
    PopupMenu           aFormMenu;

    SwWrtShell&         rWrtSh;
    SwNumRule*          pNumRule;
    SwChapterNumRules*  pChapterNumRules;

    USHORT              nNumLevel;
    BOOL                bModified : 1;

    protected:
    DECL_LINK( CancelHdl, Button * );
    DECL_LINK( FormHdl, Button * );
    DECL_LINK( MenuSelectHdl, Menu * );

        virtual void    PageCreated(USHORT nPageId, SfxTabPage& rPage);
        virtual short   Ok();

    public:
        SwOutlineTabDialog(Window* pParent,
                    const SfxItemSet* pSwItemSet,
                    SwWrtShell &);
        ~SwOutlineTabDialog();

    SwNumRule*          GetNumRule() {return pNumRule;}
    USHORT              GetLevel(const String &rFmtName) const;
    String*             GetCollNames() {return aCollNames;}
    USHORT              GetActNumLevel() {return nNumLevel;}
    void                SetActNumLevel(USHORT nSet) {nNumLevel = nSet;}
};
/* -----------------07.07.98 13:47-------------------
 *
 * --------------------------------------------------*/
class SwOutlineSettingsTabPage : public SfxTabPage
{
    ListBox         aLevelLB;
    FixedLine       aLevelFL;

    FixedText       aCollLbl;
    ListBox         aCollBox;
    FixedText       aNumberLbl;
    SwNumberingTypeListBox  aNumberBox;
    FixedText       aCharFmtFT;
    ListBox         aCharFmtLB;
    FixedText       aAllLevelFT;
    NumericField    aAllLevelNF;
    FixedText       aDelim;
    FixedText       aPrefixFT;
    Edit            aPrefixED;
    FixedText       aSuffixFT;
    Edit            aSuffixED;
    FixedText       aStartLbl;
    NumericField    aStartEdit;
    FixedLine        aNumberFL;
    NumberingPreview aPreviewWIN;

    String              aNoFmtName;
    String              aSaveCollNames[MAXLEVEL];
    SwWrtShell*         pSh;
    SwNumRule*          pNumRule;
    String*             pCollNames;
    USHORT              nActLevel;

    DECL_LINK( LevelHdl, ListBox * );
    DECL_LINK( ToggleComplete, NumericField * );
    DECL_LINK( CollSelect, ListBox * );
    DECL_LINK( CollSelectGetFocus, ListBox * );
    DECL_LINK( NumberSelect, SwNumberingTypeListBox * );
    DECL_LINK( DelimModify, Edit * );
    DECL_LINK( StartModified, NumericField * );
    DECL_LINK( CharFmtHdl, ListBox * );

    void    Update();

    void    SetModified(){aPreviewWIN.Invalidate();}
    void    CheckForStartValue_Impl(sal_uInt16 nNumberingType);

public:
    SwOutlineSettingsTabPage(Window* pParent, const SfxItemSet& rSet);
    ~SwOutlineSettingsTabPage();

    void SetWrtShell(SwWrtShell* pShell);

    virtual void        ActivatePage(const SfxItemSet& rSet);
    virtual int         DeactivatePage(SfxItemSet *pSet);
    virtual BOOL        FillItemSet( SfxItemSet& rSet );
    virtual void        Reset( const SfxItemSet& rSet );
    static SfxTabPage*  Create( Window* pParent,
                                const SfxItemSet& rAttrSet);
};
#endif
<commit_msg>INTEGRATION: CWS os52 (1.4.1068); FILE MERGED 2005/02/01 11:13:26 os 1.4.1068.1: #i41387# outline dialog: store current numbering level at runtime<commit_after>/*************************************************************************
 *
 *  $RCSfile: outline.hxx,v $
 *
 *  $Revision: 1.5 $
 *
 *  last change: $Author: kz $ $Date: 2005-03-01 15:28:14 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/
#ifndef _OUTLINE_HXX
#define _OUTLINE_HXX

#ifndef _SFXTABDLG_HXX //autogen
#include <sfx2/tabdlg.hxx>
#endif

#ifndef _SV_MENU_HXX //autogen
#include <vcl/menu.hxx>
#endif

#ifndef _BUTTON_HXX //autogen
#include <vcl/button.hxx>
#endif

#ifndef _STDCTRL_HXX //autogen
#include <svtools/stdctrl.hxx>
#endif

#ifndef _FIXED_HXX //autogen
#include <vcl/fixed.hxx>
#endif

#ifndef _LSTBOX_HXX //autogen
#include <vcl/lstbox.hxx>
#endif

#ifndef _EDIT_HXX //autogen
#include <vcl/edit.hxx>
#endif

#ifndef _FIELD_HXX //autogen
#include <vcl/field.hxx>
#endif

#include "swtypes.hxx"      //fuer MAXLEVEL
#ifndef _NUMPREVW_HXX
#include <numprevw.hxx>
#endif
#ifndef _NUMBERINGTYPELISTBOX_HXX
#include <numberingtypelistbox.hxx>
#endif

class SwWrtShell;
class SwTxtFmtColl;
class SwNumRule;
class SwChapterNumRules;

/* -----------------07.07.98 13:38-------------------
 *
 * --------------------------------------------------*/
class SwOutlineTabDialog : public SfxTabDialog
{
    static     USHORT    nNumLevel;

    String              aNullStr;
    String              aCollNames[MAXLEVEL];
    PopupMenu           aFormMenu;

    SwWrtShell&         rWrtSh;
    SwNumRule*          pNumRule;
    SwChapterNumRules*  pChapterNumRules;

    BOOL                bModified : 1;

    protected:
    DECL_LINK( CancelHdl, Button * );
    DECL_LINK( FormHdl, Button * );
    DECL_LINK( MenuSelectHdl, Menu * );

        virtual void    PageCreated(USHORT nPageId, SfxTabPage& rPage);
        virtual short   Ok();

    public:
        SwOutlineTabDialog(Window* pParent,
                    const SfxItemSet* pSwItemSet,
                    SwWrtShell &);
        ~SwOutlineTabDialog();

    SwNumRule*          GetNumRule() {return pNumRule;}
    USHORT              GetLevel(const String &rFmtName) const;
    String*             GetCollNames() {return aCollNames;}

    static USHORT       GetActNumLevel() {return nNumLevel;}
    static void         SetActNumLevel(USHORT nSet) {nNumLevel = nSet;}
};
/* -----------------07.07.98 13:47-------------------
 *
 * --------------------------------------------------*/
class SwOutlineSettingsTabPage : public SfxTabPage
{
    ListBox         aLevelLB;
    FixedLine       aLevelFL;

    FixedText       aCollLbl;
    ListBox         aCollBox;
    FixedText       aNumberLbl;
    SwNumberingTypeListBox  aNumberBox;
    FixedText       aCharFmtFT;
    ListBox         aCharFmtLB;
    FixedText       aAllLevelFT;
    NumericField    aAllLevelNF;
    FixedText       aDelim;
    FixedText       aPrefixFT;
    Edit            aPrefixED;
    FixedText       aSuffixFT;
    Edit            aSuffixED;
    FixedText       aStartLbl;
    NumericField    aStartEdit;
    FixedLine        aNumberFL;
    NumberingPreview aPreviewWIN;

    String              aNoFmtName;
    String              aSaveCollNames[MAXLEVEL];
    SwWrtShell*         pSh;
    SwNumRule*          pNumRule;
    String*             pCollNames;
    USHORT              nActLevel;

    DECL_LINK( LevelHdl, ListBox * );
    DECL_LINK( ToggleComplete, NumericField * );
    DECL_LINK( CollSelect, ListBox * );
    DECL_LINK( CollSelectGetFocus, ListBox * );
    DECL_LINK( NumberSelect, SwNumberingTypeListBox * );
    DECL_LINK( DelimModify, Edit * );
    DECL_LINK( StartModified, NumericField * );
    DECL_LINK( CharFmtHdl, ListBox * );

    void    Update();

    void    SetModified(){aPreviewWIN.Invalidate();}
    void    CheckForStartValue_Impl(sal_uInt16 nNumberingType);

public:
    SwOutlineSettingsTabPage(Window* pParent, const SfxItemSet& rSet);
    ~SwOutlineSettingsTabPage();

    void SetWrtShell(SwWrtShell* pShell);

    virtual void        ActivatePage(const SfxItemSet& rSet);
    virtual int         DeactivatePage(SfxItemSet *pSet);
    virtual BOOL        FillItemSet( SfxItemSet& rSet );
    virtual void        Reset( const SfxItemSet& rSet );
    static SfxTabPage*  Create( Window* pParent,
                                const SfxItemSet& rAttrSet);
};
#endif
<|endoftext|>


<commit_before>// Copyright (c) 2015-2016 Andrew Sutton
// All rights reserved

#include "constraint.hpp"
#include "ast.hpp"
#include "context.hpp"
#include "call.hpp"
#include "initialization.hpp"
#include "substitution.hpp"
#include "normalization.hpp"
#include "hash.hpp"
#include "print.hpp"
#include "inspection.hpp"

#include <iostream>

namespace banjo
{

// -------------------------------------------------------------------------- //
// Concept expansion

Cons&
expand_expr(Context& cxt, Expression_def& def, Substitution& sub)
{
  Expr& e = substitute(cxt, def.expression(), sub);
  return normalize(cxt, e);
}

// This is backwards from the previous case where we substitute
// first and normalize second. I believe that these operations are
// interchangeable.
Cons&
expand_def(Context& cxt, Concept_def& def, Substitution& sub)
{
  Cons& c1 = normalize(cxt, def);
  return substitute(cxt, c1, sub);
}

// Expand the concept by substituting the template arguments
// throughthe concept's definition and normalizing the result.
//
// TODO: Memoize the expansions.
Cons&
expand(Context& cxt, Concept_cons& c)
{
  Concept_decl& d = c.declaration();
  Decl_list& tparms = d.parameters();
  Term_list& targs = c.arguments();

  // NOTE: Template arguments must have been checked (in kind?)
  // prior to the formation of the constraint. It's should be
  // a semantic requirement of the original check expression.
  Substitution sub(tparms, targs);

  Def& def = d.definition();
  if (Expression_def* expr = as<Expression_def>(&def))
    return expand_expr(cxt, *expr, sub);
  if (Concept_def* body = as<Concept_def>(&def))
    return expand_def(cxt, *body, sub);
  banjo_unhandled_case(def);
}

Cons const&
expand(Context& cxt, Concept_cons const& c)
{
  return expand(cxt, const_cast<Concept_cons&>(c));
}

// -------------------------------------------------------------------------- //
// Admissibility of expressions
//
// Determine the admissibility of an expression within a dependent
// context. Note that this is the slow way of doing it since we
// are doing a full recursion over the constraint in order to find
// a proof of admissibility.

inline Expr*
admit_concept_expr(Context& cxt, Concept_cons& c, Expr& e)
{
  return admit_expression(cxt, expand(cxt, c), e);
}

// This is plain weird, and I should probably never be here. When
// does a reference-expression appear as a constraint?
template<typename Usage>
Expr*
admit_reference_expr(Context& cxt, Usage& c, Reference_expr& e)
{
  return &e;
}

// For both basic and conversion constraints. Determine if the operands
// of e can be converted to the declared types of the required expression.
// If so, return e with its result type adjusted to that of the required
// expression.
//
// Note that for a conversion constraint, the result type of the matched
// expression is *not* the destinatio type. It is the expression type.
template<typename Usage>
Expr*
admit_binary_expr(Context& cxt, Usage& c, Binary_expr& e)
{
  // Determine if the operands can be converted to the
  // declared type of the required expressin.
  Binary_expr& a = cast<Binary_expr>(c.expression());
  Type& t1 = declared_type(a.left());
  Type& t2 = declared_type(a.right());
  try {
    copy_initialize(cxt, t1, e.left());
    copy_initialize(cxt, t2, e.right());
  } catch(Translation_error&) {
    return nullptr;
  }

  // Adjust the type of the expression under test to that of
  // the required expression.
  //
  // FIXME: See the notes on admit_binary_conv. We may want
  // to preserve the conversions for the purpose of ordering.
  e.ty = &a.type();
  return &e;
}

// Determine if a basic or conversion constraint admit the call
// expression e.
template<typename Usage>
Expr*
admit_call_expr(Context& cxt, Usage& c, Call_expr& e)
{
  Call_expr& a = cast<Call_expr>(c.expression());

  // Build the list of parameter types from the declared types
  // of operands in the constraint.
  Type_list ts {&declared_type(a.function())};
  for (Expr& e0 : a.arguments())
    ts.push_back(declared_type(e0));

  // Build the list of arguments from e. Note that the first
  // argument is actually the function.
  Expr_list es {&e.function()};
  for (Expr& e0 : e.arguments())
    es.push_back(e0);

  // If conversion fails, this is not accessible.
  try {
    initialize_parameters(cxt, ts, es);
  } catch (Translation_error&) {
    return nullptr;
  }

  // Adjust the type and admit the expression.
  e.ty = &a.type();
  return &e;
}

// For both basic and conversion constraints, determine if the constraints
// admits the expression. In general, this is the case when the operands
// of the e are dependently convertible to the types of the expression in c.
template<typename Usage>
Expr*
admit_usage_expr(Context& cxt, Usage& c, Expr& e)
{
  struct fn
  {
    Context& cxt;
    Usage&   c;
    Expr* operator()(Expr& e)           { banjo_unhandled_case(e); }
    Expr* operator()(Reference_expr& e) { return admit_reference_expr(cxt, c, e); }
    Expr* operator()(Binary_expr& e)    { return admit_binary_expr(cxt, c, e); }
    Expr* operator()(Call_expr& e)      { return admit_call_expr(cxt, c, e); }
  };

  // An expression of a different kind prove admissibility.
  if (typeid(c.expression()) != typeid(e))
    return nullptr;

  return apply(e, fn{cxt, c});
}

// Just look through to the nested constraint.
inline Expr*
admit_parametric_expr(Context& cxt, Parameterized_cons& c, Expr& e)
{
  return admit_expression(cxt, c.constraint(), e);
}

// An expression is admissible for a conjunction of assumptions if
// support is found in either the left or right operand.
Expr*
admit_conjunction_expr(Context& cxt, Conjunction_cons& c, Expr& e)
{
  if (Expr* e1 = admit_expression(cxt, c.left(), e))
    return e1;
  else
    return admit_expression(cxt, c.right(), e);
}

// An expression is admissible for a disjinction of assumptions if
// support is found both the left and right operand.
inline Expr*
admit_disjunction_expr(Context& cxt, Disjunction_cons& c, Expr& e)
{
  if (Expr* e1 = admit_expression(cxt, c.left(), e)) {
    if (Expr* e2 = admit_expression(cxt, c.right(), e)) {
      if (!is_equivalent(e1->type(), e2->type()))
        throw Translation_error(cxt, "multiple types deduced for '{}'", e);
      return e1;
    }
  }
  return nullptr;
}

// Determine if the expression `e` is admissible under the given
// constraint set. Returns a fully typed expression if admissible
// and nullptr if not.
//
// Note that a predicate constraint does not admit any expressions.
// That will change when we add axioms.
//
// FIXME: This needs to be a deduction algorithm. That is, we
// want find a substitution form `e` to some assumed expression.
//
// FIXME: What happens if we have requirements like these:
//
//    |e : t1| /\ |e : t2|
//
// This only seems illogical to assume contradictory things. However,
// from the perspective of checking it doesn't actually matter since
// any proof requires only that e have type t1 or t2. Not both.
//
// Then there's this:
//
//    |e : t1| \/ |e : t2|
//
// Here, we have to show that both are satisfied. I would think that t
// his is never satisfiable since e could never have different types.
// However, we *could* defer that resolution by creating an intersection
// type. But that might not fly.
Expr*
admit_expression(Context& cxt, Cons& c, Expr& e)
{
  struct fn
  {
    Context& cxt;
    Expr&    e;
    Expr* operator()(Cons& c)               { banjo_unhandled_case(c); }
    Expr* operator()(Concept_cons& c)       { return admit_concept_expr(cxt, c, e); }
    Expr* operator()(Predicate_cons& c)     { return nullptr; }
    Expr* operator()(Expression_cons& c)    { return admit_usage_expr(cxt, c, e); }
    Expr* operator()(Conversion_cons& c)    { return admit_usage_expr(cxt, c, e); }
    Expr* operator()(Parameterized_cons& c) { return admit_parametric_expr(cxt, c, e); }
    Expr* operator()(Conjunction_cons& c)   { return admit_conjunction_expr(cxt, c, e); }
    Expr* operator()(Disjunction_cons& c)   { return admit_disjunction_expr(cxt, c, e); }
  };
  // note("admit expr '{}' in '{}'", e, c);
  return apply(c, fn{cxt, e});
}

Expr*
admit_expression(Context& cxt, Expr& c, Expr& e)
{
  return admit_expression(cxt, normalize(cxt, c), e);
}

// -------------------------------------------------------------------------- //
// Availability of conversions

Expr*
admit_concept_conv(Context& cxt, Concept_cons& c, Expr& e, Type& t)
{
  return admit_conversion(cxt, expand(cxt, c), e, t);
}

// Note that, within this function, the the conversion's type
// is equivalent to the destination type.
//
// FIXME: Factor out the "conformance checking" code.
Expr*
admit_binary_conv(Context& cxt, Conversion_cons& c, Binary_expr& e)
{
  // Determine if the operands can be converted to the
  // declared type of the required expressin.
  Expr *c1, *c2;
  Binary_expr& a = cast<Binary_expr>(c.expression());
  Type& t1 = declared_type(a.left());
  Type& t2 = declared_type(a.right());
  try {
    c1 = &copy_initialize(cxt, t1, e.left());
    c2 = &copy_initialize(cxt, t2, e.right());
  } catch(Translation_error&) {
    return nullptr;
  }

  // Adjust the type of the expression under test to that of
  // the required expression.
  //
  // FIXME: It's possible that we need to preserve the original
  // conversions in order to sort candidates. Consider:
  //
  //    requires (T a, T const b) {
  //      f(a) -> T;        // #1
  //      f(b) -> T const;  // #2
  //    }
  //
  // In an algorithm that uses a f(x) where x is const, we would
  // prefer #1. Perhaps we should collect viable conversion
  // and then sort at the end. Note that this is true for simple
  // typings also.
  //
  // FIXME: Add constructors to the builder. This is just plain dumb.
  return new Dependent_conv(c.type(), e);
}

Expr*
admit_usage_conv(Context& cxt, Conversion_cons& c, Expr& e, Type& t)
{
  struct fn
  {
    Context&         cxt;
    Conversion_cons& c;
    Expr* operator()(Expr& e)        { banjo_unhandled_case(e); }
    Expr* operator()(Binary_expr& e) { return admit_binary_conv(cxt, c, e); }
  };

  // An expression of a different kind prove admissibility.
  Expr& e2 = c.expression();
  if (typeid(e2) != typeid(e))
    return nullptr;

  // If the expression's type is not equivalent to t, this constraint
  // does not prove admissibility.
  if (!is_equivalent(c.type(), t))
    return nullptr;

  return apply(e, fn{cxt, c});
}

Expr*
admit_parametric_conv(Context& cxt, Parameterized_cons& c, Expr& e, Type& t)
{
  return admit_conversion(cxt, c.constraint(), e, t);
}

Expr*
admit_conjunction_conv(Context& cxt, Conjunction_cons& c, Expr& e, Type& t)
{
  if (Expr* c1 = admit_conversion(cxt, c.left(), e, t))
    return c1;
  else
    return admit_conversion(cxt, c.right(), e, t);
}

Expr*
admit_disjunction_conv(Context& cxt, Disjunction_cons& c, Expr& e, Type& t)
{
  // Note that if we find evidence for a conversion in both branches
  // then it is guaranteed that they are equivalent.
  if (Expr* c1 = admit_conversion(cxt, c.left(), e, t)) {
    if (admit_conversion(cxt, c.right(), e, t))
      return c1;
  }
  return nullptr;
}

// TODO: Factor out the traversal of constraints in order to avoid
// repeating all this bolier plate?
Expr*
admit_conversion(Context& cxt, Cons& c, Expr& e, Type& t)
{
  struct fn
  {
    Context& cxt;
    Expr&    e;
    Type&    t;
    Expr* operator()(Cons& c)               { banjo_unhandled_case(c); }
    Expr* operator()(Concept_cons& c)       { return admit_concept_conv(cxt, c, e, t); }
    Expr* operator()(Expression_cons& c)    { return nullptr; }
    Expr* operator()(Conversion_cons& c)    { return admit_usage_conv(cxt, c, e, t); }
    Expr* operator()(Parameterized_cons& c) { return admit_parametric_conv(cxt, c, e, t); }
    Expr* operator()(Conjunction_cons& c)   { return admit_conjunction_conv(cxt, c, e, t); }
    Expr* operator()(Disjunction_cons& c)   { return admit_disjunction_conv(cxt, c, e, t); }
  };
  // note("admit conv '{}' to '{}' in '{}'", e, t, c);
  return apply(c, fn{cxt, e, t});
}

Expr*
admit_conversion(Context& cxt, Expr& c, Expr& e, Type& t)
{
  return admit_conversion(cxt, normalize(cxt, c), e, t);
}

} // namespace banjo
<commit_msg>Remove unused variables.<commit_after>// Copyright (c) 2015-2016 Andrew Sutton
// All rights reserved

#include "constraint.hpp"
#include "ast.hpp"
#include "context.hpp"
#include "call.hpp"
#include "initialization.hpp"
#include "substitution.hpp"
#include "normalization.hpp"
#include "hash.hpp"
#include "print.hpp"
#include "inspection.hpp"

#include <iostream>

namespace banjo
{

// -------------------------------------------------------------------------- //
// Concept expansion

Cons&
expand_expr(Context& cxt, Expression_def& def, Substitution& sub)
{
  Expr& e = substitute(cxt, def.expression(), sub);
  return normalize(cxt, e);
}

// This is backwards from the previous case where we substitute
// first and normalize second. I believe that these operations are
// interchangeable.
Cons&
expand_def(Context& cxt, Concept_def& def, Substitution& sub)
{
  Cons& c1 = normalize(cxt, def);
  return substitute(cxt, c1, sub);
}

// Expand the concept by substituting the template arguments
// throughthe concept's definition and normalizing the result.
//
// TODO: Memoize the expansions.
Cons&
expand(Context& cxt, Concept_cons& c)
{
  Concept_decl& d = c.declaration();
  Decl_list& tparms = d.parameters();
  Term_list& targs = c.arguments();

  // NOTE: Template arguments must have been checked (in kind?)
  // prior to the formation of the constraint. It's should be
  // a semantic requirement of the original check expression.
  Substitution sub(tparms, targs);

  Def& def = d.definition();
  if (Expression_def* expr = as<Expression_def>(&def))
    return expand_expr(cxt, *expr, sub);
  if (Concept_def* body = as<Concept_def>(&def))
    return expand_def(cxt, *body, sub);
  banjo_unhandled_case(def);
}

Cons const&
expand(Context& cxt, Concept_cons const& c)
{
  return expand(cxt, const_cast<Concept_cons&>(c));
}

// -------------------------------------------------------------------------- //
// Admissibility of expressions
//
// Determine the admissibility of an expression within a dependent
// context. Note that this is the slow way of doing it since we
// are doing a full recursion over the constraint in order to find
// a proof of admissibility.

inline Expr*
admit_concept_expr(Context& cxt, Concept_cons& c, Expr& e)
{
  return admit_expression(cxt, expand(cxt, c), e);
}

// This is plain weird, and I should probably never be here. When
// does a reference-expression appear as a constraint?
template<typename Usage>
Expr*
admit_reference_expr(Context& cxt, Usage& c, Reference_expr& e)
{
  return &e;
}

// For both basic and conversion constraints. Determine if the operands
// of e can be converted to the declared types of the required expression.
// If so, return e with its result type adjusted to that of the required
// expression.
//
// Note that for a conversion constraint, the result type of the matched
// expression is *not* the destinatio type. It is the expression type.
template<typename Usage>
Expr*
admit_binary_expr(Context& cxt, Usage& c, Binary_expr& e)
{
  // Determine if the operands can be converted to the
  // declared type of the required expressin.
  Binary_expr& a = cast<Binary_expr>(c.expression());
  Type& t1 = declared_type(a.left());
  Type& t2 = declared_type(a.right());
  try {
    copy_initialize(cxt, t1, e.left());
    copy_initialize(cxt, t2, e.right());
  } catch(Translation_error&) {
    return nullptr;
  }

  // Adjust the type of the expression under test to that of
  // the required expression.
  //
  // FIXME: See the notes on admit_binary_conv. We may want
  // to preserve the conversions for the purpose of ordering.
  e.ty = &a.type();
  return &e;
}

// Determine if a basic or conversion constraint admit the call
// expression e.
template<typename Usage>
Expr*
admit_call_expr(Context& cxt, Usage& c, Call_expr& e)
{
  Call_expr& a = cast<Call_expr>(c.expression());

  // Build the list of parameter types from the declared types
  // of operands in the constraint.
  Type_list ts {&declared_type(a.function())};
  for (Expr& e0 : a.arguments())
    ts.push_back(declared_type(e0));

  // Build the list of arguments from e. Note that the first
  // argument is actually the function.
  Expr_list es {&e.function()};
  for (Expr& e0 : e.arguments())
    es.push_back(e0);

  // If conversion fails, this is not accessible.
  try {
    initialize_parameters(cxt, ts, es);
  } catch (Translation_error&) {
    return nullptr;
  }

  // Adjust the type and admit the expression.
  e.ty = &a.type();
  return &e;
}

// For both basic and conversion constraints, determine if the constraints
// admits the expression. In general, this is the case when the operands
// of the e are dependently convertible to the types of the expression in c.
template<typename Usage>
Expr*
admit_usage_expr(Context& cxt, Usage& c, Expr& e)
{
  struct fn
  {
    Context& cxt;
    Usage&   c;
    Expr* operator()(Expr& e)           { banjo_unhandled_case(e); }
    Expr* operator()(Reference_expr& e) { return admit_reference_expr(cxt, c, e); }
    Expr* operator()(Binary_expr& e)    { return admit_binary_expr(cxt, c, e); }
    Expr* operator()(Call_expr& e)      { return admit_call_expr(cxt, c, e); }
  };

  // An expression of a different kind prove admissibility.
  if (typeid(c.expression()) != typeid(e))
    return nullptr;

  return apply(e, fn{cxt, c});
}

// Just look through to the nested constraint.
inline Expr*
admit_parametric_expr(Context& cxt, Parameterized_cons& c, Expr& e)
{
  return admit_expression(cxt, c.constraint(), e);
}

// An expression is admissible for a conjunction of assumptions if
// support is found in either the left or right operand.
Expr*
admit_conjunction_expr(Context& cxt, Conjunction_cons& c, Expr& e)
{
  if (Expr* e1 = admit_expression(cxt, c.left(), e))
    return e1;
  else
    return admit_expression(cxt, c.right(), e);
}

// An expression is admissible for a disjinction of assumptions if
// support is found both the left and right operand.
inline Expr*
admit_disjunction_expr(Context& cxt, Disjunction_cons& c, Expr& e)
{
  if (Expr* e1 = admit_expression(cxt, c.left(), e)) {
    if (Expr* e2 = admit_expression(cxt, c.right(), e)) {
      if (!is_equivalent(e1->type(), e2->type()))
        throw Translation_error(cxt, "multiple types deduced for '{}'", e);
      return e1;
    }
  }
  return nullptr;
}

// Determine if the expression `e` is admissible under the given
// constraint set. Returns a fully typed expression if admissible
// and nullptr if not.
//
// Note that a predicate constraint does not admit any expressions.
// That will change when we add axioms.
//
// FIXME: This needs to be a deduction algorithm. That is, we
// want find a substitution form `e` to some assumed expression.
//
// FIXME: What happens if we have requirements like these:
//
//    |e : t1| /\ |e : t2|
//
// This only seems illogical to assume contradictory things. However,
// from the perspective of checking it doesn't actually matter since
// any proof requires only that e have type t1 or t2. Not both.
//
// Then there's this:
//
//    |e : t1| \/ |e : t2|
//
// Here, we have to show that both are satisfied. I would think that t
// his is never satisfiable since e could never have different types.
// However, we *could* defer that resolution by creating an intersection
// type. But that might not fly.
Expr*
admit_expression(Context& cxt, Cons& c, Expr& e)
{
  struct fn
  {
    Context& cxt;
    Expr&    e;
    Expr* operator()(Cons& c)               { banjo_unhandled_case(c); }
    Expr* operator()(Concept_cons& c)       { return admit_concept_expr(cxt, c, e); }
    Expr* operator()(Predicate_cons& c)     { return nullptr; }
    Expr* operator()(Expression_cons& c)    { return admit_usage_expr(cxt, c, e); }
    Expr* operator()(Conversion_cons& c)    { return admit_usage_expr(cxt, c, e); }
    Expr* operator()(Parameterized_cons& c) { return admit_parametric_expr(cxt, c, e); }
    Expr* operator()(Conjunction_cons& c)   { return admit_conjunction_expr(cxt, c, e); }
    Expr* operator()(Disjunction_cons& c)   { return admit_disjunction_expr(cxt, c, e); }
  };
  // note("admit expr '{}' in '{}'", e, c);
  return apply(c, fn{cxt, e});
}

Expr*
admit_expression(Context& cxt, Expr& c, Expr& e)
{
  return admit_expression(cxt, normalize(cxt, c), e);
}

// -------------------------------------------------------------------------- //
// Availability of conversions

Expr*
admit_concept_conv(Context& cxt, Concept_cons& c, Expr& e, Type& t)
{
  return admit_conversion(cxt, expand(cxt, c), e, t);
}

// Note that, within this function, the the conversion's type
// is equivalent to the destination type.
//
// FIXME: Factor out the "conformance checking" code.
Expr*
admit_binary_conv(Context& cxt, Conversion_cons& c, Binary_expr& e)
{
  // Determine if the operands can be converted to the
  // declared type of the required expressin.
  Binary_expr& a = cast<Binary_expr>(c.expression());
  Type& t1 = declared_type(a.left());
  Type& t2 = declared_type(a.right());
  try {
    copy_initialize(cxt, t1, e.left());
    copy_initialize(cxt, t2, e.right());
  } catch(Translation_error&) {
    return nullptr;
  }

  // Adjust the type of the expression under test to that of
  // the required expression.
  //
  // FIXME: It's possible that we need to preserve the original
  // conversions in order to sort candidates. Consider:
  //
  //    requires (T a, T const b) {
  //      f(a) -> T;        // #1
  //      f(b) -> T const;  // #2
  //    }
  //
  // In an algorithm that uses a f(x) where x is const, we would
  // prefer #1. Perhaps we should collect viable conversion
  // and then sort at the end. Note that this is true for simple
  // typings also.
  //
  // FIXME: Add constructors to the builder. This is just plain dumb.
  return new Dependent_conv(c.type(), e);
}

Expr*
admit_usage_conv(Context& cxt, Conversion_cons& c, Expr& e, Type& t)
{
  struct fn
  {
    Context&         cxt;
    Conversion_cons& c;
    Expr* operator()(Expr& e)        { banjo_unhandled_case(e); }
    Expr* operator()(Binary_expr& e) { return admit_binary_conv(cxt, c, e); }
  };

  // An expression of a different kind prove admissibility.
  Expr& e2 = c.expression();
  if (typeid(e2) != typeid(e))
    return nullptr;

  // If the expression's type is not equivalent to t, this constraint
  // does not prove admissibility.
  if (!is_equivalent(c.type(), t))
    return nullptr;

  return apply(e, fn{cxt, c});
}

Expr*
admit_parametric_conv(Context& cxt, Parameterized_cons& c, Expr& e, Type& t)
{
  return admit_conversion(cxt, c.constraint(), e, t);
}

Expr*
admit_conjunction_conv(Context& cxt, Conjunction_cons& c, Expr& e, Type& t)
{
  if (Expr* c1 = admit_conversion(cxt, c.left(), e, t))
    return c1;
  else
    return admit_conversion(cxt, c.right(), e, t);
}

Expr*
admit_disjunction_conv(Context& cxt, Disjunction_cons& c, Expr& e, Type& t)
{
  // Note that if we find evidence for a conversion in both branches
  // then it is guaranteed that they are equivalent.
  if (Expr* c1 = admit_conversion(cxt, c.left(), e, t)) {
    if (admit_conversion(cxt, c.right(), e, t))
      return c1;
  }
  return nullptr;
}

// TODO: Factor out the traversal of constraints in order to avoid
// repeating all this bolier plate?
Expr*
admit_conversion(Context& cxt, Cons& c, Expr& e, Type& t)
{
  struct fn
  {
    Context& cxt;
    Expr&    e;
    Type&    t;
    Expr* operator()(Cons& c)               { banjo_unhandled_case(c); }
    Expr* operator()(Concept_cons& c)       { return admit_concept_conv(cxt, c, e, t); }
    Expr* operator()(Expression_cons& c)    { return nullptr; }
    Expr* operator()(Conversion_cons& c)    { return admit_usage_conv(cxt, c, e, t); }
    Expr* operator()(Parameterized_cons& c) { return admit_parametric_conv(cxt, c, e, t); }
    Expr* operator()(Conjunction_cons& c)   { return admit_conjunction_conv(cxt, c, e, t); }
    Expr* operator()(Disjunction_cons& c)   { return admit_disjunction_conv(cxt, c, e, t); }
  };
  // note("admit conv '{}' to '{}' in '{}'", e, t, c);
  return apply(c, fn{cxt, e, t});
}

Expr*
admit_conversion(Context& cxt, Expr& c, Expr& e, Type& t)
{
  return admit_conversion(cxt, normalize(cxt, c), e, t);
}

} // namespace banjo
<|endoftext|>


<commit_before>#include <cassert>
#include <var>
#include <map>
#include <fstream>

using namespace std;

var getString()
{
    cout << "In getString()" << endl;
    var str;
    str = "An example string with spaces.";
    return str;
}

void useString(var s)
{
    // The ampersand suppresses the double quotes by passing the raw
    // char* to the format operator.
    cout << "Using: " << &s << endl;
}

int main(int argc, char** argv)
{
    // Copy the command line
    var arg(argc, argv);

    // Check that one and two level indexing works
    cout << "Arg: " << arg << endl;
    cout << "Arg[0]: " << arg[0] << endl;
    cout << "Arg[0][0]: " << arg[0][0] << endl;

    // Check that operator == works
    if (arg[0][0] == '.')
        cout << "It's a dot" << endl;
    else
        cout << "It's not a dot" << endl;

    // Check that index() finds (or not) an element
    if (arg.index("-f"))
        cout << "There's a -f" << endl;
    else
        cout << "There's no -f" << endl;

    // Basic numerical tests
    var s, w, x, y, z, dummy;
    cout << (s ? "true" : "false") << endl;
    cout << (!s ? "true" : "false") << endl;
    w = 'w';
    x = 2;
    y = 3.14;
    z = y.cos();
    s = "Hello!";
    cout << (s ? "true" : "false") << endl;
    cout << (!s ? "true" : "false") << endl;

    cout << "Var size is " << sizeof(var) << endl;
    cout << w << endl;
    cout << x << endl;
    cout << y << endl;
    cout << z << endl;
    cout << s << endl;
    cout << s.at(0) << endl;
    cout << s[0] << endl;

    // Numerical overloads
    x += 2;
    y += 3.14f;
    x.push(2);
    y -= 1;
    cout << x << endl;
    cout << y << endl;

    // Reference the same object & reference counting
    var a = s;
    cout << "a is: " << a << endl;
    a = "New string";

    // String comparison
    var n = "New string";
    if (a == n)
        cout << "equal" << endl;
    else
        cout << "not equal" << endl;

    // Pass a string into a function, it's still valid outside
    cout << "Calling getString()" << endl;
    var b = getString();
    cout << "b is: " << b << endl;
    useString(b);
    cout << "b is: " << b << endl;

    // Basic string split
    var sp;
    sp = b.split("n");
    cout << "sp is: " << sp << endl;

    // String strip
    var ss = "  Hello ";
    cout << ss;
    cout << " strips to ";
    cout << ss.strip() << endl;

    // Basic string insert
    a.insert("ddd", 1);
    cout << "a is: " << a << endl;
    a.append("aaa");
    cout << "a is: " << a << endl;

    // String sprintf
    var str;
    cout << "sprintf: " << str.sprintf("This string is %d %f", 1, 0.1) << endl;

    // Shifting of command line
    arg.insert("insert", 1);
    cout << "arg is: " << arg << endl;
    arg.remove(0);
    cout << "arg is: " << arg << endl;
    var as = arg.shift();
    cout << "arg is: " << arg << " shifted: " << as << endl;

    // Join the command line args
    cout << "Joining: " << arg << endl;
    cout << "Joined: " << arg.join("-") << endl;

    // Sort the command line args
    cout << arg.sort() << endl;

    // Check that we can act as a std::map key (just needs operator<)
    map<var, int> map;
    map["One"] = 1;
    map["Two"] = 2;
    map["Three"] = 3;
    cout << map.count("Zero") << endl;
    cout << map.count("One") << endl;

    // Read a small file
    var f;
    var t;
    ifstream is("tests.txt", ifstream::in);
    if (is.fail())
        cout << "Open failed" << endl;
    while (f.getline(is))
    {
        cout << f << endl;
        t.push(f.copy());
    }
    cout << t << endl;

    // Shallow copy
    var c1 = arg;
    c1.push("extra");
    cout << c1 << endl;
    cout << arg << endl;
    var c2 = arg.copy();
    c2.pop();
    cout << c2 << endl;
    cout << arg << endl;

    // Modify an array using operator[]
    int xxa[] = {1,2,3,4,5};
    var xa(5, xxa);
    cout << "xa is " << xa << endl;
    xa[1] = xa[0];
    xa[2] += xa[1];
    xa[3] = 7;
    xa[4] += 7;
    cout << "xa is " << xa << endl;

    // Array of vars by pushing
    var arr1;
    arr1.push("Hi!");
    cout << arr1 << endl;

    // Array of vars just by assigning indeces
    var arr2;
    arr2[1] = "Hi!";
    cout << arr2 << endl;

    // Array indexed by var
    var vmap;
    vmap["one"] = 1;
    vmap["two"] = 2;
    vmap["three"] = 3;
    cout << "vmap[0] is " << vmap[0] << endl;
    cout << "vmap[1] is " << vmap[1] << endl;
    cout << "vmap[\"three\"] is " << vmap["three"] << endl;
    cout << "vmap is " << vmap << endl;
    cout << "vmap.copy() is " << vmap.copy() << endl;

    // Multi-dimensional array
    var iarr;
    iarr[1][2] = 3;
    iarr[1][4] = 5;
    cout << "iarr is " << iarr << endl;

    // Multi-dimensional map
    var wmap;
    wmap["one"]["two"] = "three";
    wmap["one"]["four"] = "five";
    cout << "wmap is " << wmap << endl;

    // Init from variable argument list
    var ai(5, 5,4,3,2,1);
    cout << "ai is: " << ai << endl;

    // Read a text file via a dynamic library
    vfile txtf("txt");
    var txt = txtf.read("tests.txt");
    cout << "Loaded: " << txt << endl;

    // Read a .ini file via a dynamic library
    vfile inif("ini");
    var ini = inif.read("tests.ini");
    cout << "Loaded: " << ini << endl;

    // Plot something with gnuplot
    var gnu;
    gnu.push("plot sin(x), \"-\"");
    gnu.push(ai);
    vfile gnuf("gnuplot");
    gnuf.write("tests.eps", gnu);

    // Tensors
    var ts = 0.0f;
    ts.resize(16);
    for (int i=0; i<16; i++)
        ts[i] = (float)i;
    var t1 = ts.view(2, 4, 4);
    var t2 = ts.view(3, 2, 2, 4);
    cout << ts << endl;
    cout << t1 << endl;
    cout << t2 << endl;
    cout << "t1(1,2): " << t1(1,2) << endl;
    cout << "t2(1,1,2): " << t2(1,1,2) << endl;
    t1(1,2) = 2.3f;
    cout << t1 << endl;

    // BLAS
    var bt(4, 1.0f, 1.2, 0.8, -2.0);
    cout << bt << "  sums to " << bt.sum()  << endl;
    cout << bt << " asums to " << bt.asum() << endl;

    // Stream
    vstream vstr;
    float fl = 23e-1f;
    vstr << "H";
    cout << vstr.var() << endl;
    vstr << "ello: " << fl;
    cout << vstr.var() << endl;

    // gedcom
    vfile gedf("ged");
    var ged = gedf.read("tests.ged");
    cout << "Loaded: " << ged << endl;

    // XML
    vfile xmlf("xml");
    var xml = xmlf.read("tests.xml");
    cout << "Loaded: " << xml << endl;

    // wav
    vfile wavf("snd");
    var wav = wavf.read("tests.wav");
    cout << "Loaded wav file:" << endl;
    cout << " rate:     " << wav["rate"] << endl;
    cout << " channels: " << wav["channels"] << endl;
    cout << " size:     " << wav["data"].size() << endl;

    // Exception
    try {
        throw vruntime_error(bt);
    }
    catch (vruntime_error e) {
        cout << "Caught: " << e.what() << endl;
    };

    // Init by overloading operator,()
    var comma;
    comma = 1.2, 2.0, 4, 5;
    cout << "Comma is: " << comma << endl;

    // Regular expressions
    cout << "Searching ello: " << ss << endl;
    if (ss.search("ello"))
        cout << "Matches" << endl;
    else
        cout << "Matches not" << endl;
    cout << "Matching \\S+ell\\S: " << ss << endl;
    if (ss.search("\\S+ell\\S"))
        cout << "Matches" << endl;
    else
        cout << "Matches not" << endl;
    var rep = ss.replace("lo", "ls bells");
    cout << "Replaced to: " << rep << endl;
    return 0;
}
<commit_msg>Wrap the tests that leak memory in a flag so valgrind runs clean<commit_after>#include <cassert>
#include <var>
#include <map>
#include <fstream>

using namespace std;

var getString()
{
    cout << "In getString()" << endl;
    var str;
    str = "An example string with spaces.";
    return str;
}

void useString(var s)
{
    // The ampersand suppresses the double quotes by passing the raw
    // char* to the format operator.
    cout << "Using: " << &s << endl;
}

int main(int argc, char** argv)
{
    // Copy the command line
    var arg(argc, argv);

    // Check that one and two level indexing works
    cout << "Arg: " << arg << endl;
    cout << "Arg[0]: " << arg[0] << endl;
    cout << "Arg[0][0]: " << arg[0][0] << endl;

    // Check that operator == works
    if (arg[0][0] == '.')
        cout << "It's a dot" << endl;
    else
        cout << "It's not a dot" << endl;

    // Check that index() finds (or not) an element
    if (arg.index("-f"))
        cout << "There's a -f" << endl;
    else
        cout << "There's no -f" << endl;

    // Given command line working, set a flag for valgrind
    bool vg = true;
    if (arg.index("-vg"))
        vg = false;

    // Basic numerical tests
    var s, w, x, y, z, dummy;
    cout << (s ? "true" : "false") << endl;
    cout << (!s ? "true" : "false") << endl;
    w = 'w';
    x = 2;
    y = 3.14;
    z = y.cos();
    s = "Hello!";
    cout << (s ? "true" : "false") << endl;
    cout << (!s ? "true" : "false") << endl;

    cout << "Var size is " << sizeof(var) << endl;
    cout << w << endl;
    cout << x << endl;
    cout << y << endl;
    cout << z << endl;
    cout << s << endl;
    cout << s.at(0) << endl;
    cout << s[0] << endl;

    // Numerical overloads
    x += 2;
    y += 3.14f;
    x.push(2);
    y -= 1;
    cout << x << endl;
    cout << y << endl;

    // Reference the same object & reference counting
    var a = s;
    cout << "a is: " << a << endl;
    a = "New string";

    // String comparison
    var n = "New string";
    if (a == n)
        cout << "equal" << endl;
    else
        cout << "not equal" << endl;

    // Pass a string into a function, it's still valid outside
    cout << "Calling getString()" << endl;
    var b = getString();
    cout << "b is: " << b << endl;
    useString(b);
    cout << "b is: " << b << endl;

    // Basic string split
    var sp;
    sp = b.split("n");
    cout << "sp is: " << sp << endl;

    // String strip
    var ss = "  Hello ";
    cout << ss;
    cout << " strips to ";
    cout << ss.strip() << endl;

    // Basic string insert
    a.insert("ddd", 1);
    cout << "a is: " << a << endl;
    a.append("aaa");
    cout << "a is: " << a << endl;

    // String sprintf
    var str;
    cout << "sprintf: " << str.sprintf("This string is %d %f", 1, 0.1) << endl;

    // Shifting of command line
    arg.insert("insert", 1);
    cout << "arg is: " << arg << endl;
    arg.remove(0);
    cout << "arg is: " << arg << endl;
    var as = arg.shift();
    cout << "arg is: " << arg << " shifted: " << as << endl;

    // Join the command line args
    cout << "Joining: " << arg << endl;
    cout << "Joined: " << arg.join("-") << endl;

    // Sort the command line args
    cout << arg.sort() << endl;

    // Check that we can act as a std::map key (just needs operator<)
    map<var, int> map;
    map["One"] = 1;
    map["Two"] = 2;
    map["Three"] = 3;
    cout << map.count("Zero") << endl;
    cout << map.count("One") << endl;

    // Read a small file
    var f;
    var t;
    ifstream is("tests.txt", ifstream::in);
    if (is.fail())
        cout << "Open failed" << endl;
    while (f.getline(is))
    {
        cout << f << endl;
        t.push(f.copy());
    }
    cout << t << endl;

    // Shallow copy
    var c1 = arg;
    c1.push("extra");
    cout << c1 << endl;
    cout << arg << endl;
    var c2 = arg.copy();
    c2.pop();
    cout << c2 << endl;
    cout << arg << endl;

    // Modify an array using operator[]
    int xxa[] = {1,2,3,4,5};
    var xa(5, xxa);
    cout << "xa is " << xa << endl;
    xa[1] = xa[0];
    xa[2] += xa[1];
    xa[3] = 7;
    xa[4] += 7;
    cout << "xa is " << xa << endl;

    // Array of vars by pushing
    var arr1;
    arr1.push("Hi!");
    cout << arr1 << endl;

    // Array of vars just by assigning indeces
    var arr2;
    arr2[1] = "Hi!";
    cout << arr2 << endl;

    // Array indexed by var
    var vmap;
    vmap["one"] = 1;
    vmap["two"] = 2;
    vmap["three"] = 3;
    cout << "vmap[0] is " << vmap[0] << endl;
    cout << "vmap[1] is " << vmap[1] << endl;
    cout << "vmap[\"three\"] is " << vmap["three"] << endl;
    cout << "vmap is " << vmap << endl;
    cout << "vmap.copy() is " << vmap.copy() << endl;

    // Multi-dimensional array
    var iarr;
    iarr[1][2] = 3;
    iarr[1][4] = 5;
    cout << "iarr is " << iarr << endl;

    // Multi-dimensional map
    var wmap;
    wmap["one"]["two"] = "three";
    wmap["one"]["four"] = "five";
    cout << "wmap is " << wmap << endl;

    // Init from variable argument list
    var ai(5, 5,4,3,2,1);
    cout << "ai is: " << ai << endl;

    // Read a text file via a dynamic library
    if (vg)
    {
        vfile txtf("txt");
        var txt = txtf.read("tests.txt");
        cout << "Loaded: " << txt << endl;
    }

    // Read a .ini file via a dynamic library
    if (vg)
    {
        vfile inif("ini");
        var ini = inif.read("tests.ini");
        cout << "Loaded: " << ini << endl;
    }

    // Plot something with gnuplot
    if (vg)
    {
        var gnu;
        gnu.push("plot sin(x), \"-\"");
        gnu.push(ai);
        vfile gnuf("gnuplot");
        gnuf.write("tests.eps", gnu);
    }

    // Tensors
    var ts = 0.0f;
    ts.resize(16);
    for (int i=0; i<16; i++)
        ts[i] = (float)i;
    var t1 = ts.view(2, 4, 4);
    var t2 = ts.view(3, 2, 2, 4);
    cout << ts << endl;
    cout << t1 << endl;
    cout << t2 << endl;
    cout << "t1(1,2): " << t1(1,2) << endl;
    cout << "t2(1,1,2): " << t2(1,1,2) << endl;
    t1(1,2) = 2.3f;
    cout << t1 << endl;

    // BLAS
    var bt(4, 1.0f, 1.2, 0.8, -2.0);
    if (vg)
    {
        cout << bt << "  sums to " << bt.sum()  << endl;
        cout << bt << " asums to " << bt.asum() << endl;
    }

    // Stream
    vstream vstr;
    float fl = 23e-1f;
    vstr << "H";
    cout << vstr.var() << endl;
    vstr << "ello: " << fl;
    cout << vstr.var() << endl;

    // gedcom
    if (vg)
    {
        vfile gedf("ged");
        var ged = gedf.read("tests.ged");
        cout << "Loaded: " << ged << endl;
    }

    // XML
    if (vg)
    {
        vfile xmlf("xml");
        var xml = xmlf.read("tests.xml");
        cout << "Loaded: " << xml << endl;
    }

    // wav
    if (vg)
    {
        vfile wavf("snd");
        var wav = wavf.read("tests.wav");
        cout << "Loaded wav file:" << endl;
        cout << " rate:     " << wav["rate"] << endl;
        cout << " channels: " << wav["channels"] << endl;
        cout << " size:     " << wav["data"].size() << endl;
    }

    // Exception
    try {
        throw vruntime_error(bt);
    }
    catch (vruntime_error e) {
        cout << "Caught: " << e.what() << endl;
    };

    // Init by overloading operator,()
    var comma;
    comma = 1.2, 2.0, 4, 5;
    cout << "Comma is: " << comma << endl;

    // Regular expressions
    cout << "Searching ello: " << ss << endl;
    if (ss.search("ello"))
        cout << "Matches" << endl;
    else
        cout << "Matches not" << endl;
    cout << "Matching \\S+ell\\S: " << ss << endl;
    if (ss.search("\\S+ell\\S"))
        cout << "Matches" << endl;
    else
        cout << "Matches not" << endl;
    var rep = ss.replace("lo", "ls bells");
    cout << "Replaced to: " << rep << endl;
    return 0;
}
<|endoftext|>


<commit_before>//g++ -std=c++14 tests.cpp -o tests && ./tests

#include "LinkedList.h"
#include <assert.h>
#include <iostream>

void GivenNothingInList_WhenSizeCalled_Returns0()
{
    LinkedList<int> list = LinkedList<int>();
    assert(list.size() == 0);
}

void GivenNothingInList_WhenAddNodeAtPlace0_ReturnsTrue()
{
    LinkedList<int> list = LinkedList<int>();
    assert(list.add(0, 1) == true);
}

void GivenOneNodeInList_WhenAddNodeIndexLargerThanListSize_ThenNodeAddedToEndOfList()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();
    list.add(1);

    //Act
    list.add(5, 2);
    list.add(4, 3);

    //Assert
    assert(list.get(0) == 1);
    assert(list.get(1) == 2);
    assert(list.get(2) == 3);
}

void GivenThreeNodesInList_WhenSizeCalled_Returns3()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();
    list.add(1);
    list.add(2);
    list.add(3);

    //Act Assert
    assert(list.size() == 3);
}

void GivenNothingInList_WhenUnshiftCalled_ThenListSize1()
{
    LinkedList<int> list = LinkedList<int>();
    list.unshift(1);

    assert(list.size() == 1);
}

void GivenOneNodeInList_WhenUnshiftCalled_ThenListSize2()
{
    LinkedList<int> list = LinkedList<int>();
    list.add(1);
    list.unshift(2);

    assert(list.size() == 2);
}

void GivenOneNodeInList_WhenUnshiftCalled_ThenExistingNodeLast()
{
    LinkedList<int> list = LinkedList<int>();
    list.add(1);
    list.unshift(2);

    assert(list.get(0) == 2);
    assert(list.get(1) == 1);
}

void GivenNothingInList_WhenSetIsCalled_ThenReturnsFalse()
{
    LinkedList<int> list = LinkedList<int>();

    assert(list.set(-1, 1) == false);
    assert(list.set(0, 1) == false);
    assert(list.set(1, 1) == false);
}

void GivenThreeNodesInList_WhenSetIsCalledAtPlace1_ThenSecondNodeIsSet()
{
    LinkedList<int> list = LinkedList<int>();
    list.add(0);
    list.add(1);
    list.add(2);

    list.set(1, 10);

    assert(list.get(0) == 0);
    assert(list.get(1) == 10);
    assert(list.get(2) == 2);
}

void GivenNothingInList_WhenPopIsCalled_ThenReturnsFalse()
{
    LinkedList<int> list = LinkedList<int>();
    assert(list.pop() == false);
}

void GivenTwoNodesInList_WhenPopIsCalled_ThenReturnsLastNode()
{
    LinkedList<int> list = LinkedList<int>();
    list.add(0);
    list.add(1);

    assert(list.pop() == 1);
}

void GivenTwoNodesInList_WhenPopIsCalled_ThenListIsShorter()
{
    LinkedList<int> list = LinkedList<int>();
    list.add(0);
    list.add(1);

    assert(list.size() == 2);
    list.pop();
    assert(list.size() == 1);
}

void GivenNothingInList_WhenShiftIsCalled_ThenReturnsFalse()
{
    LinkedList<int> list = LinkedList<int>();
    assert(list.shift() == false);
}

void GivenOneNodeInList_WhenShiftIsCalled_ThenReturnsData()
{
    LinkedList<int> list = LinkedList<int>();
    list.add(5);

    assert(list.shift() == 5);
}

void GivenOneNodeInList_WhenShiftIsCalled_ThenListEmpty()
{
    LinkedList<int> list = LinkedList<int>();
    list.add(5);

    assert(list.size() == 1);
    list.shift();
    assert(list.size() == 0);
}

void GivenThreeNodesInList_WhenShiftIsCalled_ThenReturnsFirstData()
{
    LinkedList<int> list = LinkedList<int>();
    list.add(0);
    list.add(1);
    list.add(2);

    assert(list.shift() == 0);
}

void GivenThreeNodesInList_WhenShiftIsCalled_ThenListIsShorter()
{
    LinkedList<int> list = LinkedList<int>();
    list.add(0);
    list.add(1);
    list.add(2);

    assert(list.size() == 3);
    list.shift();
    assert(list.size() == 2);
}

void GivenNothingInList_WhenRemoveIsCalled_ThenFalseIsReturned()
{
    LinkedList<int> list = LinkedList<int>();

    assert(list.remove(0) == false);
}

void GivenThreeNodesInList_WhenRemoveIsCalledAtPlace0_ThenFirstNodeDataIsReturned()
{
    LinkedList<int> list = LinkedList<int>();
    list.add(0);
    list.add(1);
    list.add(2);

    assert(list.remove(0) == 0);
}

void GivenThreeNodesInList_WhenRemoveIsCalledAtPlace2_ThenLastNodeDataIsReturned()
{
    LinkedList<int> list = LinkedList<int>();
    list.add(0);
    list.add(1);
    list.add(2);

    assert(list.remove(2) == 2);
}

void GivenThreeNodesInList_WhenRemoveIsCalled_ThenListIsShorter()
{
    LinkedList<int> list = LinkedList<int>();
    list.add(0);
    list.add(1);
    list.add(2);

    assert(list.size() == 3);
    list.remove(1);
    assert(list.size() == 2);
}

void GivenNothingInList_WhenGetIsCalled_ThenReturnsFalse()
{
    LinkedList<int> list = LinkedList<int>();

    assert(list.get(0) == false);
}

void GivenThreeNodesInList_WhenGetIsCalled_ThenReturnsData()
{
    LinkedList<int> list = LinkedList<int>();
    list.add(0);
    list.add(1);
    list.add(2);

    assert(list.get(1) == 1);
}

void GivenNothingInList_WhenClearIsCalled_ThenSizeUnchanged()
{
    LinkedList<int> list = LinkedList<int>();

    assert(list.size() == 0);
    list.clear();
    assert(list.size() == 0);
}

void GivenThreeInList_WhenClearIsCalled_ThenListEmpty()
{
    LinkedList<int> list = LinkedList<int>();
    list.add(0);
    list.add(1);
    list.add(2);

    list.clear();

    assert(list.size() == 0);
}

int main()
{
    GivenNothingInList_WhenSizeCalled_Returns0();
    GivenNothingInList_WhenAddNodeAtPlace0_ReturnsTrue();
    GivenOneNodeInList_WhenAddNodeIndexLargerThanListSize_ThenNodeAddedToEndOfList();
    GivenThreeNodesInList_WhenSizeCalled_Returns3();
    GivenNothingInList_WhenUnshiftCalled_ThenListSize1();
    GivenOneNodeInList_WhenUnshiftCalled_ThenListSize2();
    GivenOneNodeInList_WhenUnshiftCalled_ThenExistingNodeLast();
    GivenNothingInList_WhenSetIsCalled_ThenReturnsFalse();
    GivenThreeNodesInList_WhenSetIsCalledAtPlace1_ThenSecondNodeIsSet();
    GivenNothingInList_WhenPopIsCalled_ThenReturnsFalse();
    GivenTwoNodesInList_WhenPopIsCalled_ThenReturnsLastNode();
    GivenTwoNodesInList_WhenPopIsCalled_ThenListIsShorter();
    GivenNothingInList_WhenShiftIsCalled_ThenReturnsFalse();
    GivenOneNodeInList_WhenShiftIsCalled_ThenReturnsData();
    GivenOneNodeInList_WhenShiftIsCalled_ThenListEmpty();
    GivenThreeNodesInList_WhenShiftIsCalled_ThenReturnsFirstData();
    GivenThreeNodesInList_WhenShiftIsCalled_ThenListIsShorter();
    GivenNothingInList_WhenRemoveIsCalled_ThenFalseIsReturned();
    GivenThreeNodesInList_WhenRemoveIsCalledAtPlace0_ThenFirstNodeDataIsReturned();
    GivenThreeNodesInList_WhenRemoveIsCalledAtPlace2_ThenLastNodeDataIsReturned();
    GivenThreeNodesInList_WhenRemoveIsCalled_ThenListIsShorter();
    GivenNothingInList_WhenGetIsCalled_ThenReturnsFalse();
    GivenThreeNodesInList_WhenGetIsCalled_ThenReturnsData();
    GivenNothingInList_WhenClearIsCalled_ThenSizeUnchanged();
    GivenThreeInList_WhenClearIsCalled_ThenListEmpty();

    std::cout<< "Tests pass"<< std::endl;
}
<commit_msg>More consistent test structure<commit_after>//g++ -std=c++14 tests.cpp -o tests && ./tests

#include "LinkedList.h"
#include <assert.h>
#include <iostream>

void GivenNothingInList_WhenSizeCalled_Returns0()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();

    //Act Assert
    assert(list.size() == 0);
}

void GivenNothingInList_WhenAddNodeAtPlace0_ReturnsTrue()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();

    //Act Assert
    assert(list.add(0, 1) == true);
}

void GivenOneNodeInList_WhenAddNodeIndexLargerThanListSize_ThenNodeAddedToEndOfList()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();
    list.add(1);

    //Act
    list.add(5, 2);
    list.add(4, 3);

    //Assert
    assert(list.get(0) == 1);
    assert(list.get(1) == 2);
    assert(list.get(2) == 3);
}

void GivenThreeNodesInList_WhenSizeCalled_Returns3()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();
    list.add(1);
    list.add(2);
    list.add(3);

    //Act Assert
    assert(list.size() == 3);
}

void GivenNothingInList_WhenUnshiftCalled_ThenListSize1()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();

    //Act
    list.unshift(1);

    //Assert
    assert(list.size() == 1);
}

void GivenOneNodeInList_WhenUnshiftCalled_ThenListSize2()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();
    list.add(1);

    //Act
    list.unshift(2);

    //Assert
    assert(list.size() == 2);
}

void GivenOneNodeInList_WhenUnshiftCalled_ThenExistingNodeLast()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();
    list.add(1);

    //Act
    list.unshift(2);

    //Assert
    assert(list.get(0) == 2);
    assert(list.get(1) == 1);
}

void GivenNothingInList_WhenSetIsCalled_ThenReturnsFalse()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();

    //Act Assert
    assert(list.set(-1, 1) == false);
    assert(list.set(0, 1) == false);
    assert(list.set(1, 1) == false);
}

void GivenThreeNodesInList_WhenSetIsCalledAtPlace1_ThenSecondNodeIsSet()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();
    list.add(0);
    list.add(1);
    list.add(2);

    //Act
    list.set(1, 10);

    //Assert
    assert(list.get(0) == 0);
    assert(list.get(1) == 10);
    assert(list.get(2) == 2);
}

void GivenNothingInList_WhenPopIsCalled_ThenReturnsFalse()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();

    //Act Assert
    assert(list.pop() == false);
}

void GivenTwoNodesInList_WhenPopIsCalled_ThenReturnsLastNode()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();
    list.add(0);
    list.add(1);

    //Act Assert
    assert(list.pop() == 1);
}

void GivenTwoNodesInList_WhenPopIsCalled_ThenListIsShorter()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();
    list.add(0);
    list.add(1);

    //Act Assert
    assert(list.size() == 2);
    list.pop();
    assert(list.size() == 1);
}

void GivenNothingInList_WhenShiftIsCalled_ThenReturnsFalse()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();

    //Act Assert
    assert(list.shift() == false);
}

void GivenOneNodeInList_WhenShiftIsCalled_ThenReturnsData()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();
    list.add(5);

    //Act Assert
    assert(list.shift() == 5);
}

void GivenOneNodeInList_WhenShiftIsCalled_ThenListEmpty()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();
    list.add(5);

    //Act Assert
    assert(list.size() == 1);
    list.shift();
    assert(list.size() == 0);
}

void GivenThreeNodesInList_WhenShiftIsCalled_ThenReturnsFirstData()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();
    list.add(0);
    list.add(1);
    list.add(2);

    //Act Assert
    assert(list.shift() == 0);
}

void GivenThreeNodesInList_WhenShiftIsCalled_ThenListIsShorter()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();
    list.add(0);
    list.add(1);
    list.add(2);

    //Act Assert
    assert(list.size() == 3);
    list.shift();
    assert(list.size() == 2);
}

void GivenNothingInList_WhenRemoveIsCalled_ThenFalseIsReturned()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();

    //Act Assert
    assert(list.remove(0) == false);
}

void GivenThreeNodesInList_WhenRemoveIsCalledAtPlace0_ThenFirstNodeDataIsReturned()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();
    list.add(0);
    list.add(1);
    list.add(2);

    //Act Assert
    assert(list.remove(0) == 0);
}

void GivenThreeNodesInList_WhenRemoveIsCalledAtPlace2_ThenLastNodeDataIsReturned()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();
    list.add(0);
    list.add(1);
    list.add(2);

    //Act Assert
    assert(list.remove(2) == 2);
}

void GivenThreeNodesInList_WhenRemoveIsCalled_ThenListIsShorter()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();
    list.add(0);
    list.add(1);
    list.add(2);

    //Act Assert
    assert(list.size() == 3);
    list.remove(1);
    assert(list.size() == 2);
}

void GivenNothingInList_WhenGetIsCalled_ThenReturnsFalse()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();

    //Act Assert
    assert(list.get(0) == false);
}

void GivenThreeNodesInList_WhenGetIsCalled_ThenReturnsData()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();
    list.add(0);
    list.add(1);
    list.add(2);

    //Act Assert
    assert(list.get(1) == 1);
}

void GivenNothingInList_WhenClearIsCalled_ThenSizeUnchanged()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();

    //Act Assert
    assert(list.size() == 0);
    list.clear();
    assert(list.size() == 0);
}

void GivenThreeInList_WhenClearIsCalled_ThenListEmpty()
{
    //Arrange
    LinkedList<int> list = LinkedList<int>();
    list.add(0);
    list.add(1);
    list.add(2);

    //Act
    list.clear();

    //Assert
    assert(list.size() == 0);
}

int main()
{
    GivenNothingInList_WhenSizeCalled_Returns0();
    GivenNothingInList_WhenAddNodeAtPlace0_ReturnsTrue();
    GivenOneNodeInList_WhenAddNodeIndexLargerThanListSize_ThenNodeAddedToEndOfList();
    GivenThreeNodesInList_WhenSizeCalled_Returns3();
    GivenNothingInList_WhenUnshiftCalled_ThenListSize1();
    GivenOneNodeInList_WhenUnshiftCalled_ThenListSize2();
    GivenOneNodeInList_WhenUnshiftCalled_ThenExistingNodeLast();
    GivenNothingInList_WhenSetIsCalled_ThenReturnsFalse();
    GivenThreeNodesInList_WhenSetIsCalledAtPlace1_ThenSecondNodeIsSet();
    GivenNothingInList_WhenPopIsCalled_ThenReturnsFalse();
    GivenTwoNodesInList_WhenPopIsCalled_ThenReturnsLastNode();
    GivenTwoNodesInList_WhenPopIsCalled_ThenListIsShorter();
    GivenNothingInList_WhenShiftIsCalled_ThenReturnsFalse();
    GivenOneNodeInList_WhenShiftIsCalled_ThenReturnsData();
    GivenOneNodeInList_WhenShiftIsCalled_ThenListEmpty();
    GivenThreeNodesInList_WhenShiftIsCalled_ThenReturnsFirstData();
    GivenThreeNodesInList_WhenShiftIsCalled_ThenListIsShorter();
    GivenNothingInList_WhenRemoveIsCalled_ThenFalseIsReturned();
    GivenThreeNodesInList_WhenRemoveIsCalledAtPlace0_ThenFirstNodeDataIsReturned();
    GivenThreeNodesInList_WhenRemoveIsCalledAtPlace2_ThenLastNodeDataIsReturned();
    GivenThreeNodesInList_WhenRemoveIsCalled_ThenListIsShorter();
    GivenNothingInList_WhenGetIsCalled_ThenReturnsFalse();
    GivenThreeNodesInList_WhenGetIsCalled_ThenReturnsData();
    GivenNothingInList_WhenClearIsCalled_ThenSizeUnchanged();
    GivenThreeInList_WhenClearIsCalled_ThenListEmpty();

    std::cout<< "Tests pass"<< std::endl;
}
<|endoftext|>