Skip to content

Commit

Permalink
vimscript/difference-of-squares: 1st iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed Apr 16, 2024
1 parent 783a342 commit b7cba71
Show file tree
Hide file tree
Showing 10 changed files with 342 additions and 4 deletions.
1 change: 1 addition & 0 deletions vimscript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
- [reverse-string](./reverse-string/README.md)
- [roman-numerals](./roman-numerals/README.md)
- [scrabble-score](./scrabble-score/README.md)
- [difference-of-squares](./difference-of-squares/README.md)
1 change: 1 addition & 0 deletions vimscript/difference-of-squares/.coverage_covimerage
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!coverage.py: This is a private format, don't read it directly!{"lines":{"/home/vpayno/git_vpayno/exercism-workspace/vimscript/difference-of-squares/difference_of_squares.vim":[14,18,24]},"file_tracers":{"/home/vpayno/git_vpayno/exercism-workspace/vimscript/difference-of-squares/difference_of_squares.vim":"covimerage.CoveragePlugin"}}
3 changes: 3 additions & 0 deletions vimscript/difference-of-squares/.coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[run]
plugins = covimerage
data_file = .coverage_covimerage
17 changes: 17 additions & 0 deletions vimscript/difference-of-squares/.themisrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
" .themisrc

let g:repo_root = fnamemodify(expand('<sfile>'), ':h:h')

call themis#option('exclude', g:repo_root . '/*.md')
call themis#option('exclude', g:repo_root . '/*.vader')
call themis#option('exclude', g:repo_root . '/*.txt')
call themis#helper('command').with(themis#helper('assert'))

if $PROFILE_LOG !=# ''
execute 'profile' 'start' $PROFILE_LOG
execute 'profile!' 'file' g:repo_root . '/*.vim'
endif

call themis#option('runtimepath', expand(g:repo_root))

" vim:ft=vim
10 changes: 9 additions & 1 deletion vimscript/difference-of-squares/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ Finding the best algorithm for the problem is a key skill in software engineerin

### Based on

Problem 6 at Project Euler - https://projecteuler.net/problem=6
Problem 6 at Project Euler - https://projecteuler.net/problem=6

### My Solution

- [my solution](./difference_of_squares.vim)
- [vader tests](./difference_of_squares.vader)
- [themis tests](./themis.vimspec)
- [themis profile](./profile.txt)
- [run-tests output](./run-tests-vimscript.txt)
26 changes: 26 additions & 0 deletions vimscript/difference-of-squares/coverage.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" ?>
<coverage branch-rate="0" branches-covered="0" branches-valid="0" complexity="0" line-rate="0.4286" lines-covered="3" lines-valid="7" timestamp="1713236661829" version="4.5.4">
<!-- Generated by coverage.py: https://coverage.readthedocs.io -->
<!-- Based on https://raw.githubusercontent.com/cobertura/web/master/htdocs/xml/coverage-04.dtd -->
<sources>
<source>/home/vpayno/git_vpayno/exercism-workspace/vimscript/difference-of-squares</source>
</sources>
<packages>
<package branch-rate="0" complexity="0" line-rate="0.4286" name=".">
<classes>
<class branch-rate="0" complexity="0" filename="difference_of_squares.vim" line-rate="0.4286" name="difference_of_squares.vim">
<methods/>
<lines>
<line hits="1" number="14"/>
<line hits="0" number="15"/>
<line hits="1" number="18"/>
<line hits="0" number="19"/>
<line hits="0" number="21"/>
<line hits="1" number="24"/>
<line hits="0" number="25"/>
</lines>
</class>
</classes>
</package>
</packages>
</coverage>
8 changes: 5 additions & 3 deletions vimscript/difference-of-squares/difference_of_squares.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
" 22
"
function! DifferenceOfSquares(number) abort
" your implementation goes here
return SquareOfSum(a:number) - SumOfSquares(a:number)
endfunction

function! SquareOfSum(number) abort
" your implementation goes here
let l:result = a:number * (a:number + 1) / 2

return l:result * l:result
endfunction

function! SumOfSquares(number) abort
" your implementation goes here
return a:number * (a:number + 1) * (2 * a:number + 1) / 6
endfunction
74 changes: 74 additions & 0 deletions vimscript/difference-of-squares/profile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
SCRIPT /home/vpayno/git_vpayno/exercism-workspace/vimscript/difference-of-squares/difference_of_squares.vim
Sourced 1 time
Total time: 0.000017730
Self time: 0.000017730

