How to use lcs method of diff Package

Best Got code snippet using diff.lcs

diff_test.go

Source:diff_test.go Github

copy

Full Screen

...55		name     string56		a        string57		b        string58		ed       int59		lcs      string60		ses      []SesElem[rune]61		uniHunks []UniHunk[rune]62	}{63		{64			name: "string diff1",65			a:    "abc",66			b:    "abd",67			ed:   2,68			lcs:  "ab",69			ses: []SesElem[rune]{70				{e: 'a', t: SesCommon, aIdx: 1, bIdx: 1},71				{e: 'b', t: SesCommon, aIdx: 2, bIdx: 2},72				{e: 'c', t: SesDelete, aIdx: 3, bIdx: 0},73				{e: 'd', t: SesAdd, aIdx: 0, bIdx: 3},74			},75			uniHunks: []UniHunk[rune]{76				{a: 1, b: 3, c: 1, d: 3,77					changes: []SesElem[rune]{78						{e: 'a', t: SesCommon, aIdx: 1, bIdx: 1},79						{e: 'b', t: SesCommon, aIdx: 2, bIdx: 2},80						{e: 'c', t: SesDelete, aIdx: 3, bIdx: 0},81						{e: 'd', t: SesAdd, aIdx: 0, bIdx: 3},82					},83				},84			},85		},86		{87			name: "string diff2",88			a:    "abcdef",89			b:    "dacfea",90			ed:   6,91			lcs:  "acf",92			ses: []SesElem[rune]{93				{e: 'd', t: SesAdd, aIdx: 0, bIdx: 1},94				{e: 'a', t: SesCommon, aIdx: 1, bIdx: 2},95				{e: 'b', t: SesDelete, aIdx: 2, bIdx: 0},96				{e: 'c', t: SesCommon, aIdx: 3, bIdx: 3},97				{e: 'd', t: SesDelete, aIdx: 4, bIdx: 0},98				{e: 'e', t: SesDelete, aIdx: 5, bIdx: 0},99				{e: 'f', t: SesCommon, aIdx: 6, bIdx: 4},100				{e: 'e', t: SesAdd, aIdx: 0, bIdx: 5},101				{e: 'a', t: SesAdd, aIdx: 0, bIdx: 6},102			},103			uniHunks: []UniHunk[rune]{104				{a: 1, b: 6, c: 1, d: 6,105					changes: []SesElem[rune]{106						{e: 'd', t: SesAdd, aIdx: 0, bIdx: 1},107						{e: 'a', t: SesCommon, aIdx: 1, bIdx: 2},108						{e: 'b', t: SesDelete, aIdx: 2, bIdx: 0},109						{e: 'c', t: SesCommon, aIdx: 3, bIdx: 3},110						{e: 'd', t: SesDelete, aIdx: 4, bIdx: 0},111						{e: 'e', t: SesDelete, aIdx: 5, bIdx: 0},112						{e: 'f', t: SesCommon, aIdx: 6, bIdx: 4},113						{e: 'e', t: SesAdd, aIdx: 0, bIdx: 5},114						{e: 'a', t: SesAdd, aIdx: 0, bIdx: 6},115					},116				},117			},118		},119		{120			name: "string diff3",121			a:    "acbdeacbed",122			b:    "acebdabbabed",123			ed:   6,124			lcs:  "acbdabed",125			ses: []SesElem[rune]{126				{e: 'a', t: SesCommon, aIdx: 1, bIdx: 1},127				{e: 'c', t: SesCommon, aIdx: 2, bIdx: 2},128				{e: 'e', t: SesAdd, aIdx: 0, bIdx: 3},129				{e: 'b', t: SesCommon, aIdx: 3, bIdx: 4},130				{e: 'd', t: SesCommon, aIdx: 4, bIdx: 5},131				{e: 'e', t: SesDelete, aIdx: 5, bIdx: 0},132				{e: 'a', t: SesCommon, aIdx: 6, bIdx: 6},133				{e: 'c', t: SesDelete, aIdx: 7, bIdx: 0},134				{e: 'b', t: SesCommon, aIdx: 8, bIdx: 7},135				{e: 'b', t: SesAdd, aIdx: 0, bIdx: 8},136				{e: 'a', t: SesAdd, aIdx: 0, bIdx: 9},137				{e: 'b', t: SesAdd, aIdx: 0, bIdx: 10},138				{e: 'e', t: SesCommon, aIdx: 9, bIdx: 11},139				{e: 'd', t: SesCommon, aIdx: 10, bIdx: 12},140			},141			uniHunks: []UniHunk[rune]{142				{a: 1, b: 10, c: 1, d: 12,143					changes: []SesElem[rune]{144						{e: 'a', t: SesCommon, aIdx: 1, bIdx: 1},145						{e: 'c', t: SesCommon, aIdx: 2, bIdx: 2},146						{e: 'e', t: SesAdd, aIdx: 0, bIdx: 3},147						{e: 'b', t: SesCommon, aIdx: 3, bIdx: 4},148						{e: 'd', t: SesCommon, aIdx: 4, bIdx: 5},149						{e: 'e', t: SesDelete, aIdx: 5, bIdx: 0},150						{e: 'a', t: SesCommon, aIdx: 6, bIdx: 6},151						{e: 'c', t: SesDelete, aIdx: 7, bIdx: 0},152						{e: 'b', t: SesCommon, aIdx: 8, bIdx: 7},153						{e: 'b', t: SesAdd, aIdx: 0, bIdx: 8},154						{e: 'a', t: SesAdd, aIdx: 0, bIdx: 9},155						{e: 'b', t: SesAdd, aIdx: 0, bIdx: 10},156						{e: 'e', t: SesCommon, aIdx: 9, bIdx: 11},157						{e: 'd', t: SesCommon, aIdx: 10, bIdx: 12},158					},159				},160			},161		},162		{163			name: "string diff4",164			a:    "abcbda",165			b:    "bdcaba",166			ed:   4,167			lcs:  "bcba",168			ses: []SesElem[rune]{169				{e: 'a', t: SesDelete, aIdx: 1, bIdx: 0},170				{e: 'b', t: SesCommon, aIdx: 2, bIdx: 1},171				{e: 'd', t: SesAdd, aIdx: 0, bIdx: 2},172				{e: 'c', t: SesCommon, aIdx: 3, bIdx: 3},173				{e: 'a', t: SesAdd, aIdx: 0, bIdx: 4},174				{e: 'b', t: SesCommon, aIdx: 4, bIdx: 5},175				{e: 'd', t: SesDelete, aIdx: 5, bIdx: 0},176				{e: 'a', t: SesCommon, aIdx: 6, bIdx: 6},177			},178			uniHunks: []UniHunk[rune]{179				{a: 1, b: 6, c: 1, d: 6,180					changes: []SesElem[rune]{181						{e: 'a', t: SesDelete, aIdx: 1, bIdx: 0},182						{e: 'b', t: SesCommon, aIdx: 2, bIdx: 1},183						{e: 'd', t: SesAdd, aIdx: 0, bIdx: 2},184						{e: 'c', t: SesCommon, aIdx: 3, bIdx: 3},185						{e: 'a', t: SesAdd, aIdx: 0, bIdx: 4},186						{e: 'b', t: SesCommon, aIdx: 4, bIdx: 5},187						{e: 'd', t: SesDelete, aIdx: 5, bIdx: 0},188						{e: 'a', t: SesCommon, aIdx: 6, bIdx: 6},189					},190				},191			},192		},193		{194			name: "string diff5",195			a:    "bokko",196			b:    "bokkko",197			ed:   1,198			lcs:  "bokko",199			ses: []SesElem[rune]{200				{e: 'b', t: SesCommon, aIdx: 1, bIdx: 1},201				{e: 'o', t: SesCommon, aIdx: 2, bIdx: 2},202				{e: 'k', t: SesCommon, aIdx: 3, bIdx: 3},203				{e: 'k', t: SesCommon, aIdx: 4, bIdx: 4},204				{e: 'k', t: SesAdd, aIdx: 0, bIdx: 5},205				{e: 'o', t: SesCommon, aIdx: 5, bIdx: 6},206			},207			uniHunks: []UniHunk[rune]{208				{a: 2, b: 4, c: 2, d: 5,209					changes: []SesElem[rune]{210						{e: 'o', t: SesCommon, aIdx: 2, bIdx: 2},211						{e: 'k', t: SesCommon, aIdx: 3, bIdx: 3},212						{e: 'k', t: SesCommon, aIdx: 4, bIdx: 4},213						{e: 'k', t: SesAdd, aIdx: 0, bIdx: 5},214						{e: 'o', t: SesCommon, aIdx: 5, bIdx: 6},215					},216				},217			},218		},219		{220			name: "string diff6",221			a:    "abcaaaaaabd",222			b:    "abdaaaaaabc",223			ed:   4,224			lcs:  "abaaaaaab",225			ses: []SesElem[rune]{226				{e: 'a', t: SesCommon, aIdx: 1, bIdx: 1},227				{e: 'b', t: SesCommon, aIdx: 2, bIdx: 2},228				{e: 'c', t: SesDelete, aIdx: 3, bIdx: 0},229				{e: 'd', t: SesAdd, aIdx: 0, bIdx: 3},230				{e: 'a', t: SesCommon, aIdx: 4, bIdx: 4},231				{e: 'a', t: SesCommon, aIdx: 5, bIdx: 5},232				{e: 'a', t: SesCommon, aIdx: 6, bIdx: 6},233				{e: 'a', t: SesCommon, aIdx: 7, bIdx: 7},234				{e: 'a', t: SesCommon, aIdx: 8, bIdx: 8},235				{e: 'a', t: SesCommon, aIdx: 9, bIdx: 9},236				{e: 'b', t: SesCommon, aIdx: 10, bIdx: 10},237				{e: 'd', t: SesDelete, aIdx: 11, bIdx: 0},238				{e: 'c', t: SesAdd, aIdx: 0, bIdx: 11},239			},240			uniHunks: []UniHunk[rune]{241				{a: 1, b: 6, c: 1, d: 6,242					changes: []SesElem[rune]{243						{e: 'a', t: SesCommon, aIdx: 1, bIdx: 1},244						{e: 'b', t: SesCommon, aIdx: 2, bIdx: 2},245						{e: 'c', t: SesDelete, aIdx: 3, bIdx: 0},246						{e: 'd', t: SesAdd, aIdx: 0, bIdx: 3},247						{e: 'a', t: SesCommon, aIdx: 4, bIdx: 4},248						{e: 'a', t: SesCommon, aIdx: 5, bIdx: 5},249						{e: 'a', t: SesCommon, aIdx: 6, bIdx: 6},250					},251				},252				{a: 8, b: 4, c: 8, d: 4,253					changes: []SesElem[rune]{254						{e: 'a', t: SesCommon, aIdx: 8, bIdx: 8},255						{e: 'a', t: SesCommon, aIdx: 9, bIdx: 9},256						{e: 'b', t: SesCommon, aIdx: 10, bIdx: 10},257						{e: 'd', t: SesDelete, aIdx: 11, bIdx: 0},258						{e: 'c', t: SesAdd, aIdx: 0, bIdx: 11},259					},260				},261			},262		},263		{264			name:     "empty string diff1",265			a:        "",266			b:        "",267			ed:       0,268			lcs:      "",269			ses:      []SesElem[rune]{},270			uniHunks: []UniHunk[rune]{},271		},272		{273			name: "empty string diff2",274			a:    "a",275			b:    "",276			ed:   1,277			lcs:  "",278			ses: []SesElem[rune]{279				{e: 'a', t: SesDelete, aIdx: 1, bIdx: 0},280			},281			uniHunks: []UniHunk[rune]{282				{a: 1, b: 1, c: 0, d: 0, changes: []SesElem[rune]{283					{e: 'a', t: SesDelete, aIdx: 1, bIdx: 0},284				},285				},286			},287		},288		{289			name: "empty string diff3",290			a:    "",291			b:    "b",292			ed:   1,293			lcs:  "",294			ses: []SesElem[rune]{295				{e: 'b', t: SesAdd, aIdx: 0, bIdx: 1},296			},297			uniHunks: []UniHunk[rune]{298				{a: 0, b: 0, c: 1, d: 1, changes: []SesElem[rune]{299					{e: 'b', t: SesAdd, aIdx: 0, bIdx: 1},300				},301				},302			},303		},304		{305			name: "multi byte string diff",306			a:    "久保竜彦",307			b:    "久保達彦",308			ed:   2,309			lcs:  "久保彦",310			ses: []SesElem[rune]{311				{e: '久', t: SesCommon, aIdx: 1, bIdx: 1},312				{e: '保', t: SesCommon, aIdx: 2, bIdx: 2},313				{e: '竜', t: SesDelete, aIdx: 3, bIdx: 0},314				{e: '達', t: SesAdd, aIdx: 0, bIdx: 3},315				{e: '彦', t: SesCommon, aIdx: 4, bIdx: 4},316			},317			uniHunks: []UniHunk[rune]{318				{a: 1, b: 4, c: 1, d: 4, changes: []SesElem[rune]{319					{e: '久', t: SesCommon, aIdx: 1, bIdx: 1},320					{e: '保', t: SesCommon, aIdx: 2, bIdx: 2},321					{e: '竜', t: SesDelete, aIdx: 3, bIdx: 0},322					{e: '達', t: SesAdd, aIdx: 0, bIdx: 3},323					{e: '彦', t: SesCommon, aIdx: 4, bIdx: 4},324				},325				},326			},327		},328	}329	for _, tt := range tests {330		diff := New([]rune(tt.a), []rune(tt.b))331		diff.Compose()332		ed := diff.Editdistance()333		lcs := string(diff.Lcs())334		ses := diff.Ses()335		uniHunks := diff.UnifiedHunks()336		if tt.ed != ed {337			t.Fatalf(":%s:ed: want: %d, got: %d", tt.name, tt.ed, ed)338		}339		if tt.lcs != lcs {340			t.Fatalf(":%s:lcs: want: %s, got: %s", tt.name, tt.lcs, lcs)341		}342		if !equalsSesElemSlice(tt.ses, ses) {343			t.Fatalf(":%s:ses: want: %v, got: %v", tt.name, tt.ses, ses)344		}345		if !equalsUniHunks(tt.uniHunks, uniHunks) {346			t.Fatalf(":%s:uniHunks: want: %v, got: %v", tt.name, tt.uniHunks, tt.uniHunks)347		}348	}349}350func TestSliceDiff(t *testing.T) {351	tests := []struct {352		name     string353		a        []int354		b        []int355		ed       int356		lcs      []int357		ses      []SesElem[int]358		uniHunks []UniHunk[int]359	}{360		{361			name: "int slice diff",362			a:    []int{1, 2, 3},363			b:    []int{1, 5, 3},364			ed:   2,365			lcs:  []int{1, 3},366			ses: []SesElem[int]{367				{e: 1, t: SesCommon, aIdx: 1, bIdx: 1},368				{e: 2, t: SesDelete, aIdx: 2, bIdx: 0},369				{e: 5, t: SesAdd, aIdx: 0, bIdx: 2},370				{e: 3, t: SesCommon, aIdx: 3, bIdx: 3},371			},372			uniHunks: []UniHunk[int]{373				{a: 1, b: 3, c: 1, d: 3,374					changes: []SesElem[int]{375						{e: 1, t: SesCommon, aIdx: 1, bIdx: 1},376						{e: 2, t: SesDelete, aIdx: 2, bIdx: 0},377						{e: 5, t: SesAdd, aIdx: 0, bIdx: 2},378						{e: 3, t: SesCommon, aIdx: 3, bIdx: 3},379					},380				},381			},382		},383		{384			name:     "empty slice diff",385			a:        []int{},386			b:        []int{},387			ed:       0,388			lcs:      []int{},389			ses:      []SesElem[int]{},390			uniHunks: []UniHunk[int]{},391		},392	}393	for _, tt := range tests {394		diff := New(tt.a, tt.b)395		diff.Compose()396		ed := diff.Editdistance()397		lcs := diff.Lcs()398		ses := diff.Ses()399		uniHunks := diff.UnifiedHunks()400		if tt.ed != ed {401			t.Fatalf(":%s:ed: want: %d, got: %d", tt.name, tt.ed, ed)402		}403		if !equalsSlice(tt.lcs, lcs) {404			t.Fatalf(":%s:lcs: want: %v, got: %v", tt.name, tt.lcs, lcs)405		}406		if !equalsSesElemSlice(tt.ses, ses) {407			t.Fatalf(":%s:ses: want: %v, got: %v", tt.name, tt.ses, ses)408		}409		if !equalsUniHunks(tt.uniHunks, uniHunks) {410			t.Fatalf(":%s:uniHunks: want: %v, got: %v", tt.name, tt.uniHunks, tt.uniHunks)411		}412	}413}414func TestPluralDiff(t *testing.T) {415	a := []rune("abc")416	b := []rune("abd")417	diff := New(a, b)418	diff.SetRouteSize(1)419	diff.Compose()420	lcs := string(diff.Lcs())421	sesActual := diff.Ses()422	sesExpected := []SesElem[rune]{423		{e: 'a', t: SesCommon, aIdx: 1, bIdx: 1},424		{e: 'b', t: SesCommon, aIdx: 2, bIdx: 2},425		{e: 'c', t: SesDelete, aIdx: 3, bIdx: 0},426		{e: 'd', t: SesAdd, aIdx: 0, bIdx: 3},427	}428	if diff.Editdistance() != 2 {429		t.Fatalf("want: 2, actual: %v", diff.Editdistance())430	}431	if lcs != "ab" {432		t.Fatalf("want: ab, actual: %v", lcs)433	}434	if !equalsSesElemSlice(sesActual, sesExpected) {435		t.Fatalf("want: %v, actual: %v", sesExpected, sesActual)436	}437	uniHunksActual := diff.UnifiedHunks()438	uniHunksExpected := []UniHunk[rune]{439		{a: 1, b: 3, c: 1, d: 3, changes: sesExpected},440	}441	if !equalsUniHunks(uniHunksActual, uniHunksExpected) {442		t.Fatalf(":uniHunks: want: %v, got: %v", uniHunksExpected, uniHunksActual)443	}444}445func TestDiffOnlyEditdistance(t *testing.T) {446	a := []rune("abc")447	b := []rune("abd")448	diff := New(a, b)449	diff.OnlyEd()450	diff.Compose()451	lcs := string(diff.Lcs())452	sesActual := diff.Ses()453	sesExpected := []SesElem[rune]{}454	uniHunksActual := diff.UnifiedHunks()455	uniHunksExpected := []UniHunk[rune]{}456	if diff.Editdistance() != 2 {457		t.Fatalf("want: 2, actual: %v", diff.Editdistance())458	}459	if lcs != "" {460		t.Fatalf("want: \"\", actual: %v", lcs)461	}462	if !equalsSesElemSlice(sesActual, sesExpected) {463		t.Fatalf("want: %v, actual: %v", sesExpected, sesActual)464	}465	if !equalsUniHunks(uniHunksActual, uniHunksExpected) {466		t.Fatalf(":uniHunks: want: %v, got: %v", uniHunksExpected, uniHunksActual)467	}468}469func TestDiffPluralSubsequence(t *testing.T) {470	a := []rune("abcaaaaaabd")471	b := []rune("abdaaaaaabc")472	diff := New(a, b)473	diff.SetRouteSize(2) // dividing sequences forcibly474	diff.Compose()...

Full Screen

Full Screen

model_test.go

Source:model_test.go Github

copy

Full Screen

...42}43func TestModelFail(t *testing.T) {44	ms := []struct {45		m   hd.Model46		lcs []hd.LoadCase47		mcs []hd.ModalCase48	}{49		// error of input data50		{51			m: hd.Model{52				Points: [][2]float64{53					{-math.MaxFloat64 - 1, math.NaN()},54					{0.0, 0.0},55					{0.0, math.Inf(0)},56				},57				Beams: []hd.BeamProp{58					{N: [2]int{-1, 1}, A: -12e-4, J: -120e-6, E: -2.0e11},59					{N: [2]int{1, 1}, A: 12e-4, J: 120e-6, E: 2.0e11},60					{N: [2]int{1, 20}, A: 12e-4, J: math.NaN(), E: math.Inf(-1)},61				},62				Supports: [][3]bool{63					{true, true, true},64					{false, false, false},65					{false, true, false},66					{false, false, false},67					{false, false, false},68					{false, false, false},69					{false, false, false},70					{false, false, false},71					{false, false, false},72				},73				Pins: [][6]bool{74					{true, true, true, true, true, false},75					{true, false, false, true, false, false},76				},77			},78			lcs: []hd.LoadCase{79				{80					LoadNodes: []hd.LoadNode{81						{N: -1, Forces: [3]float64{0, 2.3, 0}},82						{N: 5, Forces: [3]float64{math.Inf(1), 0, math.NaN()}},83					},84					AmountLinearBuckling: 1,85				},86			},87			mcs: []hd.ModalCase{88				{ModalMasses: []hd.ModalMass{{N: 7, Mass: -100}}},89				{ModalMasses: []hd.ModalMass{{N: -1, Mass: math.NaN()}}},90				{ModalMasses: []hd.ModalMass{{N: 0, Mass: math.NaN()}}},91				{}, // Modal mass is empty92			},93		},94		// error of inpossible solving - all points are free. No fix supports95		{96			m: hd.Model{97				Points: [][2]float64{98					{0, 0},99					{0, 1},100					{1, 1},101				},102				Beams: []hd.BeamProp{103					{N: [2]int{0, 1}, A: 12e-4, J: 120e-6, E: 2.0e11},104					{N: [2]int{1, 2}, A: 12e-4, J: 120e-6, E: 2.0e11},105				},106				Supports: [][3]bool{107					{false, false, false},108					{false, false, false},109					{false, false, false},110				},111			},112			lcs: []hd.LoadCase{113				{114					LoadNodes: []hd.LoadNode{115						{N: 1, Forces: [3]float64{0, 2.3, 0}},116					},117				},118			},119			mcs: []hd.ModalCase{120				{ModalMasses: []hd.ModalMass{{N: 1, Mass: 100}}},121			},122		},123		// error - too big model124		{125			m: hd.Model{126				Points: [][2]float64{127					{0, 0},128					{-1e300, 1e300},129					{1e300, 1e300},130				},131				Beams: []hd.BeamProp{132					{N: [2]int{0, 1}, A: 12e-300, J: 120e-300, E: 2.0e300},133					{N: [2]int{1, 2}, A: 12e+300, J: 120e+300, E: 2.0e300},134				},135				Supports: [][3]bool{136					{true, true, true},137					{false, false, false},138					{false, false, false},139				},140			},141			lcs: []hd.LoadCase{142				{143					LoadNodes: []hd.LoadNode{144						{N: 1, Forces: [3]float64{0, 2.3, 0}},145					},146				},147			},148			mcs: []hd.ModalCase{149				{ModalMasses: []hd.ModalMass{{N: 1, Mass: 100}}},150			},151		},152		// error - too big load153		{154			m: hd.Model{155				Points: [][2]float64{156					{0, 0},157					{0, 1},158					{1, 1},159				},160				Beams: []hd.BeamProp{161					{N: [2]int{0, 1}, A: 12e-30, J: 120e-30, E: 2.0e30},162					{N: [2]int{1, 2}, A: 12e-30, J: 120e-30, E: 2.0e30},163				},164				Supports: [][3]bool{165					{true, true, true},166					{false, false, false},167					{false, false, false},168				},169			},170			lcs: []hd.LoadCase{171				{172					LoadNodes: []hd.LoadNode{173						{N: 2, Forces: [3]float64{1e300, 1e308, 1e300}},174						{N: 2, Forces: [3]float64{1e300, 1e308, 1e300}},175					},176				},177			},178			mcs: []hd.ModalCase{179				{ModalMasses: []hd.ModalMass{{N: 1, Mass: 1e300}}},180			},181		},182		// error - too small load183		{184			m: hd.Model{185				Points: [][2]float64{186					{0, 0},187					{0, 1},188					{1, 1},189				},190				Beams: []hd.BeamProp{191					{N: [2]int{0, 1}, A: 12e-30, J: math.SmallestNonzeroFloat64, E: math.SmallestNonzeroFloat64},192					{N: [2]int{1, 2}, A: 12e-30, J: 120e-30, E: 2.0e30},193				},194				Pins: [][6]bool{195					{false, false, false, false, false, false},196					{false, false, true, false, false, true},197					{false, false, false, false, false, false},198				},199				Supports: [][3]bool{200					{true, true, true},201					{false, false, false},202					{false, false, false},203				},204			},205			lcs: []hd.LoadCase{206				{207					LoadNodes: []hd.LoadNode{208						{N: 20, Forces: [3]float64{0, 1, 0}},209						{N: -20, Forces: [3]float64{0, 1, 0}},210					},211				},212			},213			mcs: []hd.ModalCase{214				{ModalMasses: []hd.ModalMass{215					{N: 1, Mass: 1},216					{N: -1, Mass: -1},217					{N: 10000, Mass: 0},218				}},219			},220		},221		// error - rigid222		{223			m: hd.Model{224				Points: [][2]float64{225					{0, 0},226					{0, math.SmallestNonzeroFloat64},227					{1, math.SmallestNonzeroFloat64},228				},229				Beams: []hd.BeamProp{230					{N: [2]int{0, 1}, A: 1e200, J: 1e200, E: 1e200},231					{N: [2]int{1, 2}, A: 1e200, J: 1e200, E: 1e200},232				},233				Supports: [][3]bool{234					{true, true, true},235					{false, false, false},236					{false, false, false},237				},238			},239			lcs: []hd.LoadCase{240				{241					LoadNodes: []hd.LoadNode{242						{N: 2, Forces: [3]float64{1e20, 1e20, 0}},243					},244				},245			},246			mcs: []hd.ModalCase{247				{ModalMasses: []hd.ModalMass{248					{N: 1, Mass: 1e20},249					{N: -10, Mass: 1e20},250					{N: 10, Mass: 1e20},251				}},252			},253		},254		// error - two models in one model255		{256			m: hd.Model{257				Points: [][2]float64{258					{0, 0},259					{1, 0},260					{2, 0},261					{0, 1},262					{1, 1},263					{2, 0},264				},265				Beams: []hd.BeamProp{266					{N: [2]int{0, 1}, A: 1e200, J: 1e200, E: 1e200},267					{N: [2]int{1, 2}, A: 1e200, J: 1e200, E: 1e200},268					{N: [2]int{3, 4}, A: 1e200, J: 1e200, E: 1e200},269					{N: [2]int{4, 5}, A: 1e200, J: 1e200, E: 1e200},270				},271				Supports: [][3]bool{272					{true, true, true},273					{false, false, false},274					{false, false, false},275					{true, true, true},276					{false, false, false},277					{false, false, false},278				},279			},280			lcs: []hd.LoadCase{281				{282					LoadNodes: []hd.LoadNode{283						{N: 2, Forces: [3]float64{1e20, 1e20, 0}},284						{N: 5, Forces: [3]float64{1e20, 1e20, 0}},285					},286				},287			},288			mcs: []hd.ModalCase{289				{ModalMasses: []hd.ModalMass{290					{N: 1, Mass: 1},291				}},292			},293		},294	}295	for i := range ms {296		t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {297			var err error298			defer func() {299				if err == nil {300					if r := recover(); r == nil {301						t.Errorf("panic is not happen and no error")302					} else {303						t.Log(r)304						debug.PrintStack()305					}306				}307			}()308			var b bytes.Buffer309			err = hd.Run(&b, &(ms[i].m), ms[i].lcs, ms[i].mcs)310			if err == nil {311				t.Fatalf("Error : %v", err)312			}313			b.Write([]byte(err.Error()))314			t.Log(err)315		})316	}317}318func TestCodeStyle(t *testing.T) {319	cs.All(t)320}321func TestWriter(t *testing.T) {322	m, lcs, mcs := example.Truss()323	var tf *os.File324	var err error325	if tf, err = ioutil.TempFile("", "testWriter"); err != nil {326		t.Fatalf("Cannot create temp file: %v", err)327	}328	if err = hd.Run(tf, &m, lcs, mcs); err != nil {329		t.Fatalf("Cannot calculate : %v", err)330	}331	if err = tf.Close(); err != nil {332		t.Fatalf("Cannot close file : %v", err)333	}334	b, err := ioutil.ReadFile(tf.Name())335	if err != nil {336		t.Fatalf("Cannot read file : %v", err)337	}338	if len(b) == 0 {339		t.Fatalf("File is empty")340	}341}342func TestDirectionLoadNode(t *testing.T) {343	m := hd.Model{344		Points: [][2]float64{345			{0.0, 0.0},346			{1.0, 0.0},347		},348		Beams: []hd.BeamProp{349			{350				N: [2]int{0, 1},351				A: 12e-4,352				J: 120e-6,353				E: 2.0e11,354			},355		},356		Supports: [][3]bool{357			{true, true, true},358			{false, false, false},359		},360	}361	lcs := []hd.LoadCase{362		{363			LoadNodes: []hd.LoadNode{364				{N: 1, Forces: [3]float64{100.0, 0.0, 0.0}},365			},366		},367		{368			LoadNodes: []hd.LoadNode{369				{N: 1, Forces: [3]float64{0.0, 100.0, 0.0}},370			},371		},372		{373			LoadNodes: []hd.LoadNode{374				{N: 1, Forces: [3]float64{0.0, 0.0, 100.0}},375			},376		},377	}378	if err := hd.Run(nil, &m, lcs, nil); err != nil {379		t.Fatalf("Cannot calculate : %v", err)380	}381	if !(lcs[0].PointDisplacementGlobal[1][0] > 0) {382		t.Errorf("load in direction X is not ok. See: %v", lcs[0].PointDisplacementGlobal[1])383	}384	if !(lcs[1].PointDisplacementGlobal[1][1] > 0) {385		t.Errorf("load in direction Y is not ok. See: %v", lcs[1].PointDisplacementGlobal[1])386	}387	if !(lcs[2].PointDisplacementGlobal[1][1] > 0) {388		t.Errorf("load in direction M is not ok. See: %v", lcs[2].PointDisplacementGlobal[1])389	}390	t.Logf("case 0 : %v", lcs[0].PointDisplacementGlobal)391	t.Logf("case 1 : %v", lcs[1].PointDisplacementGlobal)392	t.Logf("case 2 : %v", lcs[2].PointDisplacementGlobal)393}394func TestRotateBeamLinear(t *testing.T) {395	for angle := 0.0; angle <= 360.0; angle += 5 {396		const (397			F float64 = 100.0398			L         = 2.200399		)400		var (401			Fx = F * math.Cos(math.Pi/180.0*angle)402			Fy = F * math.Sin(math.Pi/180.0*angle)403			x  = L * math.Cos(math.Pi/180.0*angle)404			y  = L * math.Sin(math.Pi/180.0*angle)405		)406		m := hd.Model{407			Points: [][2]float64{{0.0, 0.0}, {x / 2, y / 2}, {x, y}},408			Beams: []hd.BeamProp{409				{N: [2]int{0, 1}, A: 12e-4, J: 120e-6, E: 2.0e11},410				{N: [2]int{1, 2}, A: 12e-4, J: 120e-6, E: 2.0e11},411			},412			Supports: [][3]bool{{true, true, true}, {false, false, false}, {false, false, false}},413		}414		lcs := hd.LoadCase{LoadNodes: []hd.LoadNode{{N: 2, Forces: [3]float64{Fx, Fy, 0.0}}}}415		if err := hd.LinearStatic(nil, &m, &lcs); err != nil {416			t.Fatalf("Cannot calculate : %v", err)417		}418		// tolerance419		tol := 1e-6420		// compare beam force421		{422			react := math.Sqrt(math.Pow(lcs.BeamForces[0][0], 2) + math.Pow(lcs.BeamForces[0][1], 2))423			if diff := math.Abs((react - F) / F); diff > tol {424				t.Errorf("Not valid beam force on angle %5.1f : %8.5e", angle, diff)425			}426		}427		{428			react := math.Sqrt(math.Pow(lcs.BeamForces[0][3], 2) + math.Pow(lcs.BeamForces[0][4], 2))429			if diff := math.Abs((react - F) / F); diff > tol {430				t.Errorf("Not valid beam force on angle %5.1f : %8.5e", angle, diff)431			}432		}433		{434			react := math.Sqrt(math.Pow(lcs.BeamForces[1][0], 2) + math.Pow(lcs.BeamForces[1][1], 2))435			if diff := math.Abs((react - F) / F); diff > tol {436				t.Errorf("Not valid beam force on angle %5.1f : %8.5e", angle, diff)437			}438		}439		{440			react := math.Sqrt(math.Pow(lcs.BeamForces[1][3], 2) + math.Pow(lcs.BeamForces[1][4], 2))441			if diff := math.Abs((react - F) / F); diff > tol {442				t.Errorf("Not valid beam force on angle %5.1f : %8.5e", angle, diff)443			}444		}445		// compare reaction446		{447			react := math.Sqrt(math.Pow(lcs.Reactions[0][0], 2) + math.Pow(lcs.Reactions[0][1], 2))448			if diff := math.Abs((react - F) / F); diff > tol {449				t.Errorf("Not valid reaction on angle %5.1f : %8.5e", angle, diff)450			}451		}452	}453}454func TestRotateBeamModal(t *testing.T) {455	var hz [2]float64456	for angle := 0.0; angle <= 360.0; angle += 5 {457		const (458			L = 2.200459		)460		var (461			x = L * math.Cos(math.Pi/180.0*angle)462			y = L * math.Sin(math.Pi/180.0*angle)463		)464		m := hd.Model{465			Points: [][2]float64{{0.0, 0.0}, {x / 2, y / 2}, {x, y}},466			Beams: []hd.BeamProp{467				{N: [2]int{0, 1}, A: 12e-4, J: 120e-6, E: 2.0e11},468				{N: [2]int{1, 2}, A: 12e-4, J: 120e-6, E: 2.0e11},469			},470			Supports: [][3]bool{{true, true, true}, {false, false, false}, {false, false, false}},471		}472		mass := hd.ModalCase{ModalMasses: []hd.ModalMass{{N: 1, Mass: 50.0}, {N: 2, Mass: 100.0}}}473		if err := hd.Modal(nil, &m, &mass); err != nil {474			t.Fatalf("Cannot calculate : %v", err)475		}476		if angle == 0.0 {477			hz[0] = mass.Result[0].Hz478			hz[1] = mass.Result[1].Hz479			continue480		}481		// tolerance482		tol := 1e-6483		// compare484		for i := 0; i < 2; i++ {485			if diff := math.Abs((hz[i] - mass.Result[i].Hz) / hz[i]); diff > tol {486				t.Errorf("Not valid frequency on angle %5.1f: %8.5e", angle, diff)487			}488		}489	}490}491func Example() {492	m := hd.Model{493		Points: [][2]float64{494			{0.0, 0.0},495			{1.0, 0.0},496		},497		Beams: []hd.BeamProp{498			{N: [2]int{0, 1}, A: 12e-4, J: 120e-6, E: 2.0e11},499		},500		Pins: [][6]bool{501			{false, false, false, false, false, true},502		},503		Supports: [][3]bool{504			{true, true, true},505			{false, false, false},506		},507	}508	lcs := []hd.LoadCase{509		{510			LoadNodes: []hd.LoadNode{511				{N: 1, Forces: [3]float64{0, 2.3, 0}},512				{N: 1, Forces: [3]float64{-10, 0, 0}},513			},514			AmountLinearBuckling: 1,515		},516		{ // test for 2 cases with different positions517			LoadNodes: []hd.LoadNode{518				{N: 1, Forces: [3]float64{-10, 0, 0}},519				{N: 1, Forces: [3]float64{0, 2.3, 0}},520			},521			AmountLinearBuckling: 2,522		},523	}524	mcs := []hd.ModalCase{525		{526			ModalMasses: []hd.ModalMass{{N: 1, Mass: 10000}},527		},528	}529	var b bytes.Buffer530	if err := hd.Run(&b, &m, lcs, mcs); err != nil {531		fmt.Fprintf(os.Stdout, "Cannot calculate : %v", err)532		return533	}534	expect, err := ioutil.ReadFile("./example/testdata/model.String")535	if err != nil {536		panic(fmt.Errorf("Cannot read file : %v", err))537	}538	if bytes.Equal(expect, b.Bytes()) {539		fmt.Fprintf(os.Stdout, "same")540	} else {541		fmt.Fprintln(os.Stdout, b.String())542		// show a diff between files543		diff := difflib.UnifiedDiff{544			A:        difflib.SplitLines(string(expect)),...

Full Screen

Full Screen

gojsondiff.go

Source:gojsondiff.go Github

copy

Full Screen

...7	"encoding/json"8	"reflect"9	"sort"10	dmp "github.com/sergi/go-diff/diffmatchpatch"11	"github.com/yudai/golcs"12)13// A Diff holds deltas generated by a Differ14type Diff interface {15	// Deltas returns Deltas that describe differences between two JSON objects16	Deltas() []Delta17	// Modified returnes true if Diff has at least one Delta.18	Modified() bool19}20type diff struct {21	deltas []Delta22}23func (diff *diff) Deltas() []Delta {24	return diff.deltas25}26func (diff *diff) Modified() bool {27	return len(diff.deltas) > 028}29// A Differ conmapres JSON objects and apply patches30type Differ struct {31	textDiffMinimumLength int32}33// New returns new Differ with default configuration34func New() *Differ {35	return &Differ{36		textDiffMinimumLength: 30,37	}38}39// Compare compares two JSON strings as []bytes and return a Diff object.40func (differ *Differ) Compare(41	left []byte,42	right []byte,43) (Diff, error) {44	var leftMap, rightMap map[string]interface{}45	err := json.Unmarshal(left, &leftMap)46	if err != nil {47		return nil, err48	}49	err = json.Unmarshal(right, &rightMap)50	if err != nil {51		return nil, err52	}53	return differ.CompareObjects(leftMap, rightMap), nil54}55// CompareObjects compares two JSON object as map[string]interface{}56// and return a Diff object.57func (differ *Differ) CompareObjects(58	left map[string]interface{},59	right map[string]interface{},60) Diff {61	deltas := differ.compareMaps(left, right)62	return &diff{deltas: deltas}63}64// CompareArrays compares two JSON arrays as []interface{}65// and return a Diff object.66func (differ *Differ) CompareArrays(67	left []interface{},68	right []interface{},69) Diff {70	deltas := differ.compareArrays(left, right)71	return &diff{deltas: deltas}72}73func (differ *Differ) compareMaps(74	left map[string]interface{},75	right map[string]interface{},76) (deltas []Delta) {77	deltas = make([]Delta, 0)78	names := sortedKeys(left) // stabilize delta order79	for _, name := range names {80		if rightValue, ok := right[name]; ok {81			same, delta := differ.compareValues(Name(name), left[name], rightValue)82			if !same {83				deltas = append(deltas, delta)84			}85		} else {86			deltas = append(deltas, NewDeleted(Name(name), left[name]))87		}88	}89	names = sortedKeys(right) // stabilize delta order90	for _, name := range names {91		if _, ok := left[name]; !ok {92			deltas = append(deltas, NewAdded(Name(name), right[name]))93		}94	}95	return deltas96}97// ApplyPatch applies a Diff to an JSON object. This method is destructive.98func (differ *Differ) ApplyPatch(json map[string]interface{}, patch Diff) {99	applyDeltas(patch.Deltas(), json)100}101type maybe struct {102	index    int103	lcsIndex int104	item     interface{}105}106func (differ *Differ) compareArrays(107	left []interface{},108	right []interface{},109) (deltas []Delta) {110	deltas = make([]Delta, 0)111	// LCS index pairs112	lcsPairs := lcs.New(left, right).IndexPairs()113	// list up items not in LCS, they are maybe deleted114	maybeDeleted := list.New() // but maybe moved or modified115	lcsI := 0116	for i, leftValue := range left {117		if lcsI < len(lcsPairs) && lcsPairs[lcsI].Left == i {118			lcsI++119		} else {120			maybeDeleted.PushBack(maybe{index: i, lcsIndex: lcsI, item: leftValue})121		}122	}123	// list up items not in LCS, they are maybe Added124	maybeAdded := list.New() // but maybe moved or modified125	lcsI = 0126	for i, rightValue := range right {127		if lcsI < len(lcsPairs) && lcsPairs[lcsI].Right == i {128			lcsI++129		} else {130			maybeAdded.PushBack(maybe{index: i, lcsIndex: lcsI, item: rightValue})131		}132	}133	// find moved items134	var delNext *list.Element // for prefetch to remove item in iteration135	for delCandidate := maybeDeleted.Front(); delCandidate != nil; delCandidate = delNext {136		delCan := delCandidate.Value.(maybe)137		delNext = delCandidate.Next()138		for addCandidate := maybeAdded.Front(); addCandidate != nil; addCandidate = addCandidate.Next() {139			addCan := addCandidate.Value.(maybe)140			if reflect.DeepEqual(delCan.item, addCan.item) {141				deltas = append(deltas, NewMoved(Index(delCan.index), Index(addCan.index), delCan.item, nil))142				maybeAdded.Remove(addCandidate)143				maybeDeleted.Remove(delCandidate)144				break145			}146		}147	}148	// find modified or add+del149	prevIndexDel := 0150	prevIndexAdd := 0151	delElement := maybeDeleted.Front()152	addElement := maybeAdded.Front()153	for i := 0; i <= len(lcsPairs); i++ { // not "< len(lcsPairs)"154		var lcsPair lcs.IndexPair155		var delSize, addSize int156		if i < len(lcsPairs) {157			lcsPair = lcsPairs[i]158			delSize = lcsPair.Left - prevIndexDel - 1159			addSize = lcsPair.Right - prevIndexAdd - 1160			prevIndexDel = lcsPair.Left161			prevIndexAdd = lcsPair.Right162		}163		var delSlice []maybe164		if delSize > 0 {165			delSlice = make([]maybe, 0, delSize)166		} else {167			delSlice = make([]maybe, 0, maybeDeleted.Len())168		}169		for ; delElement != nil; delElement = delElement.Next() {170			d := delElement.Value.(maybe)171			if d.lcsIndex != i {172				break173			}174			delSlice = append(delSlice, d)175		}176		var addSlice []maybe177		if addSize > 0 {178			addSlice = make([]maybe, 0, addSize)179		} else {180			addSlice = make([]maybe, 0, maybeAdded.Len())181		}182		for ; addElement != nil; addElement = addElement.Next() {183			a := addElement.Value.(maybe)184			if a.lcsIndex != i {185				break186			}187			addSlice = append(addSlice, a)188		}189		if len(delSlice) > 0 && len(addSlice) > 0 {190			var bestDeltas []Delta191			bestDeltas, delSlice, addSlice = differ.maximizeSimilarities(delSlice, addSlice)192			for _, delta := range bestDeltas {193				deltas = append(deltas, delta)194			}195		}196		for _, del := range delSlice {197			deltas = append(deltas, NewDeleted(Index(del.index), del.item))198		}199		for _, add := range addSlice {200			deltas = append(deltas, NewAdded(Index(add.index), add.item))201		}202	}203	return deltas204}205func (differ *Differ) compareValues(206	position Position,207	left interface{},208	right interface{},209) (same bool, delta Delta) {210	if reflect.TypeOf(left) != reflect.TypeOf(right) {211		return false, NewModified(position, left, right)212	}213	switch left.(type) {214	case map[string]interface{}:215		l := left.(map[string]interface{})216		childDeltas := differ.compareMaps(l, right.(map[string]interface{}))217		if len(childDeltas) > 0 {218			return false, NewObject(position, childDeltas)219		}220	case []interface{}:221		l := left.([]interface{})222		childDeltas := differ.compareArrays(l, right.([]interface{}))223		if len(childDeltas) > 0 {224			return false, NewArray(position, childDeltas)225		}226	default:227		if !reflect.DeepEqual(left, right) {228			if reflect.ValueOf(left).Kind() == reflect.String &&229				reflect.ValueOf(right).Kind() == reflect.String &&230				differ.textDiffMinimumLength <= len(left.(string)) {231				textDiff := dmp.New()232				patchs := textDiff.PatchMake(left.(string), right.(string))233				return false, NewTextDiff(position, patchs, left, right)234			} else {235				return false, NewModified(position, left, right)236			}237		}238	}239	return true, nil240}241func applyDeltas(deltas []Delta, object interface{}) interface{} {242	preDeltas := make(preDeltas, 0)243	for _, delta := range deltas {244		switch delta.(type) {245		case PreDelta:246			preDeltas = append(preDeltas, delta.(PreDelta))247		}248	}249	sort.Sort(preDeltas)250	for _, delta := range preDeltas {251		object = delta.PreApply(object)252	}253	postDeltas := make(postDeltas, 0, len(deltas)-len(preDeltas))254	for _, delta := range deltas {255		switch delta.(type) {256		case PostDelta:257			postDeltas = append(postDeltas, delta.(PostDelta))258		}259	}260	sort.Sort(postDeltas)261	for _, delta := range postDeltas {262		object = delta.PostApply(object)263	}264	return object265}266func (differ *Differ) maximizeSimilarities(left []maybe, right []maybe) (resultDeltas []Delta, freeLeft, freeRight []maybe) {267	deltaTable := make([][]Delta, len(left))268	for i := 0; i < len(left); i++ {269		deltaTable[i] = make([]Delta, len(right))270	}271	for i, leftValue := range left {272		for j, rightValue := range right {273			_, delta := differ.compareValues(Index(rightValue.index), leftValue.item, rightValue.item)274			deltaTable[i][j] = delta275		}276	}277	sizeX := len(left) + 1 // margins for both sides278	sizeY := len(right) + 1279	// fill out with similarities280	dpTable := make([][]float64, sizeX)281	for i := 0; i < sizeX; i++ {282		dpTable[i] = make([]float64, sizeY)283	}284	for x := sizeX - 2; x >= 0; x-- {285		for y := sizeY - 2; y >= 0; y-- {286			prevX := dpTable[x+1][y]287			prevY := dpTable[x][y+1]288			score := deltaTable[x][y].Similarity() + dpTable[x+1][y+1]289			dpTable[x][y] = max(prevX, prevY, score)290		}291	}292	minLength := len(left)293	if minLength > len(right) {294		minLength = len(right)295	}296	maxInvalidLength := minLength - 1297	freeLeft = make([]maybe, 0, len(left)-minLength)298	freeRight = make([]maybe, 0, len(right)-minLength)299	resultDeltas = make([]Delta, 0, minLength)300	var x, y int301	for x, y = 0, 0; x <= sizeX-2 && y <= sizeY-2; {302		current := dpTable[x][y]303		nextX := dpTable[x+1][y]304		nextY := dpTable[x][y+1]305		xValidLength := len(left) - maxInvalidLength + y306		yValidLength := len(right) - maxInvalidLength + x307		if x+1 < xValidLength && current == nextX {308			freeLeft = append(freeLeft, left[x])309			x++310		} else if y+1 < yValidLength && current == nextY {311			freeRight = append(freeRight, right[y])312			y++313		} else {314			resultDeltas = append(resultDeltas, deltaTable[x][y])315			x++316			y++317		}318	}319	for ; x < sizeX-1; x++ {320		freeLeft = append(freeLeft, left[x-1])321	}322	for ; x < sizeY-1; y++ {323		freeLeft = append(freeRight, left[y-1])324	}325	return resultDeltas, freeLeft, freeRight326}327func deltasSimilarity(deltas []Delta) (similarity float64) {328	for _, delta := range deltas {329		similarity += delta.Similarity()330	}331	similarity = similarity / float64(len(deltas))332	return333}334func stringSimilarity(left, right string) (similarity float64) {335	matchingLength := float64(336		lcs.New(337			stringToInterfaceSlice(left),338			stringToInterfaceSlice(right),339		).Length(),340	)341	similarity =342		(matchingLength / float64(len(left))) * (matchingLength / float64(len(right)))343	return344}345func stringToInterfaceSlice(str string) []interface{} {346	s := make([]interface{}, len(str))347	for i, v := range str {348		s[i] = v349	}350	return s...

Full Screen

Full Screen

lcs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3    dmp := diffmatchpatch.New()4    d := dmp.DiffMain(a, b, false)5    fmt.Println(d)6}7[{0 This is a test string} {1 , with some extra text}]8import (9func main() {10    dmp := diffmatchpatch.New()11    d := dmp.DiffMain(a, b, false)12    fmt.Println(d)13}14[{0 This is a test string} {1 , with some extra text}]15import (16func main() {17    dmp := diffmatchpatch.New()18    d := dmp.DiffMain(a, b, false)19    fmt.Println(d)20}21[{0 This is a test string} {1 , with some extra text}]

Full Screen

Full Screen

lcs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3    dmp := diffmatchpatch.New()4    diffs := dmp.DiffMain(a, b, false)5    fmt.Println(diffs)6}7cannot use dmp.DiffMain(a, b, false) (type []diffmatchpatch.Diff) as type []diffmatchpatch.Diff in assignment8You need to import the package as diffmatchpatch9import (10func main() {11    dmp := diffmatchpatch.New()12    diffs := dmp.DiffMain(a, b, false)13    fmt.Println(diffs)14}

Full Screen

Full Screen

lcs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3    dmp := diffmatchpatch.New()4    d := dmp.DiffMain(a, b, false)5    fmt.Println(dmp.DiffPrettyText(d))6}7 import (8 func main() {9     dmp := diffmatchpatch.New()10     d := dmp.DiffMain(a, b, false)11     fmt.Println(dmp.DiffPrettyText(d))12 }13 import (14 func main() {15     dmp := diffmatchpatch.New()16     d := dmp.DiffMain(a, b, false)17     fmt.Println(dmp.DiffPrettyText(d))18 }

Full Screen

Full Screen

lcs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3    dmp := diffmatchpatch.New()4    d := dmp.DiffMain(a, b, false)5    fmt.Println(dmp.DiffPrettyText(d))6}7import (8func main() {9    dmp := diffmatchpatch.New()10    d := dmp.DiffMain(a, b, false)11    fmt.Println(dmp.DiffCommonPrefix(a, b))12    fmt.Println(dmp.DiffCommonSuffix(a, b))13    fmt.Println(dmp.DiffCommonOverlap(a, b))14    fmt.Println(dmp.DiffCommonOverlap(b, a))15}16import (17func main() {18    dmp := diffmatchpatch.New()19    d := dmp.DiffMain(a, b, false)20    fmt.Println(dmp.DiffCommonPrefix(a, b))21    fmt.Println(dmp.DiffCommonSuffix(a, b))22    fmt.Println(dmp.DiffCommonOverlap(a, b))23    fmt.Println(dmp.DiffCommonOverlap(b, a))24}25import (26func main() {27    dmp := diffmatchpatch.New()

Full Screen

Full Screen

lcs

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "github.com/sergi/go-diff/diffmatchpatch"3func main() {4    dmp := diffmatchpatch.New()5    a := []rune("Hello World!")6    b := []rune("Goodbye World!")7    d := dmp.DiffMain(string(a), string(b), false)8    fmt.Println(dmp.DiffPrettyText(d))9}10import "fmt"11import "github.com/sergi/go-diff/diffmatchpatch"12func main() {13    dmp := diffmatchpatch.New()14    a := []rune("Hello World!")15    b := []rune("Goodbye World!")16    d := dmp.DiffMain(string(a), string(b), false)17    fmt.Println(dmp.DiffPrettyText(d))18}19import "fmt"20import "github.com/sergi/go-diff/diffmatchpatch"21func main() {22    dmp := diffmatchpatch.New()23    a := []rune("Hello World!")24    b := []rune("Goodbye World!")25    d := dmp.DiffMain(string(a), string(b), false)26    fmt.Println(dmp.DiffPrettyText(d))27}28import "fmt"29import "github.com/sergi/go-diff/diffmatchpatch"30func main() {31    dmp := diffmatchpatch.New()32    a := []rune("Hello World!")33    b := []rune("Goodbye World!")34    d := dmp.DiffMain(string(a), string(b), false)35    fmt.Println(dmp.DiffPrettyText(d))36}37import "fmt"38import "github.com/sergi/go-diff/diffmatchpatch"39func main() {

Full Screen

Full Screen

lcs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3	dmp := diffmatchpatch.New()4	diffs := dmp.DiffMain(a, b, false)5	fmt.Println(dmp.DiffPrettyText(diffs))6}

Full Screen

Full Screen

lcs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3	fmt.Println("Enter the first string")4	fmt.Scanln(&s1)5	fmt.Println("Enter the second string")6	fmt.Scanln(&s2)7	lcs(s1, s2)8}9func lcs(s1 string, s2 string) {10	m := len(s1)11	n := len(s2)12	for i = 0; i <= m; i++ {13		for j = 0; j <= n; j++ {14			if i == 0 || j == 0 {15			} else if s1[i-1] == s2[j-1] {16			} else {17				L[i][j] = max(L[i-1][j], L[i][j-1])18			}19		}20	}21	for i > 0 && j > 0 {22		if s1[i-1] == s2[j-1] {23			result.WriteByte(s1[i-1])24		} else if L[i-1][j] > L[i][j-1] {25		} else {26		}27	}28	reverseString(&result)

Full Screen

Full Screen

lcs

Using AI Code Generation

copy

Full Screen

1import (2func main() {3    dmp := diffmatchpatch.New()4    fmt.Println(dmp.DiffMain(a, b, false))5}6[{0 Hello World}]

Full Screen

Full Screen

lcs

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "strings"3import "strconv"4import "bufio"5import "os"6import "regexp"7import "time"8import "math/rand"9type Diff struct {10}11func NewDiff(a, b string) *Diff {12    return &Diff{13        c: make([][]int, len(a)+1),14    }15}16func (d *Diff) LCS() int {17    for i := 0; i <= len(d.a); i++ {18        d.c[i] = make([]int, len(d.b)+1)19    }20    for i, x := range d.a {21        for j, y := range d.b {22            if x == y {23            } else if d.c[i+1][j] >= d.c[i][j+1] {24            } else {25            }26        }27    }28    return d.c[len(d.a)][len(d.b)]29}30func (d *Diff) PrintLCS() {31    d.LCS()32    var lcsPrint func(int, int)33    lcsPrint = func(i, j int) {34        if i == 0 || j == 0 {35        }36        if d.a[i-1] == d.b[j-1] {37            lcsPrint(i-1, j-1)38            fmt.Print(string(d.a[i-1]))39        } else if d.c[i][j-1] >= d.c[i-1][j] {40            lcsPrint(i, j-1)41        } else {42            lcsPrint(i-1, j)43        }44    }45    lcsPrint(len(d.a), len(d.b))46    fmt.Println()47}48func (d *Diff) PrintTable() {49    d.LCS()50    for _, row := range d.c {51        for _, val := range row {52            fmt.Printf("%3d ", val)53        }

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful