-- GHDL Run Time (GRT) - PSL report. -- Copyright (C) 2016 Tristan Gingold -- -- GHDL is free software; you can redistribute it and/or modify it under -- the terms of the GNU General Public License as published by the Free -- Software Foundation; either version 2, or (at your option) any later -- version. -- -- GHDL 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 General Public License -- for more details. -- -- You should have received a copy of the GNU General Public License -- along with GCC; see the file COPYING. If not, write to the Free -- Software Foundation, 59 Temple Place - Suite 330, Boston, MA -- 02111-1307, USA. -- -- As a special exception, if other files instantiate generics from this -- unit, or you link this unit with other files to produce an executable, -- this unit does not by itself cause the resulting executable to be -- covered by the GNU General Public License. This exception does not -- however invalidate any other reasons why the executable file might be -- covered by the GNU Public License. with System; with Grt.Types; use Grt.Types; with Grt.Stdio; use Grt.Stdio; with Grt.Errors; use Grt.Errors; with Grt.Astdio; use Grt.Astdio; with Grt.Hooks; use Grt.Hooks; with Grt.Rtis; use Grt.Rtis; with Grt.Rtis_Addr; use Grt.Rtis_Addr; with Grt.Rtis_Utils; use Grt.Rtis_Utils; package body Grt.Psl is -- Filename of the report. Last character is NUL. Report_Filename : String_Access; Report_Stream : FILEs; Is_First : Boolean := True; Nbr_Assert_Failed : Ghdl_U32 := 0; Nbr_Assert_Passed : Ghdl_U32 := 0; Nbr_Assume_Failed : Ghdl_U32 := 0; Nbr_Assume_Passed : Ghdl_U32 := 0; Nbr_Cover_Failed : Ghdl_U32 := 0; Nbr_Cover_Passed : Ghdl_U32 := 0; -- Return TRUE if OPT is an option for PSL. function Psl_Option (Opt : String) return Boolean is F : constant Natural := Opt'First; begin if Opt'Length > 13 and then Opt (F .. F + 12) = "--psl-report=" then Report_Filename := new String (1 .. Opt'Last - F - 13 + 1 + 1); Report_Filename (1 .. Opt'Last - F - 13 + 1) := Opt (F + 13 .. Opt'Last); Report_Filename (Report_Filename'Last) := NUL; return True; else return False; end if; end Psl_Option; procedure Psl_Help is begin Put_Line (" --psl-report=FILE report psl result at end of simulation"); end Psl_Help; procedure Inc (V : in out Ghdl_U32) is begin V := V + 1; end Inc; function Process (Ctxt : Rti_Context; Rti : Ghdl_Rti_Access) return Traverse_Result is F : constant FILEs := Report_Stream; Obj : Ghdl_Rtin_Object_Acc; Addr : System.Address; Val : Ghdl_Index_Type; begin case Rti.Kind is when Ghdl_Rtiks_Psl => null; when Ghdl_Rtik_Process => return Traverse_Skip; when others => return Traverse_Ok; end case; if Is_First then Is_First := False; else Put_Line (F, ","); end if; Put (F, " { ""directive"": "); case Ghdl_Rtiks_Psl (Rti.Kind) is when Ghdl_Rtik_Psl_Assert => Put (F, """assertion"""); when Ghdl_Rtik_Psl_Assume => Put (F, """assumption"""); when Ghdl_Rtik_Psl_Cover => Put (F, """cover"""); end case; Put_Line (F, ","); Put (F, " ""name"": """); Obj := To_Ghdl_Rtin_Object_Acc (Rti); Put (F, Ctxt); Put (F, '.'); Put (F, Obj.Name); Put_Line (F, ""","); Put (F, " ""file"": """); Put (F, Get_Filename (Ctxt)); Put_Line (F, ""","); Put (F, " ""line"": "); Put_U32 (F, Get_Linecol_Line (Obj.Linecol)); Put_Line (F, ","); Put (F, " ""count"": "); Addr := Loc_To_Addr (Obj.Common.Depth, Obj.Loc, Ctxt); Val := To_Ghdl_Index_Ptr (Addr).all; Put_U32 (F, Ghdl_U32 (Val)); Put_Line (F, ","); Put (F, " ""status"": """); case Rti.Kind is when Ghdl_Rtik_Psl_Assert => if Val = 0 then Put (F, "passed"); Inc (Nbr_Assert_Passed); else Put (F, "failed"); Inc (Nbr_Assert_Failed); end if; when Ghdl_Rtik_Psl_Assume => if Val = 0 then Put (F, "passed"); Inc (Nbr_Assume_Passed); else Put (F, "failed"); Inc (Nbr_Assume_Failed); end if; when Ghdl_Rtik_Psl_Cover => if Val = 0 then Put (F, "not covered"); Inc (Nbr_Cover_Failed); else Put (F, "covered"); Inc (Nbr_Cover_Passed); end if; when others => raise Program_Error; end case; Put (F, """}"); return Traverse_Ok; end Process; function Psl_Traverse_Blocks is new Traverse_Blocks (Process); -- Called at the end of the simulation. procedure Psl_End is Mode : constant String := "wt" & NUL; Status : Traverse_Result; F : FILEs; begin if Report_Filename = null then return; end if; F := fopen (Report_Filename.all'Address, Mode'Address); if F = NULL_Stream then Error_S ("cannot open "); Error_E (Report_Filename (Report_Filename'First .. Report_Filename'Last - 1)); return; end if; Put_Line (F, "{ ""details"" : ["); Report_Stream := F; Status := Psl_Traverse_Blocks (Get_Top_Context); pragma Assert (Status = Traverse_Ok or Status = Traverse_Skip); Put_Line (F, "],"); Put_Line (F, " ""summary"" : {"); Put (F, " ""assert"": "); Put_U32 (F, Nbr_Assert_Failed + Nbr_Assert_Passed); Put_Line (F, ","); Put (F, " ""assert-failure"": "); Put_U32 (F, Nbr_Assert_Failed); Put_Line (F, ","); Put (F, " ""assert-pass"": "); Put_U32 (F, Nbr_Assert_Passed); Put_Line (F, ","); Put (F, " ""assume"": "); Put_U32 (F, Nbr_Assume_Failed + Nbr_Assume_Passed); Put_Line (F, ","); Put (F, " ""assume-failure"": "); Put_U32 (F, Nbr_Assume_Failed); Put_Line (F, ","); Put (F, " ""assume-pass"": "); Put_U32 (F, Nbr_Assume_Passed); Put_Line (F, ","); Put (F, " ""cover"": "); Put_U32 (F, Nbr_Cover_Failed + Nbr_Cover_Passed); Put_Line (F, ","); Put (F, " ""cover-failure"": "); Put_U32 (F, Nbr_Cover_Failed); Put_Line (F, ","); Put (F, " ""cover-pass"": "); Put_U32 (F, Nbr_Cover_Passed); Put_Line (F, "}"); Put_Line (F, "}"); fclose (F); end Psl_End; Psl_Hooks : aliased constant Hooks_Type := (Desc => new String'("psl: display status of psl assertion and cover"), Option => Psl_Option'Access, Help => Psl_Help'Access, Init => null, Start => null, Finish => Psl_End'Access); procedure Register is begin Register_Hooks (Psl_Hooks'Access); end Register; end Grt.Psl; 126' href='#n126'>126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
import pytest

from pybind11_tests import enums as m


def test_unscoped_enum():
    assert str(m.UnscopedEnum.EOne) == "UnscopedEnum.EOne"
    assert str(m.UnscopedEnum.ETwo) == "UnscopedEnum.ETwo"
    assert str(m.EOne) == "UnscopedEnum.EOne"
    assert repr(m.UnscopedEnum.EOne) == "<UnscopedEnum.EOne: 1>"
    assert repr(m.UnscopedEnum.ETwo) == "<UnscopedEnum.ETwo: 2>"
    assert repr(m.EOne) == "<UnscopedEnum.EOne: 1>"

    # name property
    assert m.UnscopedEnum.EOne.name == "EOne"
    assert m.UnscopedEnum.EOne.value == 1
    assert m.UnscopedEnum.ETwo.name == "ETwo"
    assert m.UnscopedEnum.ETwo.value == 2
    assert m.EOne is m.UnscopedEnum.EOne
    # name, value readonly
    with pytest.raises(AttributeError):
        m.UnscopedEnum.EOne.name = ""
    with pytest.raises(AttributeError):
        m.UnscopedEnum.EOne.value = 10
    # name, value returns a copy
    # TODO: Neither the name nor value tests actually check against aliasing.
    # Use a mutable type that has reference semantics.
    nonaliased_name = m.UnscopedEnum.EOne.name
    nonaliased_name = "bar"  # noqa: F841
    assert m.UnscopedEnum.EOne.name == "EOne"
    nonaliased_value = m.UnscopedEnum.EOne.value
    nonaliased_value = 10  # noqa: F841
    assert m.UnscopedEnum.EOne.value == 1

    # __members__ property
    assert m.UnscopedEnum.__members__ == {
        "EOne": m.UnscopedEnum.EOne,
        "ETwo": m.UnscopedEnum.ETwo,
        "EThree": m.UnscopedEnum.EThree,
    }
    # __members__ readonly
    with pytest.raises(AttributeError):
        m.UnscopedEnum.__members__ = {}
    # __members__ returns a copy
    nonaliased_members = m.UnscopedEnum.__members__
    nonaliased_members["bar"] = "baz"
    assert m.UnscopedEnum.__members__ == {
        "EOne": m.UnscopedEnum.EOne,
        "ETwo": m.UnscopedEnum.ETwo,
        "EThree": m.UnscopedEnum.EThree,
    }

    for docstring_line in """An unscoped enumeration

Members:

  EOne : Docstring for EOne

  ETwo : Docstring for ETwo

  EThree : Docstring for EThree""".split(
        "\n"
    ):
        assert docstring_line in m.UnscopedEnum.__doc__

    # Unscoped enums will accept ==/!= int comparisons
    y = m.UnscopedEnum.ETwo
    assert y == 2
    assert 2 == y
    assert y != 3
    assert 3 != y
    # Compare with None
    assert y != None  # noqa: E711
    assert not (y == None)  # noqa: E711
    # Compare with an object
    assert y != object()
    assert not (y == object())
    # Compare with string
    assert y != "2"
    assert "2" != y
    assert not ("2" == y)
    assert not (y == "2")

    with pytest.raises(TypeError):
        y < object()  # noqa: B015

    with pytest.raises(TypeError):
        y <= object()  # noqa: B015

    with pytest.raises(TypeError):
        y > object()  # noqa: B015

    with pytest.raises(TypeError):
        y >= object()  # noqa: B015

    with pytest.raises(TypeError):
        y | object()

    with pytest.raises(TypeError):
        y & object()

    with pytest.raises(TypeError):
        y ^ object()

    assert int(m.UnscopedEnum.ETwo) == 2
    assert str(m.UnscopedEnum(2)) == "UnscopedEnum.ETwo"

    # order
    assert m.UnscopedEnum.EOne < m.UnscopedEnum.ETwo
    assert m.UnscopedEnum.EOne < 2
    assert m.UnscopedEnum.ETwo > m.UnscopedEnum.EOne
    assert m.UnscopedEnum.ETwo > 1
    assert m.UnscopedEnum.ETwo <= 2
    assert m.UnscopedEnum.ETwo >= 2
    assert m.UnscopedEnum.EOne <= m.UnscopedEnum.ETwo
    assert m.UnscopedEnum.EOne <= 2
    assert m.UnscopedEnum.ETwo >= m.UnscopedEnum.EOne
    assert m.UnscopedEnum.ETwo >= 1
    assert not (m.UnscopedEnum.ETwo < m.UnscopedEnum.EOne)
    assert not (2 < m.UnscopedEnum.EOne)

    # arithmetic
    assert m.UnscopedEnum.EOne & m.UnscopedEnum.EThree == m.UnscopedEnum.EOne
    assert m.UnscopedEnum.EOne | m.UnscopedEnum.ETwo == m.UnscopedEnum.EThree
    assert m.UnscopedEnum.EOne ^ m.UnscopedEnum.EThree == m.UnscopedEnum.ETwo


def test_scoped_enum():
    assert m.test_scoped_enum(m.ScopedEnum.Three) == "ScopedEnum::Three"
    z = m.ScopedEnum.Two
    assert m.test_scoped_enum(z) == "ScopedEnum::Two"

    # Scoped enums will *NOT* accept ==/!= int comparisons (Will always return False)
    assert not z == 3
    assert not 3 == z
    assert z != 3
    assert 3 != z
    # Compare with None
    assert z != None  # noqa: E711
    assert not (z == None)  # noqa: E711
    # Compare with an object
    assert z != object()
    assert not (z == object())
    # Scoped enums will *NOT* accept >, <, >= and <= int comparisons (Will throw exceptions)
    with pytest.raises(TypeError):
        z > 3  # noqa: B015
    with pytest.raises(TypeError):
        z < 3  # noqa: B015
    with pytest.raises(TypeError):
        z >= 3  # noqa: B015
    with pytest.raises(TypeError):
        z <= 3  # noqa: B015

    # order
    assert m.ScopedEnum.Two < m.ScopedEnum.Three
    assert m.ScopedEnum.Three > m.ScopedEnum.Two
    assert m.ScopedEnum.Two <= m.ScopedEnum.Three
    assert m.ScopedEnum.Two <= m.ScopedEnum.Two
    assert m.ScopedEnum.Two >= m.ScopedEnum.Two
    assert m.ScopedEnum.Three >= m.ScopedEnum.Two


def test_implicit_conversion():
    assert str(m.ClassWithUnscopedEnum.EMode.EFirstMode) == "EMode.EFirstMode"
    assert str(m.ClassWithUnscopedEnum.EFirstMode) == "EMode.EFirstMode"
    assert repr(m.ClassWithUnscopedEnum.EMode.EFirstMode) == "<EMode.EFirstMode: 1>"
    assert repr(m.ClassWithUnscopedEnum.EFirstMode) == "<EMode.EFirstMode: 1>"

    f = m.ClassWithUnscopedEnum.test_function
    first = m.ClassWithUnscopedEnum.EFirstMode
    second = m.ClassWithUnscopedEnum.ESecondMode

    assert f(first) == 1

    assert f(first) == f(first)
    assert not f(first) != f(first)

    assert f(first) != f(second)
    assert not f(first) == f(second)

    assert f(first) == int(f(first))
    assert not f(first) != int(f(first))

    assert f(first) != int(f(second))
    assert not f(first) == int(f(second))

    # noinspection PyDictCreation
    x = {f(first): 1, f(second): 2}
    x[f(first)] = 3
    x[f(second)] = 4
    # Hashing test
    assert repr(x) == "{<EMode.EFirstMode: 1>: 3, <EMode.ESecondMode: 2>: 4}"


def test_binary_operators():
    assert int(m.Flags.Read) == 4
    assert int(m.Flags.Write) == 2
    assert int(m.Flags.Execute) == 1
    assert int(m.Flags.Read | m.Flags.Write | m.Flags.Execute) == 7
    assert int(m.Flags.Read | m.Flags.Write) == 6
    assert int(m.Flags.Read | m.Flags.Execute) == 5
    assert int(m.Flags.Write | m.Flags.Execute) == 3
    assert int(m.Flags.Write | 1) == 3
    assert ~m.Flags.Write == -3

    state = m.Flags.Read | m.Flags.Write
    assert (state & m.Flags.Read) != 0
    assert (state & m.Flags.Write) != 0
    assert (state & m.Flags.Execute) == 0
    assert (state & 1) == 0

    state2 = ~state
    assert state2 == -7
    assert int(state ^ state2) == -1


def test_enum_to_int():
    m.test_enum_to_int(m.Flags.Read)
    m.test_enum_to_int(m.ClassWithUnscopedEnum.EMode.EFirstMode)
    m.test_enum_to_int(m.ScopedCharEnum.Positive)
    m.test_enum_to_int(m.ScopedBoolEnum.TRUE)
    m.test_enum_to_uint(m.Flags.Read)
    m.test_enum_to_uint(m.ClassWithUnscopedEnum.EMode.EFirstMode)
    m.test_enum_to_uint(m.ScopedCharEnum.Positive)
    m.test_enum_to_uint(m.ScopedBoolEnum.TRUE)
    m.test_enum_to_long_long(m.Flags.Read)
    m.test_enum_to_long_long(m.ClassWithUnscopedEnum.EMode.EFirstMode)
    m.test_enum_to_long_long(m.ScopedCharEnum.Positive)
    m.test_enum_to_long_long(m.ScopedBoolEnum.TRUE)


def test_duplicate_enum_name():
    with pytest.raises(ValueError) as excinfo:
        m.register_bad_enum()
    assert str(excinfo.value) == 'SimpleEnum: element "ONE" already exists!'


def test_char_underlying_enum():  # Issue #1331/PR #1334:
    assert type(m.ScopedCharEnum.Positive.__int__()) is int
    assert int(m.ScopedChar16Enum.Zero) == 0
    assert hash(m.ScopedChar32Enum.Positive) == 1
    assert type(m.ScopedCharEnum.Positive.__getstate__()) is int
    assert m.ScopedWCharEnum(1) == m.ScopedWCharEnum.Positive
    with pytest.raises(TypeError):
        # Even if the underlying type is char, only an int can be used to construct the enum:
        m.ScopedCharEnum("0")


def test_bool_underlying_enum():
    assert type(m.ScopedBoolEnum.TRUE.__int__()) is int
    assert int(m.ScopedBoolEnum.FALSE) == 0
    assert hash(m.ScopedBoolEnum.TRUE) == 1
    assert type(m.ScopedBoolEnum.TRUE.__getstate__()) is int
    assert m.ScopedBoolEnum(1) == m.ScopedBoolEnum.TRUE
    # Enum could construct with a bool
    # (bool is a strict subclass of int, and False will be converted to 0)
    assert m.ScopedBoolEnum(False) == m.ScopedBoolEnum.FALSE


def test_docstring_signatures():
    for enum_type in [m.ScopedEnum, m.UnscopedEnum]:
        for attr in enum_type.__dict__.values():
            # Issue #2623/PR #2637: Add argument names to enum_ methods
            assert "arg0" not in (attr.__doc__ or "")