count total (s) self (s)
"
" Find the difference between the square of the sum and the sum of the squares
" of the first N natural numbers.
"
" Examples:
"
" :echo SquareOfSum(3)
" 36
" :echo SumOfSquares(3)
" 14
" :echo DifferenceOfSquares(3)
" 22
"
1 0.000003921 function! DifferenceOfSquares(number) abort
return SquareOfSum(a:number) - SumOfSquares(a:number)
endfunction

1 0.000000749 function! SquareOfSum(number) abort
let l:result = a:number * (a:number + 1) / 2

return l:result * l:result
endfunction

1 0.000000887 function! SumOfSquares(number) abort
return a:number * (a:number + 1) * (2 * a:number + 1) / 6
endfunction

FUNCTION SumOfSquares()
Defined: ~/git_vpayno/exercism-workspace/vimscript/difference-of-squares/difference_of_squares.vim:24
Called 6 times
Total time: 0.000010721
Self time: 0.000010721

count total (s) self (s)
6 0.000010011 return a:number * (a:number + 1) * (2 * a:number + 1) / 6

FUNCTION DifferenceOfSquares()
Defined: ~/git_vpayno/exercism-workspace/vimscript/difference-of-squares/difference_of_squares.vim:14
Called 3 times
Total time: 0.000024316
Self time: 0.000009151

count total (s) self (s)
3 0.000023471 0.000008306 return SquareOfSum(a:number) - SumOfSquares(a:number)

FUNCTION SquareOfSum()
Defined: ~/git_vpayno/exercism-workspace/vimscript/difference-of-squares/difference_of_squares.vim:18
Called 6 times
Total time: 0.000018945
Self time: 0.000018945

count total (s) self (s)
6 0.000010624 let l:result = a:number * (a:number + 1) / 2

6 0.000005401 return l:result * l:result

FUNCTIONS SORTED ON TOTAL TIME
count total (s) self (s) function
3 0.000024316 0.000009151 DifferenceOfSquares()
6 0.000018945 SquareOfSum()
6 0.000010721 SumOfSquares()

FUNCTIONS SORTED ON SELF TIME
count total (s) self (s) function
6 0.000018945 SquareOfSum()
6 0.000010721 SumOfSquares()
3 0.000024316 0.000009151 DifferenceOfSquares()

126 changes: 126 additions & 0 deletions vimscript/difference-of-squares/run-tests-vimscript.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
Running automated test file(s):


===============================================================================

Running: vint --warning --verbose --enable-neovim .
vint DEBUG: checking: `difference_of_squares.vim`
vint DEBUG: severity: WARNING
vint DEBUG: disabled: ProhibitAbbreviationOption
vint DEBUG: enabled: ProhibitAutocmdWithNoGroup
vint DEBUG: enabled: ProhibitCommandRelyOnUser
vint DEBUG: enabled: ProhibitCommandWithUnintendedSideEffect
vint DEBUG: enabled: ProhibitEncodingOptionAfterScriptEncoding
vint DEBUG: enabled: ProhibitEqualTildeOperator
vint DEBUG: enabled: ProhibitImplicitScopeBuiltinVariable
vint DEBUG: disabled: ProhibitImplicitScopeVariable
vint DEBUG: enabled: ProhibitInvalidMapCall
vint DEBUG: enabled: ProhibitMissingScriptEncoding
vint DEBUG: enabled: ProhibitNoAbortFunction
vint DEBUG: enabled: ProhibitSetNoCompatible
vint DEBUG: enabled: ProhibitUnnecessaryDoubleQuote
vint DEBUG: disabled: ProhibitUnusedVariable
vint DEBUG: enabled: ProhibitUsingUndeclaredVariable

real 0m0.314s
user 0m0.201s
sys 0m0.115s

===============================================================================

