Jump to content

Functional coverage tree: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 136:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 289:
fmt.Printf("%8.6f to %8.6f\n", diff, topCoverage+diff)
h2Basement[2].setCoverage(0.75) // restore to original value if required
}</langsyntaxhighlight>
 
{{out}}
Line 348:
The raw table (supplied in the task description) is read in from a text file, parsed to a tree structure, and updated by two traversals (one bottom-up and one top down) before being serialised back to a completed outline text, with an additional 'Share of Residue' column:
{{Trans|Python}}
<langsyntaxhighlight lang="haskell">{-# LANGUAGE OverloadedStrings #-}
 
import Data.Bifunctor (first)
Line 523:
 
showN :: Int -> Float -> String
showN p n = justifyRight 7 ' ' (showFFloat (Just p) n "")</langsyntaxhighlight>
{{Out}}
<pre>NAME_HIERARCHY | WEIGHT | COVERAGE | SHARE OF RESIDUE
Line 573:
Implementation part 1 of 4 (raw data):
 
<langsyntaxhighlight Jlang="j">raw=: 0 :0
NAME_HIERARCHY |WEIGHT |COVERAGE |
cleaning | | |
Line 617:
wine_cellar | |1 |
cinema | |0.75 |
)</langsyntaxhighlight>
 
Implementation part 2 of 4 (unpacking raw data):
 
<langsyntaxhighlight Jlang="j">labels=: {.<;._2;._2 raw
'hier wspec cspec'=:|:}.<;._2;._2 raw
level=: (%+./) (0 i.~' '&=)"1 hier
weight=: (+ 0=]) ,".wspec
coverage=: ,".cspec</langsyntaxhighlight>
 
To understand this implementation, it's best to run it and inspect the data.
Line 641:
Implementation part 3 of 4 (translation of leaf coverage to functional coverage):
 
<langsyntaxhighlight Jlang="j">merge=: ;@(({.@[,(+}.)~)&.> [: +/\1,_1}.#@>)
unrooted=: ([: merge <@(_1,$:@}.);.1)^:(0<#)
parent=: unrooted level
parent_cover=: (] (1}.~.parent)}~ 1}. * %&(parent +//. ]) [)^:_</langsyntaxhighlight>
 
<code>unrooted</code> translates indentation information to a [[Tree_traversal#J:_Alternate_implementation|parent tree structure]]. However, the limitations of recursion require we distinguish the parent node from its children, so we use _1 to denote the parent node of the recursive intermediate result unrooted trees. (This works well with using arithmetic to adjust sub-tree indices based on the lengths of preceding sub-trees.) <code>merge</code> combines a boxed sequence of these subtrees to form a single tree - we also rely on the first node of each tree being both _1 and the root node.
Line 652:
Task example part 4 of 4 (format and show result):
 
<langsyntaxhighlight Jlang="j"> 1 1 }._1 }.":labels,each ":each hier;(,.weight);,.weight parent_cover coverage
NAME_HIERARCHY │WEIGHT │COVERAGE │
cleaning │ 1 │0.409167 │
Line 695:
cellars │ 1 │ 1 │
wine_cellar │ 1 │ 1 │
cinema │ 1 │ 0.75 │</langsyntaxhighlight>
 
Extra credit:
 
<langsyntaxhighlight Jlang="j">trace=: (~.@,each (0 >. parent)&{)^:_ i.#parent
power=: */@:{&(parent (] % (i.~ ~.)@[ { +//.) weight)@> trace
 
power*1-weight parent_cover coverage
0.590833 0.2675 0.0375 0.025 0.00833333 0.0166667 0 0.0125 0.045 0.0375 0.0125 0.0125 0.0125 0 0.05 0.05 0.01 0.323333 0.17 0.05 0.0125 0.0125 0.0125 0.0125 0.05 0.05 0.02 0.136667 0.0333333 0.0333333 0.00833333 0.00833333 0.00833333 0.00833333 0.0333333 0.0333333 0.00333333 0 0.0166667 0 0 0.0166667</langsyntaxhighlight>
 
Explanation:
Line 720:
{{Trans|Haskell}}
{{Trans|Python}}
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 1,158:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>NAME_HIERARCHY | WEIGHT | COVERAGE | SHARE OF RESIDUE
Line 1,207:
Most implementations of functional coverage are going to store values in a database. The implementation
stores the tree in a CSV file with an index to the parent of each entry to allow reconstitution of the tree.
<langsyntaxhighlight Julialang="julia">using CSV, DataFrames, Formatting
 
function updatecoverage(dfname, outputname)
Line 1,269:
println("\nUpdated data:")
displaycoveragedb(newdbname)
</langsyntaxhighlight>{{out}}
<pre>
Input data:
Line 1,409:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.2.10
 
class FCNode(val name: String, val weight: Int = 1, coverage: Double = 0.0) {
Line 1,546:
println("${"%8.6f".format(diff)} to ${"%8.6f".format(topCoverage + diff)}")
h2Basement[2].coverage = 0.75 // restore to original value if required
}</langsyntaxhighlight>
 
{{out}}
Line 1,600:
</pre>
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">-- DATA:
local function node(name, weight, coverage, children)
return { name=name, weight=weight or 1.0, coverage=coverage or 0.0, sumofweights=0, delta=0, children=children }
Line 1,693:
end
print("NAME_HIERARCHY |WEIGHT |COVERAGE |DELTA |")
printnode(root,0)</langsyntaxhighlight>
{{out}}
<pre>NAME_HIERARCHY |WEIGHT |COVERAGE |DELTA |
Line 1,741:
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">import strformat, strutils
 
type
Line 1,871:
echo "\nIf the coverage of the Cinema node were increased from 0.75 to 1"
echo &"the top level coverage would increase by {diff:8.6f} to {topCoverage + diff:8.6f}"
h2Basement[2].setCoverage(0.75) # Restore to original value if required.</langsyntaxhighlight>
 
{{out}}
Line 1,924:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 1,997:
wine_cellar | |1 |
cinema | |0.75 |
</syntaxhighlight>
</lang>
{{out}}
<pre style="height:20ex" style="font-size:80%;">NAME_HIERARCHY |WEIGHT |COVERAGE |
Line 2,044:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Functional_coverage_tree.exw
Line 2,156:
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,208:
It's actually some of the raw code used when researching this task.
 
<langsyntaxhighlight lang="python">from itertools import zip_longest
 
 
Line 2,360:
print('\n\nTOP COVERAGE = %f\n' % covercalc(lstc))
depth_first(lstc)
pptreefields(['NAME_HIERARCHY WEIGHT COVERAGE'.split()] + lstc)</langsyntaxhighlight>
 
{{out}}
Line 2,412:
A cleaner implementation that uses the class static variable path2node as in the previous example so you don't have to traverse the tree to work out the position to add new nodes. This relies on parent nodes appearing before their children which is the case in the order of the add_node calls.
 
<langsyntaxhighlight lang="python"># -*- coding: utf-8 -*-
 
SPACES = 4
Line 2,538:
assert isclose((delta + cover), 1.0), "Top level delta + coverage should " \
"equal 1 instead of (%f + %f)" % (delta, cover)
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,605:
Mainly uses pre-existing generic functions, including '''forestFromLineIndents''', '''foldTree''' and '''fmapTree''':
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Functional coverage tree'''
 
from itertools import chain, product
Line 3,067:
wine_cellar | |1 |
cinema | |0.75 |'''
main()</langsyntaxhighlight>
{{Out}}
<pre>NAME_HIERARCHY | WEIGHT | COVERAGE | SHARE OF RESIDUE
Line 3,118:
(in this case <code>data/functional-coverage.txt</code>).
 
<langsyntaxhighlight lang="racket">#lang racket/base
(require racket/list racket/string racket/match racket/format racket/file)
 
Line 3,189:
(for-each
(compose print-coverage-tree find-wght-cvrg)
(build-hierarchies (report->indent.c/e-list (file->string "data/functional-coverage.txt")))))</langsyntaxhighlight>
 
{{out}}
Line 3,240:
(formerly Perl 6)
{{trans|Perl}}
<syntaxhighlight lang="raku" perl6line>sub walktree ($data) {
my (@parts, $cnt);
 
Line 3,311:
cinema | |0.75 |
END
</syntaxhighlight>
</lang>
{{out}}
<pre style="font-size:70%;">NAME_HIERARCHY |WEIGHT |COVERAGE |
Line 3,362:
{{trans|Kotlin}}
 
<langsyntaxhighlight lang="swift">import Foundation
 
extension String {
Line 3,528:
print("\nIf the coverage of the Cinema node were increased from 0.75 to 1.0")
print("the top level coverage would increase by ")
print("\(String(format: "%8.6f", diff)) to \(String(format: "%8.6f", top))")</langsyntaxhighlight>
 
{{out}}
Line 3,584:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
class FCNode {
Line 3,730:
System.write("the top level coverage would increase by ")
Fmt.print("$8.6f to $8.6f", diff, topCoverage + diff)
h2Basement[2].coverage = 0.75 // restore to original value if required</langsyntaxhighlight>
 
{{out}}
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.