< Summary

Class:NanoCLang.Entities.IntegerType
Assembly:NanoCLang
File(s):C:\GitLab-Runner\builds\JxAESPd8\0\chenmichael\nanoc\src\NanoCLang\Entities\BasicType\IntegerType.cs
Covered lines:35
Uncovered lines:6
Coverable lines:41
Total lines:89
Line coverage:85.3% (35 of 41)
Covered branches:24
Total branches:32
Branch coverage:75% (24 of 32)

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%1100%
.ctor(...)100%1100%
get_Size()100%1100%
get_Values()100%1100%
get_IsVoid()50%2100%
get_Void()100%1100%
get_Boolean()100%1100%
WellFormed(...)100%2100%
Tokens()100%14100%
Clone()100%1100%
GetCType(...)30%1062.5%
Tokens()100%1100%
Equals(...)100%1100%
Equals(...)100%4100%
GetHashCode()100%10%
op_Equality(...)100%10%
op_Inequality(...)100%10%

File(s)

C:\GitLab-Runner\builds\JxAESPd8\0\chenmichael\nanoc\src\NanoCLang\Entities\BasicType\IntegerType.cs

#LineLine coverage
 1using NanoCLang.Environemnts;
 2using System;
 3using System.Collections.Generic;
 4
 5namespace NanoCLang.Entities {
 6    /// <summary>
 7    /// Provides a class for NanoC integer types.
 8    /// </summary>
 9    public class IntegerType : ArbitraryIntegerType, IEquatable<IntegerType?> {
 10        /// <summary>
 11        /// Creates a new instance of a NanoC integer type.
 12        /// </summary>
 13        /// <param name="size">The size in bytes of the type.</param>
 14        /// <param name="values">The index of valid values.</param>
 184215        public IntegerType(int size, Index values) {
 92116            Size = size;
 92117            Values = values;
 92118        }
 19        /// <summary>
 20        /// Creates a new instance of a NanoC integer type with arbitrary index.
 21        /// </summary>
 22        /// <param name="size">The size in bytes of the type.</param>
 50723        public IntegerType(int size) : this(size, new ArbitraryIndex()) { }
 24        /// <summary>
 25        /// Size of the type in bytes.
 26        /// </summary>
 284527        public override int Size { get; }
 28        /// <summary>
 29        /// Index of allowed values for the integer.
 30        /// </summary>
 176731        public Index Values { get; }
 32        /// <summary>
 33        /// Checks if the type is a void type.
 34        /// </summary>
 435        public bool IsVoid => Size == 0 && Values == Index.Zero;
 36        /// <summary>
 37        /// Returns a new void type.
 38        /// </summary>
 6439        public static IntegerType Void => new IntegerType(0, new SingletonIndex(0));
 40        /// <summary>
 41        /// Returns a new boolean type.
 42        /// </summary>
 15543        public static IntegerType Boolean => new IntegerType(1, new SequenceIndex(0, 1));
 44        /// <inheritdoc/>
 19845        public override void WellFormed(LocalEnvironment gamma, Heap heap) {
 19846            VerbConsole.WriteLine(VerbosityLevel.Default, $"WF-Int: {this}");
 19847            if (Size < 0)
 148                throw new IllFormedException(this, $"Integer cannot have negative size {Size}!");
 19749        }
 50        /// <inheritdoc/>
 59151        public override IEnumerable<StringFormatterToken> Tokens(NanoCSourceFormat args) {
 59152            switch (Size) {
 14753            case 0 when Values == Index.Zero: yield return "void"; break;
 82854            case 1 when Values is ArbitraryIndex: yield return "char"; break;
 66955            case 4 when Values is ArbitraryIndex: yield return "int"; break;
 56            default:
 6457                yield return Size.ToString();
 6458                yield return "[";
 60659                foreach (var tk in Values.Tokens(args)) yield return tk;
 6460                yield return "]";
 6461                break;
 62            }
 59163        }
 64        /// <inheritdoc/>
 26865        public override object Clone() => new IntegerType(Size, (Index)Values.Clone());
 11566        internal static StringFormatterToken GetCType(int size) => size switch {
 1967            0 => "void",
 1868            1 => "int8_t",
 069            2 => "int16_t",
 7870            4 => "int32_t",
 071            8 => "int64_t",
 072            _ => throw new InvalidProgramException($"Cannot declare a {size}-Byte integer in C!"),
 11573        };
 74        /// <inheritdoc/>
 18675        public override IEnumerable<StringFormatterToken> Tokens(CSourceFormat args) { yield return GetCType(Size); }
 76        #region Equality checks
 77        /// <inheritdoc/>
 34778        public override bool Equals(object? obj) => Equals(obj as IntegerType);
 79        /// <inheritdoc/>
 34780        public bool Equals(IntegerType? other) => !(other is null) && Size == other.Size && EqualityComparer<Index>.Defa
 81        /// <inheritdoc/>
 082        public override int GetHashCode() => HashCode.Combine(Size, Values);
 83        /// <inheritdoc/>
 084        public static bool operator ==(IntegerType? left, IntegerType? right) => EqualityComparer<IntegerType?>.Default.
 85        /// <inheritdoc/>
 086        public static bool operator !=(IntegerType? left, IntegerType? right) => !(left == right);
 87        #endregion
 88    }
 89}