Running: vim '+source *vim | Vader!*' ./*.vader && echo Success || echo Failure
Vim: Warning: Output is not to a terminal
[?1049h[?1h=[?2004h[?25l"./difference_of_squares.vader" 46L, 1081B[?2004l[?1l>[?1049l[?25hVader note: cannot print to stderr reliably/directly. Please consider using Vim's -es/-Es option (mode=n).
VIM - Vi IMproved 9.1 (2024 Jan 02, compiled Jan 24 2024 13:08:58)
Included patches: 1-50
Compiled by vpayno@penguin
Huge version with GTK2 GUI. Features included (+) or not (-):
+acl +cmdline_hist +ex_extra +jumplist +mouse_dec +perl/dyn -sodium +textobjects +wildmenu
+arabic +cmdline_info +extra_search +keymap -mouse_gpm +persistent_undo +sound +textprop +windows
+autocmd +comments -farsi +lambda -mouse_jsbterm +popupwin +spell +timers +writebackup
+autochdir +conceal +file_in_path +langmap +mouse_netterm +postscript +startuptime +title +X11
-autoservername +cryptv +find_in_path +libcall +mouse_sgr +printer +statusline +toolbar +xattr
+balloon_eval +cscope +float +linebreak -mouse_sysmouse +profile -sun_workshop +user_commands -xfontset
+balloon_eval_term +cursorbind +folding +lispindent +mouse_urxvt -python +syntax +vartabs +xim
+browse +cursorshape -footer +listcmds +mouse_xterm +python3/dyn +tag_binary +vertsplit -xpm
++builtin_terms +dialog_con_gui +fork() +localmap +multi_byte +quickfix -tag_old_static +vim9script +xsmp_interact
+byte_offset +diff +gettext +lua/dyn +multi_lang +reltime -tag_any_white +viminfo +xterm_clipboard
+channel +digraphs -hangul_input +menu -mzscheme +rightleft +tcl/dyn +virtualedit -xterm_save
+cindent +dnd +iconv +mksession +netbeans_intg +ruby +termguicolors +visual
+clientserver -ebcdic +insert_expand +modify_fname +num64 +scrollbind +terminal +visualextra
+clipboard +emacs_tags +ipv6 +mouse +packages +signs +terminfo +vreplace
+cmdline_compl +eval +job +mouseshape +path_extra +smartindent +termresponse +wildignore
system vimrc file: "$VIM/vimrc"
user vimrc file: "$HOME/.vimrc"
2nd user vimrc file: "~/.vim/vimrc"
user exrc file: "$HOME/.exrc"
system gvimrc file: "$VIM/gvimrc"
user gvimrc file: "$HOME/.gvimrc"
2nd user gvimrc file: "~/.vim/gvimrc"
defaults file: "$VIMRUNTIME/defaults.vim"
system menu file: "$VIMRUNTIME/menu.vim"
fall-back for $VIM: "/home/vpayno/.local/vim/usr/share/vim"
f-b for $VIMRUNTIME: "/home/vpayno/.local/vim/usr/share/vim/vim82"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_GTK -pthread -I/usr/include/gtk-2.0 -I/usr/lib/x86_64-linux-gnu/gtk-2.0/include -I/usr/include/pango-1.0 -I/usr/include/atk-1.0 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/pango-1.0 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/fribidi -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/harfbuzz -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/uuid -I/usr/include/freetype2 -I/usr/include/libpng16 -g -O2 -D_REENTRANT -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: gcc -Wl,-E -rdynamic -Wl,-export-dynamic -L/usr/local/lib -Wl,--as-needed -o vim -lgtk-x11-2.0 -lgdk-x11-2.0 -lpangocairo-1.0 -latk-1.0 -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lpangoft2-1.0 -lpango-1.0 -lgobject-2.0 -lglib-2.0 -lharfbuzz -lfontconfig -lfreetype -lSM -lICE -lXt -lX11 -lXdmcp -lSM -lICE -lm -ltinfo -lselinux -lcanberra -lrt -ldl -L/home/vpayno/.local/activetcl-8.6/lib -ltclstub8.6 -ldl -lz -lpthread -lm -Wl,-rpath,/home/vpayno/.rbenv/versions/3.1.1/lib -L/home/vpayno/.rbenv/versions/3.1.1/lib -lruby -lm

Starting Vader: 1 suite(s), 9 case(s)
Starting Vader: /home/vpayno/git_vpayno/exercism-workspace/vimscript/difference-of-squares/difference_of_squares.vader
(1/9) [EXECUTE] square of sum 1
(2/9) [EXECUTE] square of sum 5
(3/9) [EXECUTE] square of sum 100
(4/9) [EXECUTE] sum of squares 1
(5/9) [EXECUTE] sum of squares 5
(6/9) [EXECUTE] sum of squares 100
(7/9) [EXECUTE] difference of squares 1
(8/9) [EXECUTE] difference of squares 5
(9/9) [EXECUTE] difference of squares 100
Success/Total: 9/9
Success/Total: 9/9 (assertions: 9/9)
Elapsed time: 0.12 sec.
[?1049h[?1h=[?2004h[?2004l[?2004h[?2004l[?2004l[?1l>[?1049l
real 0m2.545s
user 0m0.335s
sys 0m0.147s
Success

===============================================================================

Running: themis ./themis.vimspec
1..3
ok 1 - thesis tests test_DifferenceOfSquares
ok 2 - thesis tests test_SquareOfSum
ok 3 - thesis tests test_SumOfSquares

# tests 3
# passes 3

real 0m0.032s
user 0m0.020s
sys 0m0.008s

===============================================================================

Script line does not match function line, ignoring: ' return a:number * (a:number + 1) * (2 * a:number + 1) / 6' != '10011 return a:number * (a:number + 1) * (2 * a:number + 1) / 6'.
Script line does not match function line, ignoring: ' return SquareOfSum(a:number) - SumOfSquares(a:number)' != '08306 return SquareOfSum(a:number) - SumOfSquares(a:number)'.
Script line does not match function line, ignoring: ' let l:result = a:number * (a:number + 1) / 2' != '10624 let l:result = a:number * (a:number + 1) / 2'.
Could not find source for function: SumOfSquares
Could not find source for function: DifferenceOfSquares
Could not find source for function: SquareOfSum
Writing coverage file .coverage_covimerage.
Coverage.py warning: Plugin file tracers (covimerage.CoveragePlugin) aren't supported with PyTracer
Name Stmts Miss Cover
-----------------------------------------------
difference_of_squares.vim 7 4 43%
Coverage.py warning: Plugin file tracers (covimerage.CoveragePlugin) aren't supported with PyTracer

===============================================================================

Running: misspell .

real 0m0.019s
user 0m0.012s
sys 0m0.019s

===============================================================================

Expand Down
80 changes: 80 additions & 0 deletions vimscript/difference-of-squares/themis.vimspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
" themis.vimspec

let s:suite = themis#suite('thesis tests')
let s:assert = themis#helper('assert')

source *.vim

" SquareOfSum
let g:test_cases1 = {
\ 1: 1,
\ 5: 225,
\ 100: 25502500
\ }

" SumOfSquares
let g:test_cases2 = {
\ 1: 1,
\ 5: 55,
\ 100: 338350
\ }

" DifferenceOfSquares
let g:test_cases3 = {
\ 1: 0,
\ 5: 170,
\ 100: 25164150
\ }

" The function name(my_test_1) will be a test name.
function s:suite.test_SquareOfSum()
let l:number = 0
let l:result = 0

for l:key in keys(g:test_cases1)
let l:number = l:key
let l:result = g:test_cases1[l:key]

let l:got = SquareOfSum(l:number)
let l:want = l:result

" appending the number to the want/got values to make it easier to debug failures
call s:assert.equals(l:number . ':' . l:want, l:number . ':' . l:got)
endfor
endfunction

" The function name(my_test_1) will be a test name.
function s:suite.test_SumOfSquares()
let l:number = 0
let l:result = 0

for l:key in keys(g:test_cases2)
let l:number = l:key
let l:result = g:test_cases2[l:key]

let l:got = SumOfSquares(l:number)
let l:want = l:result

" appending the number to the want/got values to make it easier to debug failures
call s:assert.equals(l:number . ':' . l:want, l:number . ':' . l:got)
endfor
endfunction

" The function name(my_test_1) will be a test name.
function s:suite.test_DifferenceOfSquares()
let l:number = 0
let l:result = 0

for l:key in keys(g:test_cases3)
let l:number = l:key
let l:result = g:test_cases3[l:key]

let l:got = DifferenceOfSquares(l:number)
let l:want = l:result

" appending the number to the want/got values to make it easier to debug failures
call s:assert.equals(l:number . ':' . l:want, l:number . ':' . l:got)
endfor
endfunction

" vim: ft=vim

0 comments on commit b7cba71

Please sign in to comment.